package _VisualDVM.ComponentsServer.BugReport.UI;
import Common.Database.Tables.DataSet;
import Common.Passes.PassCode_;
import Common.Visual.DataSetControlForm;
import Common.Visual.Menus.DataMenuBar;
import Common.Visual.Tables.ColumnInfo;
import Common.Visual.Tables.RendererDate;
import Common.Visual.Tables.RendererProgressBar;
import Common.Visual.Tables.RendererStatusEnum;
import _VisualDVM.ComponentsServer.BugReport.BugReport;
import _VisualDVM.ComponentsServer.BugReport.BugReportState;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Comparator;
public class BugReportsForm extends DataSetControlForm<BugReport> {
    //todo упразднить и раскидать по стандартным фильтрам. галки и столбцы
    public static String filterKey = "";
    public static String filterSenderName = "";
    public static String filterDescription = "";//ввести рендерер, чтобы значение столбца выдавало фулл описание и фильтр по столбцу работал
    public static String filterComment = "";
    public static String filterExecutor = "";
    public static String filterVersion = "";
    public static boolean filterOpenedOnly = false;
    public static boolean filterMyOnly = false;
    //-
    public BugReportsForm(DataSet<?, BugReport> dataSource_in, JPanel mountPanel_in) {
        super(dataSource_in, mountPanel_in);
    }
    @Override
    protected void createColumns() {
        AddColumns(
                new ColumnInfo<BugReport>("Описание") {
                    @Override
                    public Object getFieldAt(BugReport object) {
                        return object.getDescriptionHeader();
                    }
                    @Override
                    public int getMaxWidth() {
                        return 600;
                    }
                },
                new ColumnInfo<BugReport>("Отправитель") {
                    @Override
                    public Object getFieldAt(BugReport object) {
                        return object.sender_name;
                    }
                },
                new ColumnInfo<BugReport>("Исполнитель") {
                    @Override
                    public Object getFieldAt(BugReport object) {
                        return object.executor;
                    }
                },
                new ColumnInfo<BugReport>("Проект") {
                    @Override
                    public Object getFieldAt(BugReport object) {
                        return object.project_version;
                    }
                },
                new ColumnInfo<BugReport>("Завершенность") {
                    @Override
                    public Object getFieldAt(BugReport object) {
                        return object.percentage;
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererProgressBar.class;
                    }
                },
                new ColumnInfo<BugReport>("Дата создания") {
                    @Override
                    public Object getFieldAt(BugReport object) {
                        return object.date;
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererDate.class;
                    }
                },
                new ColumnInfo<BugReport>("Дата изменения") {
                    @Override
                    public Object getFieldAt(BugReport object) {
                        return object.change_date;
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererDate.class;
                    }
                },
                new ColumnInfo<BugReport>("Статус") {
                    @Override
                    public Object getFieldAt(BugReport object) {
                        return object.state;
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererStatusEnum.class;
                    }
                }
                /*,
                new ColumnInfo<BugReport>("id_") {
                    @Override
                    public Object getFieldAt(BugReport object) {
                        return object.id_;
                    }
                }
                 */
        );
    }
    @Override
    protected boolean hasCheckBox() {
        return false;
    }
    @Override
    protected void ShowCurrentObject() throws Exception {
        super.ShowCurrentObject();
        Global.mainModule.getUI().getMainWindow().getCallbackWindow().ShowCurrentBugReport();
    }
    @Override
    protected void ShowNoCurrentObject() throws Exception {
        super.ShowNoCurrentObject();
        Global.mainModule.getUI().getMainWindow().getCallbackWindow().ShowNoCurrentBugReport();
    }
    @Override
    protected void MouseAction2() throws Exception {
        Global.mainModule.getPass(PassCode.OpenBugReportTestProject).Do();
    }
    @Override
    protected void createControl() {
        //https://stackoverflow.com/questions/9091208/jtable-enter-key
        super.createControl();
        final String solve = "Solve";
        KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
        control.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(enter, solve);
        control.getActionMap().put(solve, new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Global.mainModule.getPass(PassCode.OpenBugReportTestProject).Do();
            }
        });
    }
    @Override
    public PassCode_ getDeletePassCode() {
        return PassCode.DeleteBugReport;
    }
    @Override
    public DataMenuBar createMenuBar() {
        return new DataMenuBar(dataSource.getPluralDescription(),
                PassCode.SynchronizeBugReports,
              //  PassCode.DownloadAllBugReportsArchives,
                PassCode.AddBugReport,
                PassCode.PublishBugReport,
                PassCode.OpenBugReportTestProject,
                PassCode.OpenBugReport,
                PassCode.UpdateBugReportProgress,
                PassCode.CloseBugReport,
                PassCode.DeleteBugReport);
    }
    @Override
    protected Comparator<BugReport> getDefaultComparator() {

        return (o1, o2) -> -Long.compare(o1.date, o2.date);
    }
    @Override
    public boolean isObjectVisible(BugReport object) {
        return super.isObjectVisible(object) && (
                object.state.equals(BugReportState.draft) ||
                        (String.valueOf(object.id)).toUpperCase().contains(filterKey.toUpperCase())
                                && object.sender_name.toUpperCase().contains(filterSenderName.toUpperCase())
                                && object.description.toUpperCase().contains(filterDescription.toUpperCase())
                                && object.comment.toUpperCase().contains(filterComment.toUpperCase())
                                && object.executor.toUpperCase().contains(filterExecutor.toUpperCase())
                                && object.project_version.toUpperCase().contains(filterVersion.toUpperCase())
                                && (!filterOpenedOnly || object.state.equals(BugReportState.active))
                                && (!filterMyOnly ||
                                (object.sender_address.equalsIgnoreCase(Global.mainModule.getAccount().email) ||
                                        object.executor_address.equalsIgnoreCase(Global.mainModule.getAccount().email)
                                )
                        )
        );
    }
}
