package _VisualDVM.TestingSystem.DVM.DVMTasks.UI;
import Common.Database.Tables.DataSet;
import Common.Visual.Controls.StableMenuItem;
import Common.Visual.DataSetControlForm;
import Common.Visual.DataSetFiltersMenu;
import Common.Visual.FilterMenuFlag;
import Common.Visual.Menus.DataMenuBar;
import Common.Visual.Tables.ColumnInfo;
import Common.Visual.Tables.HeaderTextFilter;
import Common.Visual.Tables.RendererProgressBar;
import Common.Visual.Tables.RendererStatusEnum;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.Tasks.TaskState;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.ProjectData.LanguageName;
import _VisualDVM.TestingSystem.DVM.DVMTasks.DVMRunTask;
import _VisualDVM.Utils;

import javax.swing.*;
import java.util.Comparator;
public class DVMRunTasksForm extends DataSetControlForm<DVMRunTask> {
    public DVMRunTasksForm(DataSet<?, DVMRunTask> dataSource_in, JPanel mountPanel_in) {
        super(dataSource_in, mountPanel_in);
    }
    @Override
    protected void createColumns() {
        AddColumns(
                new ColumnInfo<DVMRunTask>("Тест") {
                    @Override
                    public Object getFieldAt(DVMRunTask object) {
                        return object.test_description;
                    }
                },
                new ColumnInfo<DVMRunTask>("Язык") {
                    @Override
                    public Object getFieldAt(DVMRunTask object) {
                        return object.language;
                    }
                },
                new ColumnInfo<DVMRunTask>("Флаги") {
                    @Override
                    public Object getFieldAt(DVMRunTask object) {
                        return object.flags;
                    }
                },
                new ColumnInfo<DVMRunTask>("Компиляция") {
                    @Override
                    public Object getFieldAt(DVMRunTask object) {
                        return object.compilation_state;
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererStatusEnum.class;
                    }
                },
                new ColumnInfo<DVMRunTask>("Запуск") {
                    @Override
                    public Object getFieldAt(DVMRunTask object) {
                        return object.state;
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererStatusEnum.class;
                    }
                },
                new ColumnInfo<DVMRunTask>("Матрица") {
                    @Override
                    public Object getFieldAt(DVMRunTask object) {
                        return object.matrix;
                    }
                },
                new ColumnInfo<DVMRunTask>("dim") {
                    @Override
                    public Object getFieldAt(DVMRunTask object) {
                        return Utils.getMatrixDimension(object.matrix);
                    }
                },
                new ColumnInfo<DVMRunTask>("CPU") {
                    @Override
                    public Object getFieldAt(DVMRunTask object) {
                        return Utils.getMatrixProcessors(object.matrix);
                    }
                },
                new ColumnInfo<DVMRunTask>("Окружение") {
                    @Override
                    public Object getFieldAt(DVMRunTask object) {
                        return object.getEnvironments();
                    }
                },
                new ColumnInfo<DVMRunTask>("usr.par") {
                    @Override
                    public Object getFieldAt(DVMRunTask object) {
                        return object.getUsrPar();
                    }
                },
                new ColumnInfo<DVMRunTask>("Время компиляции (с)") {
                    @Override
                    public Object getFieldAt(DVMRunTask object) {
                        return object.compilation_time;
                    }
                },
                new ColumnInfo<DVMRunTask>("Время запуска (с)") {
                    @Override
                    public Object getFieldAt(DVMRunTask object) {
                        return object.Time;
                    }
                },
                new ColumnInfo<DVMRunTask>("Чистое время (с)") {
                    @Override
                    public Object getFieldAt(DVMRunTask object) {
                        return object.CleanTime;
                    }
                },
                new ColumnInfo<DVMRunTask>("Прогресс") {
                    @Override
                    public Object getFieldAt(DVMRunTask object) {
                        return object.progress;
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererProgressBar.class;
                    }
                }
        );
    }
    @Override
    public DataMenuBar createMenuBar() {
        return new DataMenuBar(dataSource.getPluralDescription(), PassCode.DownloadTaskTest, PassCode.CompareDVMRunTaskToEthalon);
    }
    @Override
    protected void createFilters() {
        AddFilters(
                new HeaderTextFilter<>(dataSource, 2, 70),
                new HeaderTextFilter<>(dataSource, 7, 100, false),
                new DataSetFiltersMenu<DVMRunTask>("Язык", dataSource) {
                    @Override
                    public void fill() {
                        for (LanguageName languageName : LanguageName.values()) {
                            field_filters.add(new FilterMenuFlag<DVMRunTask>(dataSource, new StableMenuItem(languageName.getDescription()), false) {
                                @Override
                                protected boolean validate(DVMRunTask object) {
                                    return object.language.equals(languageName);
                                }
                            });
                        }
                    }
                },
                new DataSetFiltersMenu<DVMRunTask>("Компиляция", dataSource) {
                    @Override
                    public void fill() {
                        for (TaskState state : TaskState.values()) {
                            if (state.isVisible()) {
                                field_filters.add(new FilterMenuFlag<DVMRunTask>(dataSource, new StableMenuItem(state.getDescription()), false) {
                                    @Override
                                    protected boolean validate(DVMRunTask object) {
                                        return object.compilation_state.equals(state);
                                    }
                                });
                            }
                        }
                    }
                },
                new DataSetFiltersMenu<DVMRunTask>("Запуск", dataSource) {
                    @Override
                    public void fill() {
                        for (TaskState state : TaskState.values()) {
                            if (state.isVisible()) {
                                field_filters.add(new FilterMenuFlag<DVMRunTask>(dataSource, new StableMenuItem(state.getDescription()), false) {
                                    @Override
                                    protected boolean validate(DVMRunTask object) {
                                        return object.state.equals(state);
                                    }
                                });
                            }
                        }
                    }
                }
        );
    }
    @Override
    protected Comparator<DVMRunTask> getDefaultComparator() {
        return new Comparator<DVMRunTask>() {
            @Override
            public int compare(DVMRunTask o1, DVMRunTask o2) {
                return o1.state.ordinal() - o2.state.ordinal();
            }
        };
    }
    @Override
    protected void ShowCurrentObject() throws Exception {
        super.ShowCurrentObject();
        Global.mainModule.getUI().getMainWindow().getTestingWindow().ShowCurrentDVMTask();
    }
    @Override
    protected void ShowNoCurrentObject() throws Exception {
        super.ShowNoCurrentObject();
        Global.mainModule.getUI().getMainWindow().getTestingWindow().ShowNoCurrentDVMTask();
    }
}
