package _VisualDVM.Visual;
import Common.Database.Tables.DataSet;
import Common.Utils.Utils_;
import Common.Visual.UI_;
import Common.Visual.Menus.DataMenuBar;
import _VisualDVM.Current;
import _VisualDVM.Global;
import _VisualDVM.Visual.Menus.CredentialsBar.CredentialsBar;
import _VisualDVM.Visual.Menus.FastAccessMenuBar.FastAccessMenuBar;
import _VisualDVM.Visual.Menus.FileMenuBar.FileMenuBar;
import _VisualDVM.Visual.Menus.MainMenuBar.MainMenuBar;
import _VisualDVM.Visual.Menus.MainMenuBar.MainWindow;
import _VisualDVM.Visual.Menus.VariantsMenuBar.VariantsMenuBar;
import _VisualDVM.Visual.Menus.VersionsMenuBar.VersionsMenuBar;
import Common.Visual.Tables.*;
import Common.Visual.Trees.GraphTreeCellRenderer;
import Common.Visual.Trees.SelectionTreeCellRenderer;
import _VisualDVM.Visual.Windows.SearchReplaceForm;
import _VisualDVM.GlobalData.Compiler.CompilersDBTable;
import _VisualDVM.GlobalData.Compiler.CompilersMenuBar;
import _VisualDVM.GlobalData.CompilerEnvironment.UI.CompilerEnvironmentValueEditor;
import _VisualDVM.GlobalData.CompilerEnvironment.UI.CompilerEnvironmentValueRenderer;
import _VisualDVM.GlobalData.CompilerOption.UI.CompilerOptionParameterNameRenderer;
import _VisualDVM.GlobalData.CompilerOption.UI.CompilerOptionParameterValueEditor;
import _VisualDVM.GlobalData.CompilerOption.UI.CompilerOptionParameterValueRenderer;
import _VisualDVM.GlobalData.DVMParameter.DVMParameterDBTable;
import _VisualDVM.GlobalData.DVMParameter.DVMParametersMenuBar;
import _VisualDVM.GlobalData.EnvironmentValue.EnvironmentValuesDBTable;
import _VisualDVM.GlobalData.EnvironmentValue.EnvironmentValuesMenuBar;
import _VisualDVM.GlobalData.GlobalDatabase;
import _VisualDVM.GlobalData.Machine.MachinesDBTable;
import _VisualDVM.GlobalData.Machine.MachinesMenuBar;
import _VisualDVM.GlobalData.Makefile.MakefilesDBTable;
import _VisualDVM.GlobalData.Makefile.MakefilesMenuBar;
import _VisualDVM.GlobalData.Module.ModulesDBTable;
import _VisualDVM.GlobalData.Module.ModulesMenuBar;
import _VisualDVM.GlobalData.RemoteFile.UI.RemoteFileChooser;
import _VisualDVM.GlobalData.RemoteFile.UI.RemoteFileRenderer;
import _VisualDVM.GlobalData.RunConfiguration.RunConfigurationsDBTable;
import _VisualDVM.GlobalData.RunConfiguration.RunConfigurationsMenuBar;
import _VisualDVM.GlobalData.SapforProfile.SapforProfilesDBTable;
import _VisualDVM.GlobalData.Settings.SettingName;
import _VisualDVM.GlobalData.Tasks.CompilationTask.CompilationTasksDBTable;
import _VisualDVM.GlobalData.Tasks.RunTask.RunTasksDBTable;
import _VisualDVM.GlobalData.User.UsersDBTable;
import _VisualDVM.GlobalData.User.UsersMenuBar;
import _VisualDVM.ProjectData.DBArray.ArraysDBTable;
import _VisualDVM.ProjectData.Files.UI.FilesTreeCellRenderer;
import _VisualDVM.ProjectData.Project.UI.VersionsTreeCellRenderer;
import _VisualDVM.ProjectData.SapforData.Arrays.ArraysSet;
import _VisualDVM.ProjectData.SapforData.Arrays.UI.DimensionRenderer;
import _VisualDVM.ProjectData.SapforData.Arrays.UI.DimensionStateChanger;
import _VisualDVM.ProjectData.SapforData.Arrays.UI.RulesTreeCellRenderer;
import _VisualDVM.ProjectData.SapforData.Regions.RegionsSet;
import _VisualDVM.ProjectData.SapforData.Variants.UI.VariantRankRenderer;
import _VisualDVM.ProjectData.SapforData.Variants.VariantsSet;
import _VisualDVM.Repository.BugReport.BugReportsDBTable;
import _VisualDVM.Repository.BugReport.BugReportsMenuBar;
import _VisualDVM.Repository.Component.UI.ComponentsForm;
import _VisualDVM.Repository.SubscriberWorkspace.SubscriberWorkspaceDBTable;
import _VisualDVM.Repository.SubscriberWorkspace.SubscriberWorkspacesMenuBar;
import _VisualDVM.Repository.Subscribes.SubscribersMenuBar;
import _VisualDVM.Repository.Subscribes.SubsribersDBTable;
import _VisualDVM.TestingSystem.Common.Group.GroupsDBTable;
import _VisualDVM.TestingSystem.Common.Group.GroupsMenuBar;
import _VisualDVM.TestingSystem.Common.Test.TestDBTable;
import _VisualDVM.TestingSystem.Common.Test.TestsMenuBar;
import _VisualDVM.TestingSystem.Common.TestingBar;
import _VisualDVM.TestingSystem.DVM.DVMConfiguration.DVMConfigurationDBTable;
import _VisualDVM.TestingSystem.DVM.DVMConfiguration.DVMConfigurationsMenuBar;
import _VisualDVM.TestingSystem.DVM.DVMPackage.DVMPackageDBTable;
import _VisualDVM.TestingSystem.DVM.DVMPackage.DVMPackagesBar;
import _VisualDVM.TestingSystem.DVM.DVMSettings.DVMSettingsDBTable;
import _VisualDVM.TestingSystem.DVM.DVMSettings.UI.DVMSettingsBar;
import _VisualDVM.TestingSystem.DVM.DVMTasks.DVMRunTasksBar;
import _VisualDVM.TestingSystem.DVM.DVMTasks.DVMRunTasksSet;
import _VisualDVM.TestingSystem.SAPFOR.SapforConfiguration.SapforConfigurationDBTable;
import _VisualDVM.TestingSystem.SAPFOR.SapforConfiguration.SapforConfigurationsMenuBar;
import _VisualDVM.TestingSystem.SAPFOR.SapforPackage.SapforPackageDBTable;
import _VisualDVM.TestingSystem.SAPFOR.SapforSettings.SapforSettingsBar;
import _VisualDVM.TestingSystem.SAPFOR.SapforSettings.SapforSettingsDBTable;
import _VisualDVM.TestingSystem.SAPFOR.SapforSettingsCommand.SapforSettingsCommandsBar;
import _VisualDVM.TestingSystem.SAPFOR.SapforSettingsCommand.SapforSettingsCommandsDBTable;
import _VisualDVM.TestingSystem.SAPFOR.SapforTasksPackage.UI.SapforPackagesBar;
import _VisualDVM.TestingSystem.SAPFOR.SapforTasksPackage.UI.SapforVersionsTreeCellRenderer;
import _VisualDVM.TestingSystem.SAPFOR.ServerSapfor.ServerSapforsBar;
import _VisualDVM.TestingSystem.SAPFOR.ServerSapfor.ServerSapforsDBTable;
import Visual_DVM_2021.Passes.PassCode;
import _VisualDVM.Visual.Interface.DebugWindow;
import _VisualDVM.Visual.Interface.VersionsWindow;
import _VisualDVM.Visual.Windows.DebugForm;
import _VisualDVM.Visual.Windows.MainForm;
import _VisualDVM.Visual.Windows.ProfilesForm;
import _VisualDVM.Visual.Windows.VersionsForm;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import javax.swing.tree.TreeCellRenderer;
import java.awt.*;
import java.util.LinkedHashMap;

