package Visual_DVM_2021.Passes.All;
import Common.Utils.Utils_;
import _VisualDVM.Current;
import _VisualDVM.GlobalData.GlobalDatabase;
import _VisualDVM.Global;
import _VisualDVM.Visual.UI;
import Common.Visual.Windows.Dialog.PercentsForm;
import Common.Visual.Windows.Dialog.SliderNumberForm;
import Common.Visual.Windows.Dialog.SpinnerNumberForm;
import Common.Visual.Windows.Dialog.Text.TextFieldDialog;
import Common.Visual.Windows.Dialog.VDirectoryChooser;
import Common.Visual.Windows.Dialog.VFileChooser;
import _VisualDVM.Utils;
import _VisualDVM.GlobalData.Settings.DBSetting;
import Visual_DVM_2021.Passes.PassCode;
import Common.Passes.Pass;

import javax.swing.*;
import java.io.File;
public class UpdateSetting extends Pass<DBSetting> {
    String NewValue;
    VDirectoryChooser directoryChooser = new VDirectoryChooser("");
    VFileChooser fileChooser = new VFileChooser("", "exe");
    @Override
    protected boolean canStart(Object... args) throws Exception {
        target = (Global.mainModule.getDb()).settings.get(args[0]);
        NewValue = target.Value;
        if (args.length == 1) {
            //интерфейсный режим. получение по клику на пункт меню.
            switch (target.settingType) {
                case SapforFlag:
                    NewValue = target.toBoolean() ? "0" : "1";
                    break;
                case PercentField:
                    PercentsForm f = new PercentsForm();
                    if (f.ShowDialog(target.Name.getDescription(), target.toInt32()))
                        NewValue = String.valueOf(f.Result);
                    break;
                case StringField:
                    switch (target.Name) {
                        case Workspace:
                            directoryChooser.setTitle(target.Name.getDescription());
                            directoryChooser.SetCurrentDirectory(
                                    target.Value.isEmpty() ? Global.ProjectsDirectory : new File(target.Value));
                            File dir = directoryChooser.ShowDialog();
                            if (dir != null)
                                NewValue = dir.getAbsolutePath();
                            break;
                        case LocalMakePathWindows:
                            fileChooser.setTitle(target.Name.getDescription());
                            File file = fileChooser.ShowDialog();
                            if (file != null)
                                NewValue = file.getAbsolutePath();
                            break;
                        default:
                            TextFieldDialog ff = new TextFieldDialog();
                            if (ff.ShowDialog(target.Name.getDescription(), target.Value)) {
                                NewValue = ff.Result;
                                if (NewValue.length() == 0)
                                    NewValue = " ";
                            }
                            break;
                    }
                    break;
                case IntField:
                    int min = 0;
                    int max = 0;
                    switch (target.Name) {
                        case LastOpenedProjectsCount:
                            SpinnerNumberForm f_ = new SpinnerNumberForm() {
                                @Override
                                public void InitFields() {
                                    fields.setModel(new SpinnerNumberModel(
                                            target.toInt32(),
                                            1,
                                            50,
                                            1));
                                }
                            };
                            if (f_.ShowDialog(target.Name.getDescription()))
                                NewValue = String.valueOf(f_.Result);
                            break;
                        case BugReportsAgeLimit:
                            min = 1;
                            max = 12;
                            SliderNumberForm fffff = new SliderNumberForm();
                            if (fffff.ShowDialog(target.Name.getDescription(), target.toInt32(), min, max))
                                NewValue = String.valueOf(fffff.Result);
                            break;
                        case FastAccessPassesCount:
                            min = 5;
                            max = 15;
                            SliderNumberForm fff = new SliderNumberForm();
                            if (fff.ShowDialog(target.Name.getDescription(), target.toInt32(), min, max))
                                NewValue = String.valueOf(fff.Result);
                            break;
                        case Kernels:
                            min = 1;
                            max = Utils.getMaxKernels();
                            SliderNumberForm ffff = new SliderNumberForm();
                            if (ffff.ShowDialog(target.Name.getDescription(), target.toInt32(), min, max))
                                NewValue = String.valueOf(ffff.Result);
                            break;
                    }

                    break;
            }
        } else {
            NewValue = args[1].toString();
        }
        //программный, тихий режим.}
        return !NewValue.equals(target.Value);
    }
    @Override
    protected void body() throws Exception {
        target.Value = NewValue;
        Global.mainModule.getDb().Update(target);
    }
    @Override
    protected void showDone() throws Exception {
        if (target.Visible) {
            target.Mark();
            switch (target.Name) {
                case GCOVLimit:
                    if (Global.mainModule.HasFile())
                        Global.mainModule.getFile().form.ShowGCOV();
                    break;
                case FastAccessPassesCount:
                    if (Global.mainModule.HasProject())
                        UI.fastAccessMenuBar.Refresh();
                    break;
                case ShowFullArraysDeclarations:
                    if (Global.mainModule.HasProject())
                        Global.mainModule.getProject().declaratedArrays.ShowUI();
                    break;
                case ShowFullTabsNames:
                    UI.getMainWindow().getTestingWindow().RefreshTabsNames();
                    if (Global.mainModule.HasProject())
                        UI.getMainWindow().getProjectWindow().RefreshTabsNames();
                    if (Global.mainModule.HasFile())
                        Global.mainModule.getFile().form.RefreshTabsNames();
                    break;
                case MPI_PROGRAM:
                    Global.mainModule.getPass(PassCode.CleanAnalyses).Do();
                    ((SPF_GetArrayDistributionOnlyAnalysis)
                            Global.mainModule.getPass(PassCode.SPF_GetArrayDistributionOnlyAnalysis)).RefreshControls();
                    break;
                case SmallScreen:
                    boolean small = target.toBoolean();
                    if (Global.mainModule.HasProject())
                        UI.getMainWindow().getProjectWindow().SwitchScreen(small);
                    UI.getMainWindow().getCallbackWindow().SwitchScreen(small);
                    break;
                case Precompilation:
                    Global.mainModule.getSapfor().ResetAllAnalyses();
                    break;
            }
        }
    }
}
