package _VisualDVM.Passes.All;
import Common.MainModule_;
import Common.Passes.Pass;
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.Global;
import _VisualDVM.NormalProperties;
import _VisualDVM.Utils;

import javax.swing.*;
import java.io.File;
import java.lang.reflect.Field;
public class UpdateProperty extends Pass<Object> {
    String name = "";
    Field field = null;
    Object oldValue = null;
    Object newValue = null;
    @Override
    protected boolean canStart(Object... args) throws Exception {
        name = (String) args[0];
        String description = Global.normalProperties.getFieldDescription(name);
        field = NormalProperties.class.getField(name);
        oldValue = field.get(Global.normalProperties);
        newValue = null;
        SliderNumberForm sliderNumberForm = new SliderNumberForm();
        VDirectoryChooser directoryChooser = new VDirectoryChooser("");
        VFileChooser fileChooser = new VFileChooser("", "exe");
        //-
        if (args.length == 1) {
            if (oldValue instanceof Boolean) {
                newValue = !(Boolean) oldValue;
                return true;
            }
            switch (name) {
                case "Kernels":
                    if (sliderNumberForm.ShowDialog(description, oldValue, 1, Utils.getMaxKernels()))
                        newValue = sliderNumberForm.Result;
                    break;
                case "LocalMakePathWindows":
                    fileChooser.setTitle(description);
                    File file = fileChooser.ShowDialog();
                    if (file != null)
                        newValue = file.getAbsolutePath();
                    break;
                case "PrecompilationFlags":
                    TextFieldDialog textFieldDialog = new TextFieldDialog();
                    if (textFieldDialog.ShowDialog(description, oldValue)) {
                        newValue = textFieldDialog.Result;
                        if (newValue.toString().isEmpty())
                            newValue = " ";
                    }
                    break;
                case "BugReportsAgeLimit":
                    if (sliderNumberForm.ShowDialog(description, oldValue, 1, 12))
                        newValue = sliderNumberForm.Result;
                    break;
                case "FastAccessPassesCount":
                    if (sliderNumberForm.ShowDialog(description, oldValue, 5, 15))
                        newValue = sliderNumberForm.Result;
                    break;
                case "LastOpenedProjectsCount":
                    SpinnerNumberForm spinnerNumberForm = new SpinnerNumberForm() {
                        @Override
                        public void InitFields() {
                            fields.setModel(new SpinnerNumberModel(
                                    (int) oldValue,
                                    1,
                                    50,
                                    1));
                        }
                    };
                    if (spinnerNumberForm.ShowDialog(description, oldValue, 1, 50))
                        newValue = spinnerNumberForm.Result;
                    break;
                case "Workspace":
                    directoryChooser.setTitle(description);
                    directoryChooser.SetCurrentDirectory(
                            Global.normalProperties.Workspace.isEmpty() ? Global.ProjectsDirectory :
                                    new File(Global.normalProperties.Workspace));
                    File dir = directoryChooser.ShowDialog();
                    if (dir != null)
                        newValue = dir.getAbsolutePath();
                    break;
            }
        } else
            newValue = args[1];
        //--
        return newValue != null && !newValue.equals(oldValue);
    }
    @Override
    protected void body() throws Exception {
        field.set(Global.normalProperties, newValue);
        Global.normalProperties.Update();
    }
    @Override
    protected void showDone() throws Exception {
        switch (name) {
            case "collapseProjectTrees":
                if (Global.mainModule.HasProject()) {
                    if ((boolean) newValue)
                        Global.mainModule.getUI().getMainWindow().getProjectWindow().CollapseProjectTrees();
                    else Global.mainModule.getUI().getMainWindow().getProjectWindow().ExpandProjectTrees();
                }
                break;
            case "collapseFileGraphs":
                if (Global.mainModule.HasFile()) {
                    if ((boolean) newValue)
                        Global.mainModule.getFile().form.CollapseGraphs();
                    else
                        Global.mainModule.getFile().form.ExpandGraphs();
                }
                break;
            case "collapseFileMessages":
                if (Global.mainModule.HasFile()) {
                    if ((boolean) newValue)
                        Global.mainModule.getFile().form.CollapseMessages();
                    else
                        Global.mainModule.getFile().form.ExpandMessages();
                }
                break;
            case "FastAccessPassesCount":
                MainModule_.instance.getUI().getFastAccessMenuBar().Refresh();
                break;
            case "ShowFullArraysDeclarations":
                if (Global.mainModule.HasProject())
                    Global.mainModule.getProject().declaratedArrays.ShowUI();
                break;
            case "ShowFullTabsNames":
                Global.mainModule.getUI().getMainWindow().getTestingWindow().RefreshTabsNames();
                if (Global.mainModule.HasProject())
                    Global.mainModule.getUI().getMainWindow().getProjectWindow().RefreshTabsNames();
                if (Global.mainModule.HasFile())
                    Global.mainModule.getFile().form.RefreshTabsNames();
                break;
            case "SmallScreen":
                if (Global.mainModule.HasProject())
                    Global.mainModule.getUI().getMainWindow().getProjectWindow().SwitchScreen(
                            Global.normalProperties.SmallScreen);
                Global.mainModule.getUI().getMainWindow().getCallbackWindow().SwitchScreen(Global.normalProperties.SmallScreen);
                break;
        }
    }
}
