package _VisualDVM.Passes.All;
import Common.Utils.Utils_;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Passes.Sapfor.SapforAnalysis;
import _VisualDVM.ProjectData.Files.DBProjectFile;
import _VisualDVM.ProjectData.Files.FileType;
import _VisualDVM.ProjectData.Files.LanguageStyle;
import _VisualDVM.ProjectData.LanguageName;
import _VisualDVM.ProjectData.SapforData.Includes.Analysis.FileInfo;
import _VisualDVM.ProjectData.SapforData.Includes.Analysis.Include;
import _VisualDVM.ProjectData.SapforData.Includes.Analysis.Json.FileIncludesJson;
import _VisualDVM.ProjectData.SapforData.Includes.Analysis.Json.IncludesJson;

import javax.swing.tree.DefaultMutableTreeNode;
public class SPF_GetIncludeDependencies extends SapforAnalysis {
    boolean update_current = false;
    @Override
    protected PassCode necessary() {
        return PassCode.SPF_ParseFilesWithOrder;
    }
    @Override
    public String phase() {
        return "BUILD_INCLUDE_DEPENDENCIES";
    }
    @Override
    protected void performPreparation() throws Exception {
        super.performPreparation(); //удаление интеррупта.
        target.numAddicted = 0;
        target.addictedFiles.clear();
        target.includes_root.removeAllChildren();
        target.allIncludes.clear();
        for (DBProjectFile file : target.db.files.Data.values())
            file.dependencies.clear();
    }
    @Override
    public void unpack(String packed) throws Exception {
        IncludesJson includesJson = Utils_.gson.fromJson(packed, IncludesJson.class);
        update_current = false;
        for (FileIncludesJson fileIncludesJson : includesJson.allIncludes) {
            //-
            fileIncludesJson.file = Utils_.toW(fileIncludesJson.file);
            //--
            FileInfo fileInfo = new FileInfo(fileIncludesJson.file);
            //---
            DBProjectFile file = target.db.files.get(fileIncludesJson.file);
            System.out.println("addicted file = " + Utils_.Brackets(fileIncludesJson.file));
            System.out.println("found dependencies :" + fileIncludesJson.includes.size());
            ///  UI.Info("+");
            for (Include include : fileIncludesJson.includes) {
                //--
                System.out.println(include.dependencyFileName);
                System.out.println(include.line);
                include.dependencyFileName = Utils_.toW(include.dependencyFileName);
                //--
                fileInfo.dependencies.add(include);
                if (!target.allIncludes.contains(include.dependencyFileName))
                    target.allIncludes.add(include.dependencyFileName);
                if (target.db.files.containsKey(include.dependencyFileName)) {
                    //- определение типов языков инклудов по файлу куда они цепляютс.
                    if (!file.dependencies.contains(include.dependencyFileName)) {
                        file.dependencies.add(include.dependencyFileName);
                        //---
                        DBProjectFile include_file = target.db.files.get(include.dependencyFileName);
                        include_file.UpdateType(FileType.header);
                        if (include_file.languageName == LanguageName.n)
                            include_file.UpdateLanguage(file.languageName);
                        if (include_file.style == LanguageStyle.none)
                            include_file.UpdateStyle(file.style);
                        if (Global.mainModule.HasFile() && Global.mainModule.getFile().name.equals(include_file.name))
                            update_current = true;
                    }
                } else {
                    System.out.println(Utils_.Brackets(include.dependencyFileName) + " не существует!");
                }
            }
            //----
            if (!target.addictedFiles.containsKey(fileInfo.file))
                target.addictedFiles.put(fileInfo.file, fileInfo);
        }
        //-теперь строим граф. todo отображать его аналогично графу процедур?
        for (FileInfo file : target.addictedFiles.values()) {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(
                    file);
            for (Include h : file.dependencies) {
                node.add(new DefaultMutableTreeNode(h));
            }
            target.includes_root.add(node);
        }
        target.numAddicted += target.addictedFiles.size();
        target.UpdateAddictedCount();
        //  UI.Info("unpack includes done");
    }
    @Override
    protected void showDone() throws Exception {
        Global.mainModule.getUI().getMainWindow().getProjectWindow().ShowIncludes();
        if (update_current)
            Global.mainModule.getFile().form.ShowProperties();
        super.showDone();
    }
    @Override
    protected void showPreparation() {
        Global.mainModule.getUI().getMainWindow().getProjectWindow().ShowNoIncludes();
    }
    @Override
    protected void FocusResult() {
        super.FocusResult();
        Global.mainModule.getUI().getMainWindow().getProjectWindow().FocusDependencies();
    }
}
