package TestingSystem.DVM.DVMTasks;
import Common.Current;
import Common.Database.DataSet;
import Common.Database.TableFilter;
import Common.UI.DataSetControlForm;
import Common.UI.Menus_2023.VisualiserMenu;
import Common.UI.UI;
import GlobalData.Tasks.TaskState;
import TestingSystem.DVM.DVMPackage.DVMPackage;

import javax.swing.*;
import java.util.Vector;

import static Common.UI.Tables.TableRenderers.RendererProgress;
import static Common.UI.Tables.TableRenderers.RendererStatusEnum;
public class DVMRunTasksSet extends DataSet<Integer, DVMRunTask> {
    //todo обобщить бы наличие фильтров для всех таблиц.
    public Vector<TableFilter<DVMRunTask>> compilationFilters;
    public Vector<TableFilter<DVMRunTask>> runFilters;
    DVMPackage target;
    public DVMRunTasksSet() {
        super(Integer.class, DVMRunTask.class);
        //todo обобщить бы наличие фильтров для всех таблиц.
        if (Current.hasUI()) {
            compilationFilters = new Vector<>();
            runFilters = new Vector<>();
            //--
            for (TaskState state : TaskState.values()) {
                if (state.isVisible()) {
                    compilationFilters.add(new TableFilter<DVMRunTask>(this, state.getDescription()) {
                        @Override
                        protected boolean validate(DVMRunTask object) {
                            return object.compilation_state.equals(state);
                        }
                    });
                }
            }
            //--
            for (TaskState state : TaskState.values()) {
                if (state.isVisible()) {
                    runFilters.add(new TableFilter<DVMRunTask>(this, state.getDescription()) {
                        @Override
                        protected boolean validate(DVMRunTask object) {
                            return object.state.equals(state);
                        }
                    });
                }
            }
        }
    }
    public void ResetFiltersCount() {
        for (TableFilter<DVMRunTask> filter : compilationFilters)
            filter.count = 0;
        for (TableFilter<DVMRunTask> filter : runFilters)
            filter.count = 0;
    }
    public void ShowFiltersCount() {
        for (TableFilter<DVMRunTask> filter : compilationFilters) {
            filter.ShowDescriptionAndCount();
        }
        for (TableFilter<DVMRunTask> filter : runFilters) {
            filter.ShowDescriptionAndCount();
        }
    }
    public boolean applyFilters(DVMRunTask object) {
        boolean compilation = false;
        //возвращать сразу нельзя. иначе количество не посчитает.
        for (TableFilter filter : compilationFilters) {
            if (filter.Validate(object)) {
                compilation |= true;
            }
        }
        boolean run = false;
        for (TableFilter filter : runFilters) {
            if (filter.Validate(object)) {
                run |= true;
            }
        }
        return compilation && run;
    }
    @Override
    public void mountUI(JPanel content_in) {
        super.mountUI(content_in);
        //-
        DVMRunTasksBar menuBar = (DVMRunTasksBar) UI.menuBars.get(getClass());
        menuBar.DropFilters();
        //----
        menuBar.addFilters(
                new VisualiserMenu("Компиляция", "/icons/Filter.png", true) {
                    {
                        for (TableFilter filter : compilationFilters)
                            add(filter.menuItem);
                    }
                },
                new VisualiserMenu("Запуск", "/icons/Filter.png", true) {
                    {
                        for (TableFilter filter : runFilters)
                            add(filter.menuItem);
                    }
                }
        );
    }
    @Override
    public void ShowUI() {
        ResetFiltersCount();
        super.ShowUI();
        ShowFiltersCount();
    }
    @Override
    public void ShowUI(Object key) {
        ResetFiltersCount();
        super.ShowUI(key);
        ShowFiltersCount();
    }
    @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;
    }
}