import static Common.Visual.Tables.TableEditors.*;
import static Common.Visual.Tables.TableRenderers.*;
import static Common.Visual.Trees.TreeRenderers.*;
public class UI {
    public static MainMenuBar mainMenuBar = null;
    public static TestingBar testingBar = null;
    public static CredentialsBar credentialsBar = null;
    public static FileMenuBar fileMenuBar = null;
    public static VersionsMenuBar versionsMenuBar = null;
    public static FastAccessMenuBar fastAccessMenuBar = null;
    //ФОРМЫ
    static MainWindow mainWindow = null;
    static ComponentsForm componentsWindow = null;
    static SearchReplaceForm searchReplaceWindow = null;
    static ProfilesForm profilesWindow = null;
    static VersionsWindow versionsWindow = null;
    static DebugWindow debugWindow = null;
    static RemoteFileChooser remoteFileChooser = null;
    //------------
    public static final Highlighter.HighlightPainter GoodLoopPainter = new DefaultHighlighter.DefaultHighlightPainter(new Color(152, 251, 152, 90));
    public static final Highlighter.HighlightPainter BadLoopPainter = new DefaultHighlighter.DefaultHighlightPainter(new Color(240, 128, 128, 90));
    public static LinkedHashMap<Common.Visual.Tables.TableRenderers, TableCellRenderer> TableRenderers = new LinkedHashMap<>();
    public static LinkedHashMap<Common.Visual.Tables.TableEditors, TableCellEditor> TableEditors = new LinkedHashMap<>();
    public static LinkedHashMap<Common.Visual.Trees.TreeRenderers, TreeCellRenderer> TreeRenderers = new LinkedHashMap<>();
    public static boolean HasMainWindow() {
        return mainWindow != null;
    }
    public static void CreateMenus() {
        mainMenuBar = new MainMenuBar();
        testingBar = new TestingBar();
        credentialsBar = new CredentialsBar();
        versionsMenuBar = new VersionsMenuBar();
        fastAccessMenuBar = new FastAccessMenuBar();
        //---------------------------------------------------->>
        UI_.menuBars.put(BugReportsDBTable.class, new BugReportsMenuBar());
        UI_.menuBars.put(SubscriberWorkspaceDBTable.class, new SubscriberWorkspacesMenuBar());
        //---------------------------------------------------->>
        UI_.menuBars.put(GroupsDBTable.class, new GroupsMenuBar());
        UI_.menuBars.put(TestDBTable.class, new TestsMenuBar());
        UI_.menuBars.put(DVMConfigurationDBTable.class, new DVMConfigurationsMenuBar());
        UI_.menuBars.put(DVMPackageDBTable.class, new DVMPackagesBar());
        UI_.menuBars.put(SapforPackageDBTable.class, new SapforPackagesBar());
        UI_.menuBars.put(DVMRunTasksSet.class, new DVMRunTasksBar());
        //--->>>
        UI_.menuBars.put(MachinesDBTable.class, new MachinesMenuBar());
        UI_.menuBars.put(UsersDBTable.class, new UsersMenuBar());
        UI_.menuBars.put(CompilersDBTable.class, new CompilersMenuBar());
        UI_.menuBars.put(MakefilesDBTable.class, new MakefilesMenuBar());
        UI_.menuBars.put(ModulesDBTable.class, new ModulesMenuBar());
        UI_.menuBars.put(RunConfigurationsDBTable.class, new RunConfigurationsMenuBar());
        UI_.menuBars.put(EnvironmentValuesDBTable.class, new EnvironmentValuesMenuBar());
        UI_.menuBars.put(DVMParameterDBTable.class, new DVMParametersMenuBar());
        UI_.menuBars.put(CompilationTasksDBTable.class, new DataMenuBar("задачи на компиляцию"));
        UI_.menuBars.put(RunTasksDBTable.class, new DataMenuBar("задачи на запуск"));
        //---->>
        UI_.menuBars.put(SapforConfigurationDBTable.class, new SapforConfigurationsMenuBar());
        //---->>
        UI_.menuBars.put(RegionsSet.class, new DataMenuBar("области распараллеливания"));
        UI_.menuBars.put(ArraysSet.class, new DataMenuBar("массивы"));
        UI_.menuBars.put(ArraysDBTable.class, new DataMenuBar("сохранённые состояния") {
            @Override
            public void createSelectionButtons(DataSet dataSet) {
                //не нужны.
            }
        });
        UI_.menuBars.put(VariantsSet.class, new VariantsMenuBar());
        UI_.menuBars.put(SubsribersDBTable.class, new SubscribersMenuBar());
        UI_.menuBars.put(SapforProfilesDBTable.class, new DataMenuBar("профили", PassCode.SaveProfile, PassCode.EditProfile, PassCode.ApplyProfile, PassCode.DeleteProfile));
        UI_.menuBars.put(ServerSapforsDBTable.class, new ServerSapforsBar());
        UI_.menuBars.put(SapforSettingsDBTable.class, new SapforSettingsBar());
        UI_.menuBars.put(SapforSettingsCommandsDBTable.class, new SapforSettingsCommandsBar());
        UI_.menuBars.put(DVMSettingsDBTable.class, new DVMSettingsBar());
        //---->>
    }
    public static void CreateWindows() {
        searchReplaceWindow = new SearchReplaceForm();
        remoteFileChooser = new RemoteFileChooser();
        profilesWindow = new ProfilesForm();
        mainWindow = new MainForm();
        //---------------------------------------
        mainWindow.Show();
    }
    //-
    public static void CreateAll() {
        //<editor-fold desc="Локализация компонентов окна JFileChooser">
        UIManager.put("FileChooser.openButtonText", "Открыть");
        UIManager.put("FileChooser.cancelButtonText", "Отмена");
        UIManager.put("FileChooser.lookInLabelText", "Смотреть в");
        UIManager.put("FileChooser.fileNameLabelText", "Имя файла");
        UIManager.put("FileChooser.filesOfTypeLabelText", "Тип файла");
        UIManager.put("FileChooser.saveButtonText", "Сохранить");
        UIManager.put("FileChooser.saveButtonToolTipText", "Сохранить");
        UIManager.put("FileChooser.openButtonToolTipText", "Открыть");
        UIManager.put("FileChooser.cancelButtonText", "Отмена");
        UIManager.put("FileChooser.cancelButtonToolTipText", "Отмена");
        UIManager.put("FileChooser.lookInLabelText", "Папка");
        UIManager.put("FileChooser.saveInLabelText", "Папка");
        UIManager.put("FileChooser.fileNameLabelText", "Имя файла");
        UIManager.put("FileChooser.filesOfTypeLabelText", "Тип файлов");
        UIManager.put("FileChooser.upFolderToolTipText", "На один уровень вверх");
        UIManager.put("FileChooser.newFolderToolTipText", "Создание новой папки");
        UIManager.put("FileChooser.listViewButtonToolTipText", "Список");
        UIManager.put("FileChooser.detailsViewButtonToolTipText", "Таблица");
        UIManager.put("FileChooser.fileNameHeaderText", "Имя");
        UIManager.put("FileChooser.fileSizeHeaderText", "Размер");
        UIManager.put("FileChooser.fileTypeHeaderText", "Тип");
        UIManager.put("FileChooser.fileDateHeaderText", "Изменен");
        UIManager.put("FileChooser.fileAttrHeaderText", "Атрибуты");
        UIManager.put("FileChooser.acceptAllFileFilterText", "Все файлы");
        //</editor-fold>
        //<editor-fold desc="Локализация компонентов окна подтверждения">
        UIManager.put("OptionPane.yesButtonText", "Да");
        UIManager.put("OptionPane.noButtonText", "Нет");
        UIManager.put("OptionPane.cancelButtonText", "Отмена");
        UIManager.put("OptionPane.okButtonText", "Готово");
        //</editor-fold>
        //</editor-fold>
        //<editor-fold desc="Объекты отрисовки и редактирования деревьев и таблиц">
        TableRenderers.put(RendererDate, new DateRenderer_());
        TableRenderers.put(RendererProgress, new ProgressBarRenderer());
        TableRenderers.put(RendererSelect, new DBObjectSelectionRenderer());
        TableRenderers.put(RendererDimension, new DimensionRenderer());
        TableRenderers.put(RendererMultiline, new MultilineRenderer());
        TableRenderers.put(RendererHyperlinks, new HyperlinksRenderer());
        TableRenderers.put(RendererTopLeft, new TopLeftRenderer());
        TableRenderers.put(RendererMaskedInt, new MaskedIntegerValueRenderer());
        TableRenderers.put(RendererVariantRank, new VariantRankRenderer());
        TableRenderers.put(RendererHiddenList, new HiddenListRenderer());
        TableRenderers.put(RendererWrapText, new WrapTextRenderer());
        TableRenderers.put(RendererCompilerOptionParameterValue, new CompilerOptionParameterValueRenderer());
        TableRenderers.put(RendererCompilerOptionParameterName, new CompilerOptionParameterNameRenderer());
        TableRenderers.put(RendererCompilerEnvironmentValue, new CompilerEnvironmentValueRenderer());
        TableRenderers.put(RendererStatusEnum, new StatusEnumRenderer());
        TableRenderers.put(RendererAutoConfiguration, new ConfigurationAutoRenderer());
        //---------------------------------------------
        TreeRenderers.put(RendererGraph, new GraphTreeCellRenderer());
        TreeRenderers.put(RendererRemoteFile, new RemoteFileRenderer());
        TreeRenderers.put(RendererFile, new FilesTreeCellRenderer());
        TreeRenderers.put(RendererVersion, new VersionsTreeCellRenderer());
        TreeRenderers.put(RendererSapforVersion, new SapforVersionsTreeCellRenderer());
        TreeRenderers.put(RendererRule, new RulesTreeCellRenderer());
        TreeRenderers.put(RendererSelection, new SelectionTreeCellRenderer());
        //----------------------------------------------
        TableEditors.put(EditorSelect, new DBObjectSelector());
        TableEditors.put(EditorHyperlinks, new VectorEditor());
        TableEditors.put(EditorDimension, new DimensionStateChanger());
        TableEditors.put(EditorCompilerOptionParameterValue, new CompilerOptionParameterValueEditor());
        TableEditors.put(EditorCompilerEnvironmentValue, new CompilerEnvironmentValueEditor());
        TableEditors.put(EditorAutoConfiguration, new ConfigurationAutoSwitcher());
        //</editor-fold>
    }
    public static void CreateComponentsForm() {
        componentsWindow = new ComponentsForm();
    }
    //-----
    public static void ShowTabsNames(JTabbedPane tabs) {
        ShowTabsNames(tabs, 0);
    }
    public static void ShowTabsNames(JTabbedPane tabs, int startIndex) {
        boolean flag = Global.mainModule.getDb().settings.get(SettingName.ShowFullTabsNames).toBoolean();
        for (int i = startIndex; i < tabs.getTabCount(); ++i)
            tabs.setTitleAt(i, flag ? tabs.getToolTipTextAt(i) : "");
    }
    public static void CreateVersionsWindow() {
        versionsWindow = new VersionsForm(Global.mainModule.getRoot());
    }
    //-
    public static void ShowSearchForm(boolean replace) {
        searchReplaceWindow.setMode(replace);
        searchReplaceWindow.ShowMode();
        ShowSearchForm();
    }
    public static void HideSearchForm() {
        if (searchReplaceWindow.isVisible())
            searchReplaceWindow.setVisible(false);
    }
    public static void ShowSearchForm() {
        if (searchReplaceWindow.isVisible())
            searchReplaceWindow.Refresh();
        else
            searchReplaceWindow.Show();
    }
    public static void ShowComponentsWindow() {
        componentsWindow.ShowDialog("");
    }
    public static void ShowProfilesWindow() {
        profilesWindow.ShowDialog("");
    }
    //----
    public static MainWindow getMainWindow(){return mainWindow;}
    public static VersionsWindow getVersionsWindow(){return versionsWindow;}
    public static ComponentsForm getComponentsWindow(){return componentsWindow;}
    public static SearchReplaceForm getSearchReplaceWindow(){return searchReplaceWindow;}
    public static ProfilesForm getProfilesWindow(){return profilesWindow;}
    public static RemoteFileChooser getRemoteFileChooser(){return remoteFileChooser;}
    //возможно перекинуть на тестирование. потом.
    public static DebugWindow getDebugWindow() {
        if (debugWindow == null)
            debugWindow = new DebugForm();
        return debugWindow;
    }
}
