package _VisualDVM.Visual.Windows;
import Common.MainModule_;
import Common.Utils.Utils_;
import Common.Visual.Controls.TabToolBar;
import Common.Visual.Editor.Viewer;
import Common.Visual.FormWithSplitters;
import Common.Visual.Trees.TreeForm;
import Common.Visual.UI;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.ProjectData.Files.DBProjectFile;
import _VisualDVM.ProjectData.Files.UI.FilesTree;
import _VisualDVM.ProjectData.ProjectView;
import _VisualDVM.ProjectData.SapforData.Functions.UI.InlineTree;
import _VisualDVM.ProjectData.SapforData.Functions.UI.InlineTree2;
import _VisualDVM.ProjectData.SapforData.Includes.UI.IncludesTree;
import _VisualDVM.Visual.Interface.AnalysisWindow;
import _VisualDVM.Visual.Interface.ArraysWindow;
import _VisualDVM.Visual.Interface.FunctionsWindow;
import _VisualDVM.Visual.Interface.ProjectWindow;
import _VisualDVM.Visual.Menus.ProjectMenuBar.ProjectMenuBar;

import javax.swing.*;
import java.awt.*;
public class ProjectForm implements FormWithSplitters, ProjectWindow {
    private final ArraysWindow arraysForm;
    private final FunctionsWindow functionsForm;
    private final AnalysisWindow analysisForm;
    private final JScrollPane FastAccessScroll;
    public JSplitPane SC3;
    //</editor-fold>
    public JPanel content;
    public TreeForm includesForm;
    ProjectMenuBar menuBar;
    private JTabbedPane projectTabs;
    private TreeForm filesForm;
    private Viewer projectDescription = null;
    private JScrollPane logScroll;
    private JPanel inlinePanel2;
    private JPanel inlinePanel;
    private TreeForm inlineForm;
    private TreeForm inlineForm2;
    private Viewer logText;
    private JPanel includesPanel;
    private JPanel fileBackground;
    private JPanel arraysFormPanel;
    private JPanel functionsFormPanel;
    private JPanel analysisFormPanel;
    private JPanel projectViewPanel;
    private JPanel projectTreesBackground;
    private JPanel projectBackground;
    public ProjectForm() {
        LoadSplitters();
        //-
        projectTreesBackground.add(menuBar = new ProjectMenuBar(), BorderLayout.NORTH);
        arraysFormPanel.add((arraysForm = new ArraysForm()).getContent());
        functionsFormPanel.add((functionsForm = new FunctionsForm()).getContent());
        analysisFormPanel.add((analysisForm = new AnalysisForm()).getContent());
        //-
        FastAccessScroll = new JScrollPane(MainModule_.instance.getUI().getFastAccessMenuBar()) {
            {
                getHorizontalScrollBar().setPreferredSize(new Dimension(0, 10));
                setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
                setPreferredSize(new Dimension(-1, 45));
            }
        };
        content.add(FastAccessScroll, BorderLayout.NORTH);
        //-
        filesForm.Show();
        InstallVersionsPanel();
        InstallDebugPanel();
        Global.mainModule.getUI().getDebugWindow().ShowAll();
        ShowAllAnalyses();
        ShowProjectView();
        SwitchScreen(Global.normalProperties.SmallScreen);
        RefreshTabsNames();
        //--
        if (Global.normalProperties.collapseProjectTrees)
            CollapseProjectTrees();
        //--
    }
    //-
    @Override
    public ArraysWindow getArraysWindow() {
        return arraysForm;
    }
    @Override
    public FunctionsWindow getFunctionsWindow() {
        return functionsForm;
    }
    @Override
    public AnalysisWindow getAnalysisWindow() {
        return analysisForm;
    }
    @Override
    public void SaveSplitters() {
        FormWithSplitters.super.SaveSplitters();
        arraysForm.SaveSplitters();
        analysisForm.SaveSplitters();
        functionsForm.SaveSplitters();
    }
    private void createUIComponents() {
        // TODO: place custom component creation code here
        filesForm = new TreeForm(FilesTree.class, new JPanel(new BorderLayout()));
        projectDescription = new Viewer();
        logText = new Viewer();
        logScroll = new JScrollPane(logText);
        includesForm = new TreeForm(IncludesTree.class, new JPanel(new BorderLayout()));
        inlineForm = new TreeForm(InlineTree.class, new JPanel(new BorderLayout()));
        inlineForm2 = new TreeForm(InlineTree2.class, new JPanel(new BorderLayout()));
    }
    @Override
    public void RefreshProjectFiles() {
        filesForm.RedrawControl();
    }
    @Override
    public void ShowSelectedDirectory() {
    }
    @Override
    public void ShowSelectedFile() {
    }
    @Override
    public void ShowNoSelectedFile() {
    }
    @Override
    public void ShowProjectSapforLog() {
        logText.setText(Global.mainModule.getProject().Log);
        logText.setCaretPosition(0);
    }
    @Override
    public void ShowFunctions() {
        functionsForm.ShowFunctions();
        inlineForm.Show();
        inlineForm2.Show();
    }
    @Override
    public void ShowNoFunctions() {
        functionsForm.ShowNoFunctions();
        inlineForm.Clear();
        inlineForm2.Clear();
    }
    @Override
    public void ShowNoIncludes() {
        includesForm.Clear();
    }
    @Override
    public void ShowIncludes() {
        includesForm.Show();
    }
    @Override
    public void ShowFile() {
        //пока текущий файл только один.
        //filesTabs.insertTab(null, null, (Current.getFile().form = new FileForm(Current.getFile())).content,
        //        "текущий файл проекта", 0);
        //filesTabs.setTabComponentAt(0,
        //        new TabToolBar(Current.getFile().name, PassCode_2021.CloseCurrentFile));
        // projectTabs.setTitleAt(0, "Файл: " + Current.getFile().name);
        fileBackground.add((Global.mainModule.getFile().form = new FileForm(Global.mainModule.getFile())).getContent());
        projectTabs.setTabComponentAt(0,
                new TabToolBar("Файл: " + Global.mainModule.getFile().name, PassCode.CloseCurrentFile) {
                    @Override
                    public void leftMouseAction() {
                        projectTabs.setSelectedIndex(0);
                    }
                });
    }
    @Override
    public void ShowNoFile() {
        //  projectTabs.setTitleAt(0, "Файл: -");
        projectTabs.setTabComponentAt(0, new TabToolBar("Файл: -") {
            @Override
            public void leftMouseAction() {
                projectTabs.setSelectedIndex(0);
            }
        });
        UI.Clear(fileBackground);
        //filesTabs.removeAll();
    }
    @Override
    public void GotoFile(String fileName, int line, boolean focus) {
        if (focus) FocusFile();
        if (Global.mainModule.HasFile() && Global.mainModule.getFile().name.equals(fileName))
            Global.mainModule.getFile().form.getEditor().gotoLine(line);
        else if (Global.mainModule.getProject().db.files.containsKey(fileName)) {
            DBProjectFile targetFile = Global.mainModule.getProject().db.files.get(fileName);
            targetFile.UpdateLastLine(line);
            Global.mainModule.getPass(PassCode.OpenCurrentFile).Do(targetFile);
        }
    }
    @Override
    public void FocusFile() {
        projectTabs.setSelectedIndex(0);
    }
    @Override
    public void FocusFileTabs() {
        projectTabs.requestFocus();
    }
    @Override
    public void FocusDependencies() {
        FocusFile();
        menuBar.projectViewMenu.SelectView(ProjectView.Includes);
    }
    @Override
    public void FocusArrays() {
        projectTabs.setSelectedIndex(1);
    }
    @Override
    public void FocusFunctions() {
        projectTabs.setSelectedIndex(2);
        menuBar.projectViewMenu.SelectView(ProjectView.FunctionsHierarchy);
    }
    @Override
    public void FocusAnalysis() {
        projectTabs.setSelectedIndex(3);
    }
    @Override
    public void FocusHierarchy() {
        FocusFile();
        menuBar.projectViewMenu.SelectView(ProjectView.FunctionsHierarchy);
    }
    @Override
    public void FocusPoints() {
        FocusFile();
        menuBar.projectViewMenu.SelectView(ProjectView.FunctionsCallsPoints);
    }
    @Override
    public void RefreshTabsNames() {
        UI.ShowTabsNames(projectTabs, 1, Global.normalProperties.ShowFullTabsNames);
    }
    @Override
    public void RefreshProjectTreeAndMessages() {
        RefreshProjectFiles();
        DBProjectFile badFile = Global.mainModule.getProject().getFirstBadFile();
        if (badFile != null) {
            Global.mainModule.getPass(PassCode.OpenCurrentFile).Do(badFile);
            Global.mainModule.getUI().getMainWindow().FocusProject();
            Global.mainModule.getUI().getMainWindow().getProjectWindow().FocusFile();
        }
        //-
        if (Global.mainModule.HasFile()) {
            Global.mainModule.getFile().form.ShowMessages();
            if (badFile != null && badFile.equals(Global.mainModule.getFile()))
                Global.mainModule.getFile().form.ShowFirstError();
        }
    }
    @Override
    public TreeForm getFilesTreeForm() {
        return filesForm;
    }
    @Override
    public void SwitchScreen(boolean small) {
        FastAccessScroll.setVisible(!small);
        content.repaint();
        content.revalidate();
    }
    @Override
    public void ShowProjectView() {
        UI.Clear(projectViewPanel);
        ProjectView view = Global.mainModule.getProjectView();
        menuBar.getProjectViewMenu().setToolTipText(view.getDescription());
        menuBar.getProjectViewMenu().setIcon(Utils_.getIcon(view.getIcon()));
        switch (view) {
            case Files:
                projectViewPanel.add(filesForm.getMountPanel());
                break;
            case Includes:
                projectViewPanel.add(includesForm.getMountPanel());
                break;
            case FunctionsHierarchy:
                projectViewPanel.add(inlineForm2.getMountPanel());
                break;
            case FunctionsCallsPoints:
                projectViewPanel.add(inlineForm.getMountPanel());
                break;
        }
        projectViewPanel.revalidate();
    }
    //-
    private void InstallVersionsPanel() {
        projectTabs.insertTab("",
                Utils_.getIcon("/icons/VersionsTree.png"),
                Global.mainModule.getUI().getVersionsWindow().getContent(),
                "Версии", 5);
    }
    private void InstallDebugPanel() {
        projectTabs.insertTab("",
                Utils_.getIcon("/Common/icons/Command.png"),
                Global.mainModule.getUI().getDebugWindow().getContent(),
                "Компиляция и запуск", 6);
    }
    @Override
    public void FocusVersions() {
        projectTabs.setSelectedIndex(5);
    }
    //--
    @Override
    public void CollapseProjectTrees() {
        Global.mainModule.getDb().splitters.get("SC3").position = SC3.getDividerLocation();
        //---
        UI.Clear(projectBackground);
        projectBackground.add(fileBackground);
    }
    @Override
    public void ExpandProjectTrees() {
        SC3.setLeftComponent(fileBackground);
        projectBackground.add(SC3);
        SC3.setDividerLocation(Global.mainModule.getDb().splitters.get("SC3").position);
        SC3.updateUI();
    }
}