package _VisualDVM.TestingSystem.DVM.DVMTasks;
import Common.Database.Tables.DataSet;
import Common.Visual.DBObjectFilter;
import Common.Visual.DataSetControlForm;
import Common.Visual.DataSetFilter;
import _VisualDVM.Current;
import _VisualDVM.GlobalData.Tasks.TaskState;
import _VisualDVM.TestingSystem.DVM.DVMPackage.DVMPackage;

import java.util.Comparator;

import static Common.Visual.Tables.TableRenderers.RendererProgress;
import static Common.Visual.Tables.TableRenderers.RendererStatusEnum;
public class DVMRunTasksSet extends DataSet<Integer, DVMRunTask> {
    //todo обобщить бы наличие фильтров для всех таблиц.
    DVMPackage target;
    public DVMRunTasksSet() {
        super(Integer.class, DVMRunTask.class);
    }
    @Override
    protected void createFilters() {
        filters.add(new DataSetFilter<DVMRunTask>("Компиляция", this) {
            @Override
            public void fill() {
                for (TaskState state : TaskState.values()) {
                    if (state.isVisible()) {
                        field_filters.add(new DBObjectFilter<DVMRunTask>(dataSet, state.getDescription()) {
                            @Override
                            protected boolean validate(DVMRunTask object) {
                                return object.compilation_state.equals(state);
                            }
                        });
                    }
                }
            }
        });
        filters.add(new DataSetFilter<DVMRunTask>("Запуск", this) {
            @Override
            public void fill() {
                for (TaskState state : TaskState.values()) {
                    if (state.isVisible()) {
                        field_filters.add(new DBObjectFilter<DVMRunTask>(dataSet, state.getDescription()) {
                            @Override
                            protected boolean validate(DVMRunTask object) {
                                return object.state.equals(state);
                            }
                        });
                    }
                }
            }
        });
    }
    @Override
    public String getSingleDescription() {
        return "задача";
    }
    @Override
    public String getPluralDescription() {
        return "задачи";
    }
    @Override
    protected DataSetControlForm createUI() {
        return new DataSetControlForm(this) {
            @Override
            public boolean hasCheckBox() {
                return true;
            }
            @Override
            protected void AdditionalInitColumns() {
                columns.get(5).setRenderer(RendererStatusEnum);
                columns.get(6).setRenderer(RendererStatusEnum);
                columns.get(13).setRenderer(RendererProgress);
            }
        };
    }
    @Override
    public String[] getUIColumnNames() {
        return new String[]{
                "Тест",
                "Язык",
                "Флаги",
                "Компиляция",
                "Запуск",
                "Матрица",
                "Окружение",
                "usr.par",
                "Время компиляции (с)",
                "Время запуска (с)",
                "Чистое время (с)",
                "Прогресс",
        };
    }
    @Override
    public Object getFieldAt(DVMRunTask object, int columnIndex) {
        switch (columnIndex) {
            case 2:
                return object.test_description;
            case 3:
                return object.language;
            case 4:
                return object.flags;
            case 5:
                return object.compilation_state;
            case 6:
                return object.state;
            case 7:
                return object.matrix;
            case 8:
                return object.getEnvironments();
            case 9:
                return object.getUsrPar();
            case 10:
                return object.compilation_time;
            case 11:
                return object.Time;
            case 12:
                return object.CleanTime;
            case 13:
                return object.progress;
            default:
                return null;
        }
    }
    @Override
    public Current CurrentName() {
        return Current.DVMRunTask;
    }
    public void ShowDVMPackage(DVMPackage dvmPackage) {
        target = dvmPackage;
        ClearUI();
        Data.clear();
        if (dvmPackage.package_json == null) {
            if (dvmPackage.getJsonFile().exists()) {
                try {
                    dvmPackage.readJson();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        if (dvmPackage.package_json != null) {
            for (DVMCompilationTask compilationTask : dvmPackage.package_json.compilationTasks) {
                for (DVMRunTask runTask : compilationTask.runTasks) {
                    Data.put(runTask.id, runTask);
                }
            }
            ShowUI();
        }
    }
    public void ShowNoPackage() {
        ClearUI();
        Data.clear();
        target = null;
    }
    @Override
    public Comparator<DVMRunTask> getComparator() {
        return new Comparator<DVMRunTask>() {
            @Override
            public int compare(DVMRunTask o1, DVMRunTask o2) {
                return o1.state.ordinal() - o2.state.ordinal();
            }
        };
        // return Comparator.comparingInt(o -> o.).reversed();
    }
    @Override
    public Class getMenuBarClass() {
        return DVMRunTasksBar.class;
    }
}
