package _VisualDVM.Visual.Windows;
import Common.Utils.Utils_;
import Common.Visual.Editor.BaseEditor;
import Common.Visual.Editor.Viewer;
import Common.Visual.FormWithSplitters;
import Common.Visual.Menus.VisualiserMenuBar;
import Common.Visual.TextField.StyledTextField;
import Common.Visual.UI;
import _VisualDVM.ComponentsServer.BugReport.BugReport;
import _VisualDVM.ComponentsServer.BugReport.BugReportState;
import _VisualDVM.ComponentsServer.BugReport.UI.BugReportsForm;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Visual.Interface.CallbackWindow;
import _VisualDVM.Visual.Interface.CommentInterface;
import _VisualDVM.Visual.Interface.DescriptionInterface;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class CallbackForm implements FormWithSplitters, CallbackWindow {
    private final JScrollPane bugCommentScroll;
    private final JScrollPane bugReportCommentAdditionScroll;
    private final BaseEditor BugReportComment;
    private final BaseEditor BugReportCommentAddition;
    //-------------------------------------------
    private final JScrollPane bugDescriptionScroll;
    private final JScrollPane bugReportDescriptionAdditionScroll;
    private final BaseEditor BugReportDescription;
    private final BaseEditor BugReportDescriptionAddition;
    public JSplitPane SC10;
    public DescriptionInterface descriptionInterface;
    public CommentInterface commentInterface;
    public JSplitPane SC6;
    public JSplitPane SC91;
    DocumentListener descriptionAdditionListener = new DocumentListener() {
        @Override
        public void removeUpdate(DocumentEvent e) {
        }
        @Override
        public void insertUpdate(DocumentEvent e) {
        }
        @Override
        public void changedUpdate(DocumentEvent e) {
            if (!Global.componentsServer.db.bugReports.getUI().getCurrent().state.equals(BugReportState.draft))
                Global.componentsServer.db.bugReports.getUI().getCurrent().descriptionAdditionDraft =
                        BugReportDescriptionAddition.getText();
        }
    };
    DocumentListener commentAdditionListener = new DocumentListener() {
        @Override
        public void removeUpdate(DocumentEvent e) {
        }
        @Override
        public void insertUpdate(DocumentEvent e) {
        }
        @Override
        public void changedUpdate(DocumentEvent e) {
            if (!Global.componentsServer.db.bugReports.getUI().getCurrent().state.equals(BugReportState.draft))
                Global.componentsServer.db.bugReports.getUI().getCurrent().commentAdditionDraft = BugReportCommentAddition.getText();
        }
    };
    DocumentListener descriptionListener = new DocumentListener() {
        @Override
        public void removeUpdate(DocumentEvent e) {
        }
        @Override
        public void insertUpdate(DocumentEvent e) {
        }
        @Override
        public void changedUpdate(DocumentEvent e) {
            if (Global.componentsServer.db.bugReports.getUI().getCurrent().state.equals(BugReportState.draft))
                Global.componentsServer.db.bugReports.getUI().getCurrent().description =
                        BugReportDescription.getText();
            Global.componentsServer.db.bugReports.getUI().RedrawControl();
        }
    };
    DocumentListener commentListener = new DocumentListener() {
        @Override
        public void removeUpdate(DocumentEvent e) {
        }
        @Override
        public void insertUpdate(DocumentEvent e) {
        }
        @Override
        public void changedUpdate(DocumentEvent e) {
            if (Global.componentsServer.db.bugReports.getUI().getCurrent().state.equals(BugReportState.draft))
                Global.componentsServer.db.bugReports.getUI().getCurrent().comment = BugReportComment.getText();
        }
    };
    //-------------------------------------------
    DescriptionTabs descriptionTabs = new DescriptionTabs();
    DescriptionFields descriptionFields = new DescriptionFields();
    CommentTabs commentTabs = new CommentTabs();
    CommentFields commentFields = new CommentFields();
    private JPanel content;
    //-
    private JToolBar accountTools;
    private JLabel lAccountName;
    private JLabel lAccountMail;
    private JLabel lAccountRole;
    private JPanel accountPanel;
    private JPanel bugReportsPanel;
    private JPanel filterFormPanel;
    private JPanel recipientsPanel;
    private JTabbedPane currentBugReportTabs;
    private JScrollPane bugSettingsScroll;
    private JCheckBox BugReportsMyOnlyFilter;
    private JCheckBox BugReportsOpenedOnly;
    private JToolBar bugsTools;
    private JToolBar settingsTools;
    private JToolBar descriptionTools;
    private JToolBar commentTools;
    private JToolBar descriptionAdditionTools;
    private JToolBar commentAdditionTools;
    private JPanel descriptionPanel;
    private JPanel commentPanel;
    private JSplitPane SCX;
    private JTextField BugReportsKeyFilter;
    private JTextField BugReportsSenderNameFilter;
    private JTextField BugReportsDescriptionFilter;
    private JTextField BugReportsVersionFilter;
    private JTextField BugReportsExecutorFilter;
    private JTextField BugReportsCommentFilter;
    private JPanel workspacesPanel;
    private JLabel adminLabel;
    private JPanel bugRepotsFilesPanel;
    private Viewer BugReportSettings;
    public CallbackForm() {
        LoadSplitters();
        //---------------------------------
        //чтобы не было индусятины, лучше создать контролы здесь
        BugReportDescription = new BaseEditor();
        BugReportDescription.setLineWrap(true);
        bugDescriptionScroll = new JScrollPane(BugReportDescription);
        //----------------------------------------------
        BugReportDescriptionAddition = new BaseEditor();
        BugReportDescriptionAddition.setLineWrap(true);
        bugReportDescriptionAdditionScroll = new JScrollPane(BugReportDescriptionAddition);
        BugReportDescription.setWrapStyleWord(true);
        BugReportDescriptionAddition.setWrapStyleWord(true);
        //---------------------------------------------
        //-
        BugReportComment = new BaseEditor();
        BugReportComment.setLineWrap(true);
        bugCommentScroll = new JScrollPane(BugReportComment);
        //-
        BugReportCommentAddition = new BaseEditor();
        BugReportCommentAddition.setLineWrap(true);
        bugReportCommentAdditionScroll = new JScrollPane(BugReportCommentAddition);
        BugReportComment.setWrapStyleWord(true);
        BugReportCommentAddition.setWrapStyleWord(true);
        //----------------------------------------------
        //тут развилка на то вкладки или поля.
        SwitchScreen(Global.normalProperties.SmallScreen);
        //-
        Global.componentsServer.db.bugReports.mountUI(bugReportsPanel);
        Global.componentsServer.db.bugReportsFiles.mountUI(bugRepotsFilesPanel);
        Global.componentsServer.db.recipients.mountUI(recipientsPanel);
        //-
        if (!Global.mainModule.getPass(PassCode.CheckAccount).isDone()) {
            accountTools.add(Global.mainModule.getPass(PassCode.CheckAccount).createButton());
        }
        //--
        BugReportsKeyFilter.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                BugReportsForm.filterKey = BugReportsKeyFilter.getText();
                ShowBugReports();
            }
            @Override
            public void removeUpdate(DocumentEvent e) {
                BugReportsForm.filterKey = BugReportsKeyFilter.getText();
                ShowBugReports();
            }
            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });
        BugReportsSenderNameFilter.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                BugReportsForm.filterSenderName = BugReportsSenderNameFilter.getText();
                ShowBugReports();
            }
            @Override
            public void removeUpdate(DocumentEvent e) {
                BugReportsForm.filterSenderName = BugReportsSenderNameFilter.getText();
                ShowBugReports();
            }
            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });
        BugReportsDescriptionFilter.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                BugReportsForm.filterDescription = BugReportsDescriptionFilter.getText();
                ShowBugReports();
            }
            @Override
            public void removeUpdate(DocumentEvent e) {
                BugReportsForm.filterDescription = BugReportsDescriptionFilter.getText();
                ShowBugReports();
            }
            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });
        //------->>>
        BugReportsCommentFilter.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                BugReportsForm.filterComment = BugReportsCommentFilter.getText();
                ShowBugReports();
            }
            @Override
            public void removeUpdate(DocumentEvent e) {
                BugReportsForm.filterComment = BugReportsCommentFilter.getText();
                ShowBugReports();
            }
            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });
        BugReportsExecutorFilter.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                BugReportsForm.filterExecutor = BugReportsExecutorFilter.getText();
                ShowBugReports();
            }
            @Override
            public void removeUpdate(DocumentEvent e) {
                BugReportsForm.filterExecutor = BugReportsExecutorFilter.getText();
                ShowBugReports();
            }
            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });
        BugReportsVersionFilter.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                BugReportsForm.filterVersion = BugReportsVersionFilter.getText();
                ShowBugReports();
            }
            @Override
            public void removeUpdate(DocumentEvent e) {
                BugReportsForm.filterVersion = BugReportsVersionFilter.getText();
                ShowBugReports();
            }
            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });
        //--------
        BugReportsMyOnlyFilter.addActionListener(e -> {
            BugReportsForm.filterMyOnly = BugReportsMyOnlyFilter.isSelected();
            ShowBugReports();
        });
        BugReportsOpenedOnly.addActionListener(e -> {
            BugReportsForm.filterOpenedOnly = BugReportsOpenedOnly.isSelected();
            ShowBugReports();
        });
        //-
        accountTools.addSeparator();
        accountTools.add(Global.mainModule.getPass(PassCode.StartComponentsServer).createButton());
        accountTools.add(Global.mainModule.getPass(PassCode.ShutdownComponentsServer).createButton());
        accountTools.add(Global.mainModule.getPass(PassCode.PublishComponentsServer).createButton());
        accountTools.add(Global.mainModule.getPass(PassCode.ComponentsServerBackUp).createButton());
    }
    public void LockMyOnly() {
        if (!BugReportsMyOnlyFilter.isSelected()) {
            BugReportsMyOnlyFilter.doClick();
        }
        BugReportsMyOnlyFilter.setEnabled(false);
    }
    public void UnlockMyOnly() {
        BugReportsMyOnlyFilter.setEnabled(true);
        if (BugReportsMyOnlyFilter.isSelected()) {
            BugReportsMyOnlyFilter.doClick();
        }
    }
    @Override
    public void SwitchScreen(boolean small) {
        UI.Clear(descriptionPanel);
        //------------------------------------------------------------------
        descriptionInterface = small ? descriptionTabs : descriptionFields;
        descriptionInterface.setEditorScroll(bugDescriptionScroll);
        descriptionInterface.setAdditionScroll(bugReportDescriptionAdditionScroll);
        descriptionPanel.add(descriptionInterface.getContent());
        //----------
        descriptionPanel.revalidate();
        descriptionPanel.repaint();
        //---------
        UI.Clear(commentPanel);
        //------------------------------------------------------------------
        commentInterface = small ? commentTabs : commentFields;
        commentInterface.setEditorScroll(bugCommentScroll);
        commentInterface.setAdditionScroll(bugReportCommentAdditionScroll);
        commentPanel.add(commentInterface.getContent());
        //----------
        commentPanel.revalidate();
        commentPanel.repaint();
    }
    @Override
    public void SaveSplitters() {
        FormWithSplitters.super.SaveSplitters();
        descriptionInterface.SaveSplitters();
        commentInterface.SaveSplitters();
    }
    @Override
    public String getBugReportDescriptionText() {
        return BugReportDescription.getText();
    }
    @Override
    public String getBugReportDescriptionAdditionText() {
        return BugReportDescriptionAddition.getText();
    }
    @Override
    public String getBugReportCommentText() {
        return BugReportComment.getText();
    }
    @Override
    public String getBugReportCommentAdditionText() {
        return BugReportCommentAddition.getText();
    }
    @Override
    public void ClearBugReportDescriptionAdditionText() {
        BugReportDescriptionAddition.setText("");
    }
    @Override
    public void ClearBugReportCommentAdditionText() {
        BugReportCommentAddition.setText("");
    }
    @Override
    public void FocusRecipients() {
        currentBugReportTabs.setSelectedIndex(2);
    }
    public void SwitchListeners(boolean on) {
        if (on) {
            BugReportDescriptionAddition.getDocument().addDocumentListener(descriptionAdditionListener);
            BugReportCommentAddition.getDocument().addDocumentListener(commentAdditionListener);
            BugReportDescription.getDocument().addDocumentListener(descriptionListener);
            BugReportComment.getDocument().addDocumentListener(commentListener);
        } else {
            BugReportDescriptionAddition.getDocument().removeDocumentListener(descriptionAdditionListener);
            BugReportCommentAddition.getDocument().removeDocumentListener(commentAdditionListener);
            BugReportDescription.getDocument().removeDocumentListener(descriptionListener);
            BugReportComment.getDocument().removeDocumentListener(commentListener);
        }
    }
    private void createUIComponents() {
        BugReportSettings = new Viewer();
        bugSettingsScroll = new JScrollPane(BugReportSettings);
        //--
        BugReportsKeyFilter = new StyledTextField();
        BugReportsSenderNameFilter = new StyledTextField();
        BugReportsDescriptionFilter = new StyledTextField();
        BugReportsCommentFilter = new StyledTextField();
        BugReportsExecutorFilter = new StyledTextField();
        BugReportsVersionFilter = new StyledTextField();
        settingsTools = new VisualiserMenuBar();
    }
    public void EnableBugreports() {
        UI.Clear(accountPanel);
        accountPanel.add(SC10);
    }
    @Override
    public void showNoBugReports() {
        UI.Clear(accountPanel);
        accountPanel.add(new CallbackWelcomeForm().content);
        adminLabel.setVisible(false);
    }
    @Override
    public void showMyOnlyBugReports() {
        EnableBugreports();
        LockMyOnly();
        adminLabel.setVisible(false);
    }
    //возможно перезапуск серверов перетащить во вкладку администрирование? вместе с бекапами. надо подумать.
    @Override
    public void showAllBugreports() {
        EnableBugreports();
        UnlockMyOnly();
        adminLabel.setVisible(false);
    }
    @Override
    public void showAllBugReportsAdmin() {
        EnableBugreports();
        UnlockMyOnly();
        adminLabel.setVisible(true);
    }
    public void ShowAccountInfo() {
        lAccountName.setText(Utils_.Brackets(Global.mainModule.getAccount().name));
        lAccountMail.setText(Utils_.Brackets(Global.mainModule.getAccount().email));
        lAccountRole.setText(Utils_.Brackets(Global.mainModule.getAccount().role.getDescription()));
    }
    @Override
    public void ShowBugReports() {
        Global.componentsServer.db.bugReports.ShowUI();
        Global.componentsServer.db.recipients.ShowUI();
    }
    @Override
    public void ShowNoBugReports() {
        Global.componentsServer.db.bugReports.ClearUI();
    }
    private void ShowDraft() {
        Global.mainModule.setPassesControlsVisible(true,
                PassCode.PublishBugReport,
                PassCode.SaveBugReportRecipients,
                PassCode.SaveBugReportExecutor,
                PassCode.DeleteBugReport
        );
        Global.mainModule.setPassesControlsVisible(false,
                PassCode.AppendBugReportDescription,
                PassCode.AppendBugReportComment,
                PassCode.SaveBugReportDescription,
                PassCode.SaveBugReportComment,
                PassCode.OpenBugReport,
                PassCode.CloseBugReport,
                PassCode.OpenBugReportTestProject,
                PassCode.UpdateBugReportProgress
        );
    }
    private void ShowUserExecutor() {
        Global.mainModule.setPassesControlsVisible(true,
                PassCode.AppendBugReportComment,
                PassCode.UpdateBugReportProgress,
                PassCode.OpenBugReportTestProject
        );
        Global.mainModule.setPassesControlsVisible(false,
                PassCode.PublishBugReport,
                PassCode.SaveBugReportRecipients,
                PassCode.SaveBugReportExecutor,
                PassCode.SaveBugReportDescription,
                PassCode.SaveBugReportComment,
                PassCode.AppendBugReportDescription,
                PassCode.OpenBugReport,
                PassCode.CloseBugReport,
                PassCode.DeleteBugReport
        );
    }
    private void ShowSender() {
        Global.mainModule.setPassesControlsVisible(true,
                PassCode.AppendBugReportComment,
                PassCode.UpdateBugReportProgress,
                PassCode.OpenBugReportTestProject,
                PassCode.SaveBugReportRecipients,
                PassCode.SaveBugReportExecutor,
                PassCode.SaveBugReportDescription,
                PassCode.SaveBugReportComment,
                PassCode.AppendBugReportDescription,
                PassCode.OpenBugReport,
                PassCode.CloseBugReport,
                PassCode.DeleteBugReport
        );
        Global.mainModule.setPassesControlsVisible(false,
                PassCode.PublishBugReport
        );
    }
    private void ShowUser() {
        Global.mainModule.setPassesControlsVisible(false,
                PassCode.PublishBugReport,
                PassCode.SaveBugReportRecipients,
                PassCode.SaveBugReportExecutor,
                PassCode.SaveBugReportDescription,
                PassCode.SaveBugReportComment,
                PassCode.AppendBugReportDescription,
                PassCode.OpenBugReport,
                PassCode.CloseBugReport,
                PassCode.DeleteBugReport,
                PassCode.AppendBugReportComment,
                PassCode.UpdateBugReportProgress,
                PassCode.OpenBugReportTestProject
        );
    }
    @Override
    public void ShowCurrentBugReport() {
        SwitchListeners(false);
        BugReport target = Global.componentsServer.db.bugReports.getUI().getCurrent();
        target.CheckRecipients();
        Global.componentsServer.db.recipients.ShowUI();
        //-
        BugReportDescription.setText(target.description);
        BugReportDescription.ShowBegin();
        BugReportDescriptionAddition.setText(target.descriptionAdditionDraft);
        //-
        BugReportComment.setText(target.comment);
        BugReportComment.ShowBegin();
        BugReportCommentAddition.setText(target.commentAdditionDraft);
        BugReportSettings.setText(target.getSettingsSummary());
        //
        if (target.state.equals(BugReportState.draft)) {
            ShowDraft();
        } else {
            switch (Global.mainModule.getAccount().role) {
                case User:
                    if (Global.mainModule.getAccount().email.equalsIgnoreCase(target.sender_address)) {
                        ShowSender();  //отправитель.может делать с багом все.
                    } else {
                        if (Global.mainModule.getAccount().email.equalsIgnoreCase(target.executor_address)) {
                            ShowUserExecutor(); //исполнитель. может менять прогресс и дополнять комментарий.
                        } else ShowUser(); //недоступно ничего. и баг ему невидим. но на всякий случай.
                    }
                    break;
                case Student:
                case Developer:
                case Admin:
                    ShowSender();
                    break;
            }
        }
        SwitchListeners(true);
    }
    @Override
    public void ShowNoCurrentBugReport() {
        SwitchListeners(false);
        Global.componentsServer.db.recipients.ShowUI();
        //-
        BugReportDescription.setText("");
        BugReportComment.setText("");
        BugReportDescriptionAddition.setText("");
        BugReportCommentAddition.setText("");
        BugReportSettings.setText("");
        //-
        Global.mainModule.setPassesControlsVisible(false, PassCode.SaveBugReportExecutor,
                PassCode.SaveBugReportRecipients,
                PassCode.AppendBugReportDescription,
                PassCode.SaveBugReportDescription,
                PassCode.SaveBugReportComment,
                PassCode.AppendBugReportComment,
                PassCode.PublishBugReport,
                PassCode.OpenBugReport,
                PassCode.CloseBugReport,
                PassCode.OpenBugReportTestProject,
                PassCode.UpdateBugReportProgress,
                PassCode.DeleteBugReport
        );
    }
    @Override
    public void ShowSubscription() {
    }
    @Override
    public JPanel getContent() {
        return content;
    }
}
