package _VisualDVM.TestingSystem.DVM.DVMPackage;
import Common.Database.Tables.iDBTable;
import Common.Visual.DataSetControlForm;
import _VisualDVM.Current;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.ServerObjectsCache.PackageCache;
import _VisualDVM.ServerObjectsCache.VisualCaches;
import _VisualDVM.Visual.UI;

import java.util.Comparator;
import java.util.Date;

import static Common.Visual.Tables.TableRenderers.*;
public class DVMPackageDBTable extends iDBTable<DVMPackage> {
    public static boolean filterMyOnly = false;
    public static boolean filterActive = false;
    public DVMPackageDBTable() {
        super(DVMPackage.class);
    }
    @Override
    public Current CurrentName() {
        return Current.DVMPackage;
    }
    @Override
    public String getSingleDescription() {
        return "пакет задач DVM";
    }
    @Override
    public String getPluralDescription() {
        return "пакеты задач DVM";
    }
    @Override
    protected DataSetControlForm createUI() {
        return new DataSetControlForm(this) {
            @Override
            public boolean hasCheckBox() {
                return true;
            }
            @Override
            protected void AdditionalInitColumns() {
                columns.get(6).setRenderer(RendererMultiline);
                //   columns.get(7).setRenderer(RendererMultiline);
                columns.get(9).setRenderer(RendererProgress);
                columns.get(10).setRenderer(RendererDate);
                columns.get(11).setRenderer(RendererDate);
                columns.get(12).setRenderer(RendererStatusEnum);
            }
            @Override
            public void ShowCurrentObject() throws Exception {
                super.ShowCurrentObject();
                //--
                Global.testingServer.db.dvmRunTasks.ShowDVMPackage(getCurrent());
                UI.getMainWindow().getTestingWindow().DropTestRunTasksComparison();
                //--
            }
            @Override
            public void ShowNoCurrentObject() throws Exception {
                super.ShowNoCurrentObject();
                Global.testingServer.db.dvmRunTasks.ShowNoPackage();
                UI.getMainWindow().getTestingWindow().DropTestRunTasksComparison();
            }
            @Override
            public void MouseAction2() throws Exception {
                Global.mainModule.getPass(PassCode.DownloadDVMPackage).Do();
            }
        };
    }
    @Override
    public String[] getUIColumnNames() {
        return new String[]{
                "Автор",
                "Машина",
                "Пользователь",
                "DVM",
                "Конфигурации",
                "Задач",
                "Ядер",
                "Прогресс",
                "Начало",
                "Изменено",
                "Статус"
        };
    }
    @Override
    public Object getFieldAt(DVMPackage object, int columnIndex) {
        PackageCache cache = (PackageCache) VisualCaches.GetCache(object);
        switch (columnIndex) {
            case 2:
                return object.sender_name;
            case 3:
                return object.machine_address + ":" + object.machine_port;
            case 4:
                return object.user_name;
            case 5:
                return object.version;
            case 6:
                return cache.getConfigurationsDescriptions();
            case 7:
                return object.tasksCount;
            case 8:
                return object.kernels;
            case 9:
                return object.progress;
            case 10:
                return new Date(object.StartDate);
            case 11:
                return new Date(object.ChangeDate);
            case 12:
                return object.state;
            default:
                return null;
        }
    }
    @Override
    public Comparator<DVMPackage> getComparator() {
        return new Comparator<DVMPackage>() {
            @Override
            public int compare(DVMPackage o1, DVMPackage o2) {
                return o2.id - o1.id;
            }
        };
        // return Comparator.comparingInt(o -> o.).reversed();
    }
    @Override
    public Class getMenuBarClass() {
        return DVMPackagesBar.class;
    }
}
