package _VisualDVM.TestingSystem.DVM.DVMConfiguration.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.HeaderTextFilter;
import Common.Visual.Tables.RendererMultiline;
import Common.Visual.Windows.Dialog.DBObjectDialog;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.ServerObjectsCache.ConfigurationCache;
import _VisualDVM.ServerObjectsCache.VisualCaches;
import _VisualDVM.TestingSystem.DVM.DVMConfiguration.DVMConfiguration;
import _VisualDVM.Visual.RDataSetControlForm;

import javax.swing.*;
public class DVMConfigurationsForm extends RDataSetControlForm<DVMConfiguration> {
    public DVMConfigurationsForm(DataSet<?, DVMConfiguration> dataSource_in, JPanel mountPanel_in) {
        super(dataSource_in, mountPanel_in);
    }
    @Override
    protected void createColumns() {
        AddColumns(
                new ColumnInfo<DVMConfiguration>("имя") {
                    @Override
                    public Object getFieldAt(DVMConfiguration object) {
                        return object.description;
                    }
                },
                new ColumnInfo<DVMConfiguration>("автор") {
                    @Override
                    public Object getFieldAt(DVMConfiguration object) {
                        return object.sender_name;
                    }
                },
                new ColumnInfo<DVMConfiguration>("ядра") {
                    @Override
                    public Object getFieldAt(DVMConfiguration object) {
                        return object.kernels;
                    }
                },
                new ColumnInfo<DVMConfiguration>("параметры") {
                    @Override
                    public Object getFieldAt(DVMConfiguration object) {
                        return ((ConfigurationCache) VisualCaches.GetCache(object)).settingsSummary;
                    }
                },
                new ColumnInfo<DVMConfiguration>("группы") {
                    @Override
                    public Object getFieldAt(DVMConfiguration object) {
                        return ((ConfigurationCache) VisualCaches.GetCache(object)).groupsSummary;
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererMultiline.class;
                    }
                },
                new ColumnInfo<DVMConfiguration>("тестов") {
                    @Override
                    public Object getFieldAt(DVMConfiguration object) {
                        return ((ConfigurationCache) VisualCaches.GetCache(object)).testsCount;
                    }
                },
                new ColumnInfo<DVMConfiguration>("компиляция(с)") {
                    @Override
                    public Object getFieldAt(DVMConfiguration object) {
                        return object.c_maxtime;
                    }
                },
                new ColumnInfo<DVMConfiguration>("запуск(с)") {
                    @Override
                    public Object getFieldAt(DVMConfiguration object) {
                        return object.maxtime;
                    }
                }
        );
    }
    @Override
    public PassCode_ getDeletePassCode() {
        return PassCode.DeleteConfiguration;
    }
    @Override
    public DataMenuBar createMenuBar() {
        return new DataMenuBar(dataSource.getPluralDescription(),
                PassCode.PublishConfiguration,
                PassCode.EditConfiguration,
                PassCode.ShowCurrentDVMConfigurationTests,
                PassCode.SaveCurrentDVMConfiguration,
                PassCode.DeleteConfiguration,
                PassCode.StartSelectedDVMConfigurations);
    }
    @Override
    protected DBObjectDialog getDialog() {
        return new DVMConfigurationDialog();
    }
    @Override
    protected boolean isObjectEditable(DVMConfiguration object) {
        if (Global.mainModule.getAccount().CheckRegistered(null)) {
            switch (Global.mainModule.getAccount().role) {
                case Admin:
                    return true;
                default:
                    return (Global.mainModule.getAccount().email.equals(object.sender_address));
            }
        }
        return false;
    }
    @Override
    protected void createFilters() {
        AddFilters(
                new HeaderTextFilter<>(dataSource, 2, 65),
                new FilterFlag<DVMConfiguration>(dataSource, new MenuBarButton() {
                    {
                        setText("Свои");
                        setToolTipText("Отображать только конфигурации тестирования DVM авторства пользователя");
                    }
                }, false) {
                    @Override
                    protected boolean validate(DVMConfiguration object) {
                        return Global.mainModule.getAccount().email.equals(object.sender_address);
                    }
                }
        );
    }
}

