package _VisualDVM.Visual.Menus;
import Common.Utils.Utils_;
import Common.Visual.Controls.StableMenuItem;
import Common.Visual.Menus.GraphMenu;
import Common.Visual.Menus.VisualiserMenuItem;
import Common.Visual.Trees.StyledTree;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.ProjectData.Files.FileType;
import _VisualDVM.ProjectData.Files.LanguageStyle;
import _VisualDVM.ProjectData.LanguageName;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class ProjectFilesMenu extends GraphMenu {
    VisualiserMenuItem m_select_all;
    VisualiserMenuItem m_unselect_all;
    VisualiserMenuItem m_multiselection;
    JMenu mLanguage;
    JMenu mStyle;
    JMenu mType;
    public ProjectFilesMenu(StyledTree tree) {
        super(tree, "подпапки");
        addSeparator();
        JMenuItem m = new VisualiserMenuItem("Открыть в проводнике...", "/Common/icons/Explorer.png");
        m.addActionListener(
                new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                            Desktop.getDesktop().open(Global.mainModule.getProject().Home);
                        } catch (Exception ex) {
                            Utils_.MainLog.PrintException(ex);
                        }
                    }
                });
        add(m);
        addSeparator();
        add(new PassesSubMenu("Добавить", "/Common/icons/RedAdd.png",
                PassCode.AddFile,
                PassCode.CreateEmptyDirectory,
                PassCode.ImportFiles));
        addSeparator();
        add(new PassesSubMenu("Переименовать", "/icons/Menu/Rename.png",
                PassCode.RenameFile,
                PassCode.RenameDirectory));
        add(new VisualiserMenuItem("Удалить текущий проект", "/Common/icons/Delete.png") {
            {
                addActionListener(e -> {
                    if (Global.mainModule.HasProject()) {
                        Global.mainModule.getUI().getVersionsWindow().getVersionsForm().getTree().SelectNode(Global.mainModule.getProject().node);
                        Global.mainModule.getPass(PassCode.DeleteVersion).Do();
                    }
                });
            }
        });
        addSeparator();
        m_multiselection = new VisualiserMenuItem("Массовый режим работы с файлами");
        m_multiselection.setIcon(Utils_.getIcon(Global.files_multiselection ? "/Common/icons/Pick.png" : "/Common/icons/NotPick.png"));
        m_multiselection.addActionListener(e -> {
            Global.files_multiselection = !Global.files_multiselection;
            m_multiselection.setIcon(Utils_.getIcon(Global.files_multiselection ? "/Common/icons/Pick.png" : "/Common/icons/NotPick.png"));
            Global.mainModule.getProject().SelectAllFiles(false);
            Global.mainModule.getUI().getMainWindow().getProjectWindow().RefreshProjectFiles();
            //-
        });
        add(m_multiselection);
        addSeparator();
        //-
        m_select_all = new StableMenuItem("Выбрать всё", "/Common/icons/SelectAll.png");
        m_select_all.addActionListener(e -> {
            Global.mainModule.getProject().SelectAllFiles(true);
            tree.updateUI();
        });
        add(m_select_all);
        m_unselect_all = new StableMenuItem("Отменить всё", "/Common/icons/UnselectAll.png");
        m_unselect_all.addActionListener(e -> {
            Global.mainModule.getProject().SelectAllFiles(false);
            tree.updateUI();
        });
        add(m_unselect_all);
        //--------------------------------------------------
        add(mLanguage = new LanguagesSubmenu(PassCode.SetSelectedFilesLanguage.getDescription()) {
            @Override
            public void action(LanguageName languageName) {
                Global.mainModule.getPass(PassCode.SetSelectedFilesLanguage).Do(languageName);
            }
        });
        add(mStyle = new StylesSubmenu(PassCode.SetSelectedFilesStyle.getDescription()) {
            @Override
            public void action(LanguageStyle languageStyle) {
                Global.mainModule.getPass(PassCode.SetSelectedFilesStyle).Do(languageStyle);
            }
        });
        add(mType = new TypesSubmenu(PassCode.SetSelectedFilesType.getDescription()) {
            @Override
            public void action(FileType fileType) {
                Global.mainModule.getPass(PassCode.SetSelectedFilesType).Do(fileType);
            }
        });
        //--------------------------------------------------
        add(Global.mainModule.getPass(PassCode.ExcludeSelectedFiles).createMenuItem());
        add(Global.mainModule.getPass(PassCode.IncludeSelectedFiles).createMenuItem());
        add(Global.mainModule.getPass(PassCode.ExcludeFile).createMenuItem());
        add(Global.mainModule.getPass(PassCode.IncludeFile).createMenuItem());
        add(Global.mainModule.getPass(PassCode.DeleteFile).createMenuItem());
        add(Global.mainModule.getPass(PassCode.DeleteDirectory).createMenuItem());
        add(Global.mainModule.getPass(PassCode.DeleteSelectedFiles).createMenuItem());
    }
    @Override
    public void CheckElementsVisibility() {
        m_select_all.setVisible(Global.files_multiselection);
        m_unselect_all.setVisible(Global.files_multiselection);
        mLanguage.setVisible(Global.files_multiselection);
        mStyle.setVisible(Global.files_multiselection);
        mType.setVisible(Global.files_multiselection);
        //-
        Global.mainModule.getPass(PassCode.ExcludeFile).setControlsVisible(!Global.files_multiselection);
        Global.mainModule.getPass(PassCode.IncludeFile).setControlsVisible(!Global.files_multiselection);
        //-
        Global.mainModule.getPass(PassCode.DeleteFile).setControlsVisible(!Global.files_multiselection);
        Global.mainModule.getPass(PassCode.DeleteDirectory).setControlsVisible(!Global.files_multiselection);
        //--
        Global.mainModule.getPass(PassCode.ExcludeSelectedFiles).setControlsVisible(Global.files_multiselection);
        Global.mainModule.getPass(PassCode.IncludeSelectedFiles).setControlsVisible(Global.files_multiselection);
        Global.mainModule.getPass(PassCode.DeleteSelectedFiles).setControlsVisible(Global.files_multiselection);
    }
}

