package _VisualDVM.Visual.Windows;
import Common.Utils.Pair;
import Common.Visual.TextField.StyledTextField;
import Common.Visual.UI;
import _VisualDVM.Global;
import _VisualDVM.Passes.All.SPF_GetGraphFunctionPositions;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.ProjectData.SapforData.Functions.UI.Graph.FunctionsGraphForm;
import _VisualDVM.ProjectData.SapforData.Functions.UI.Graph.FunctionsGraphUI;
import _VisualDVM.Visual.Interface.FunctionsWindow;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FunctionsForm implements FunctionsWindow {
    private JPanel content;
    private JButton bFitToScreen;
    private JButton bZoomIn;
    private JButton bZoomOut;
    private JButton bSaveGraphAsImage;
    private JSpinner sIterations;
    private JSpinner sResistance;
    private JSpinner sScreen;
    private JToolBar tools;
    private JTextField filterName;
    private JCheckBox ShowStandardFilter;
    private JCheckBox ShowExternalFilter;
    private JCheckBox ShowUnreachableFilter;
    private JSpinner sDepth;
    private JCheckBox ShowIn;
    private JCheckBox ShowOut;
    private JPanel functionsGraphPanel;
    private JCheckBox cbShowByCurrentFunction;
    private JLabel lCurrentFunction;
    private JToolBar filtersBar;
    private JLabel depthLabel;
    private JButton bSaveCoordinates;
    private JToolBar currentFunctionTools;
    private FunctionsGraphForm functionsGraphForm;
    public FunctionsForm() {
        LoadSplitters();
        functionsGraphForm = new FunctionsGraphForm(functionsGraphPanel);
        bZoomIn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                functionsGraphForm.getControl().zoomIn();
            }
        });
        bZoomOut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                functionsGraphForm.getControl().zoomOut();
            }
        });
        bSaveGraphAsImage.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Global.mainModule.getPass(PassCode.SaveGraph).Do();
            }
        });
        sIterations.setModel(new SpinnerNumberModel(Global.mainModule.getProject().fgIterations,
                100, 5000, 100
        ));
        sResistance.setModel(new SpinnerNumberModel(Global.mainModule.getProject().fgResistance,
                10, 5000, 50
        ));
        sScreen.setModel(new SpinnerNumberModel(Global.mainModule.getProject().fgScreen,
                0.25, 2.0, 0.05
        ));
        sDepth.setModel(new SpinnerNumberModel(1,
                0, 64, 1
        ));
        UI.MakeSpinnerRapid(sIterations, e -> {
            Global.mainModule.getProject().UpdatefgIterations((int) sIterations.getValue());
            FunctionsGraphUI.ffTimer.restart();
        });
        UI.MakeSpinnerRapid(sResistance, e -> {
            Global.mainModule.getProject().UpdatefgResistance((int) sResistance.getValue());
            FunctionsGraphUI.ffTimer.restart();
        });
        UI.MakeSpinnerRapid(sScreen, e -> {
            Global.mainModule.getProject().UpdatefgScreen((double) sScreen.getValue());
            FunctionsGraphUI.ffTimer.restart();
        });
        UI.MakeSpinnerRapid(sDepth, e -> {
            SPF_GetGraphFunctionPositions.depth = (int) sDepth.getValue();
            FunctionsGraphUI.ffTimer.restart();
        });
        //----фильтрация функций
        //<editor-fold desc="Фильтрация функций">
        FunctionsGraphUI.ffTimer.setRepeats(false);
        filterName.setText(SPF_GetGraphFunctionPositions.filterName);
        filterName.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                SPF_GetGraphFunctionPositions.filterName = filterName.getText();
                FunctionsGraphUI.ffTimer.restart();
            }
            @Override
            public void removeUpdate(DocumentEvent e) {
                SPF_GetGraphFunctionPositions.filterName = filterName.getText();
                FunctionsGraphUI.ffTimer.restart();
            }
            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });
        ShowFunctionGraphSettings();
        //-
        ShowStandardFilter.addActionListener(e -> {
            SPF_GetGraphFunctionPositions.showStandardFunctions = ShowStandardFilter.isSelected();
            FunctionsGraphUI.ffTimer.stop();
            Global.mainModule.getPass(PassCode.SPF_GetGraphFunctionPositions).Do();
        });
        ShowExternalFilter.addActionListener(e -> {
            SPF_GetGraphFunctionPositions.showExternalFunctions = ShowExternalFilter.isSelected();
            FunctionsGraphUI.ffTimer.stop();
            Global.mainModule.getPass(PassCode.SPF_GetGraphFunctionPositions).Do();
        });
        ShowUnreachableFilter.addActionListener(e -> {
            SPF_GetGraphFunctionPositions.showUnreachableFunctions = ShowUnreachableFilter.isSelected();
            FunctionsGraphUI.ffTimer.stop();
            Global.mainModule.getPass(PassCode.SPF_GetGraphFunctionPositions).Do();
        });
        cbShowByCurrentFunction.addActionListener(e -> {
            SPF_GetGraphFunctionPositions.showByCurrentFunction = cbShowByCurrentFunction.isSelected();
            FunctionsGraphUI.ffTimer.stop();
            Global.mainModule.getPass(PassCode.SPF_GetGraphFunctionPositions).Do();
        });
        ShowIn.addActionListener(e -> {
            SPF_GetGraphFunctionPositions.showIn = ShowIn.isSelected();
            FunctionsGraphUI.ffTimer.stop();
            Global.mainModule.getPass(PassCode.SPF_GetGraphFunctionPositions).Do();
        });
        ShowOut.addActionListener(e -> {
            SPF_GetGraphFunctionPositions.showOut = ShowOut.isSelected();
            FunctionsGraphUI.ffTimer.stop();
            Global.mainModule.getPass(PassCode.SPF_GetGraphFunctionPositions).Do();
        });
        //---------------------------------------------------------
        filtersBar.add(Global.mainModule.getPass(PassCode.SPF_GetGraphFunctionPositions).createButton(), 0);
        filtersBar.add(Global.mainModule.getPass(PassCode.ApplyCurrentFunction).createButton(), 6);
        filtersBar.setPreferredSize(new Dimension(0, 30));
    }
    @Override
    public JPanel getContent() {
        return content;
    }
    @Override
    public Pair<Integer, Integer> getFunctionsGraphPanelSizes() {
        return new Pair<>(functionsGraphPanel.getWidth(), functionsGraphPanel.getHeight());
    }
    @Override
    public FunctionsGraphForm getFunctionsGraphWindow() {
        return functionsGraphForm;
    }
    @Override
    public void ShowFunctions() {
        functionsGraphForm.Show();
    }
    @Override
    public void ShowNoFunctions() {
        functionsGraphForm.Clear();
    }
    @Override
    public void ShowCurrentFunction() {
        lCurrentFunction.setText(Global.mainModule.getFunction().funcName);
    }
    @Override
    public void ShowNoCurrentFunction() {
        lCurrentFunction.setText("?");
    }
    public void ShowFunctionGraphSettings() {
        ShowStandardFilter.setSelected(SPF_GetGraphFunctionPositions.showStandardFunctions);
        ShowExternalFilter.setSelected(SPF_GetGraphFunctionPositions.showExternalFunctions);
        ShowUnreachableFilter.setSelected(SPF_GetGraphFunctionPositions.showUnreachableFunctions);
        //-
        sIterations.setValue(Global.mainModule.getProject().fgIterations);
        sResistance.setValue(Global.mainModule.getProject().fgResistance);
        sScreen.setValue(Global.mainModule.getProject().fgScreen);
        //-
        cbShowByCurrentFunction.setSelected(SPF_GetGraphFunctionPositions.showByCurrentFunction);
        ShowIn.setSelected(SPF_GetGraphFunctionPositions.showIn);
        ShowOut.setSelected(SPF_GetGraphFunctionPositions.showOut);
        sDepth.setValue(SPF_GetGraphFunctionPositions.depth);
    }
    private void createUIComponents() {
        // TODO: place custom component creation code here
        filterName = new StyledTextField();
    }
}
