package _VisualDVM.TestingSystem.DVM.DVMPackage.UI;
import Common.Database.Tables.DataSet;
import Common.Passes.PassCode_;
import Common.Visual.Controls.MenuBarButton;
import Common.Visual.FilterFlag;
import Common.Visual.Menus.DataMenuBar;
import Common.Visual.Tables.ColumnInfo;
import Common.Visual.Tables.RendererDate;
import Common.Visual.Tables.RendererMilliseconds;
import Common.Visual.Tables.RendererProgressBar;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.ServerObjectsCache.DVMPackageCache;
import _VisualDVM.ServerObjectsCache.VisualCaches;
import _VisualDVM.TestingSystem.Common.TasksPackageState;
import _VisualDVM.TestingSystem.DVM.DVMPackage.DVMPackage;
import _VisualDVM.Visual.RDataSetControlForm;
import _VisualDVM.Visual.Tables.RendererTestingPackageStatus;

import javax.swing.*;
import java.util.Comparator;
public class DVMPackagesForm extends RDataSetControlForm<DVMPackage> {
    public DVMPackagesForm(DataSet<?, DVMPackage> dataSource_in, JPanel mountPanel_in) {
        super(dataSource_in, mountPanel_in);
    }
    @Override
    protected void createColumns() {
        AddColumns(
                new ColumnInfo<DVMPackage>("автор") {
                    @Override
                    public Object getFieldAt(DVMPackage object) {
                        return object.sender_name;
                    }
                },
                new ColumnInfo<DVMPackage>("машина") {
                    @Override
                    public Object getFieldAt(DVMPackage object) {
                        return object.machine_address + ":" + object.machine_port;
                    }
                },
                new ColumnInfo<DVMPackage>("пользователь") {
                    @Override
                    public Object getFieldAt(DVMPackage object) {
                        return object.user_name;
                    }
                },
                new ColumnInfo<DVMPackage>("DVM") {
                    @Override
                    public Object getFieldAt(DVMPackage object) {
                        return object.version;
                    }
                },
                new ColumnInfo<DVMPackage>("конфигурации") {
                    @Override
                    public Object getFieldAt(DVMPackage object) {
                        return ((DVMPackageCache) VisualCaches.GetCache(object)).confifurationsDescriptionsLine;
                    }
                },
                new ColumnInfo<DVMPackage>("задач") {
                    @Override
                    public Object getFieldAt(DVMPackage object) {
                        return object.tasksCount;
                    }
                },
                new ColumnInfo<DVMPackage>("ядер") {
                    @Override
                    public Object getFieldAt(DVMPackage object) {
                        return object.kernels;
                    }
                },
                new ColumnInfo<DVMPackage>("прогресс") {
                    @Override
                    public Object getFieldAt(DVMPackage object) {
                        return object.progress;
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererProgressBar.class;
                    }
                },
                new ColumnInfo<DVMPackage>("начало") {
                    @Override
                    public Object getFieldAt(DVMPackage object) {
                        return object.StartDate;
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererDate.class;
                    }
                },
                new ColumnInfo<DVMPackage>("изменено") {
                    @Override
                    public Object getFieldAt(DVMPackage object) {
                        return object.ChangeDate;
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererDate.class;
                    }
                },
                new ColumnInfo<DVMPackage>("время") {
                    @Override
                    public Object getFieldAt(DVMPackage object) {
                        return object.getMilliseconds();
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererMilliseconds.class;
                    }
                },
                new ColumnInfo<DVMPackage>("статус") {
                    @Override
                    public Object getFieldAt(DVMPackage object) {
                        return object;
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererTestingPackageStatus.class;
                    }
                }
        );
    }
    @Override
    protected void ShowCurrentObject() throws Exception {
        super.ShowCurrentObject();
        //--
        Global.testingServer.db.dvmRunTasks.ShowDVMPackage(getCurrent());
        Global.mainModule.getUI().getMainWindow().getTestingWindow().DropTestRunTasksComparison();
        //--
    }
    @Override
    protected void ShowNoCurrentObject() throws Exception {
        super.ShowNoCurrentObject();
        Global.testingServer.db.dvmRunTasks.ShowNoPackage();
        Global.mainModule.getUI().getMainWindow().getTestingWindow().DropTestRunTasksComparison();
    }
    @Override
    protected void MouseAction2() throws Exception {
        SelectAll(false);
        Global.mainModule.getPass(PassCode.DownloadDVMPackage).Do();
    }
    @Override
    public PassCode_ getDeletePassCode() {
        return PassCode.DeleteDVMPackage;
    }
    @Override
    public DataMenuBar createMenuBar() {
        return new DataMenuBar(dataSource.getPluralDescription()) {
            {
                addPasses(PassCode.SynchronizeTests);
                addSeparator();
                addPasses(PassCode.PauseDVMPackage, PassCode.ResumeDVMPackage, PassCode.AbortDVMPackage);
                addSeparator();
                addPasses(PassCode.DownloadDVMPackage,
                        //   PassCode.RefreshDVMPackageResults,
                        PassCode.ExportDVMPackageToExcel,
                        PassCode.DeleteDVMPackage);
            }
        };
    }
    @Override
    protected void createFilters() {
        AddFilters(
                new FilterFlag<DVMPackage>(dataSource, new MenuBarButton() {
                    {
                        setText("Свои");
                        setToolTipText("Отображать только пакеты тестов авторства пользователя");
                    }
                }, false) {
                    @Override
                    protected boolean validate(DVMPackage object) {
                        return Global.mainModule.getAccount().email.equals(object.sender_address);
                    }
                },
                new FilterFlag<DVMPackage>(dataSource, new MenuBarButton() {
                    {
                        setText("Активные");
                        setToolTipText("Отображать только активные пакеты тестов");
                    }
                }, false) {
                    @Override
                    protected boolean validate(DVMPackage object) {
                        return object.state.isActive();
                    }
                },
                new FilterFlag<DVMPackage>(dataSource, new MenuBarButton() {
                    {
                        setText("С ошибками");
                        setToolTipText("Отображать только пакеты тестов с ошибками");
                    }
                }, false) {
                    @Override
                    protected boolean validate(DVMPackage object) {
                        return object.state.equals(TasksPackageState.DoneWithErrors);
                    }
                }
        );
    }
    @Override
    protected Comparator<DVMPackage> getDefaultComparator() {
        return new Comparator<DVMPackage>() {
            @Override
            public int compare(DVMPackage o1, DVMPackage o2) {
                return o2.id - o1.id;
            }
        };
    }
}
