package _VisualDVM.TestingSystem.SAPFOR.SapforConfiguration.UI;
import Common.CommonConstants;
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.SAPFOR.SapforConfiguration.SapforConfiguration;
import _VisualDVM.Visual.RDataSetControlForm;
import _VisualDVM.Visual.Tables.RendererConfigurationAuto;
import _VisualDVM.Visual.Tables.SwitcherConfigurationAuto;

import javax.swing.*;
public class SapforConfigurationsForm extends RDataSetControlForm<SapforConfiguration> {
    public SapforConfigurationsForm(DataSet<?, SapforConfiguration> dataSource_in, JPanel mountPanel_in) {
        super(dataSource_in, mountPanel_in);
    }
    @Override
    protected void createColumns() {
        AddColumns(
                new ColumnInfo<SapforConfiguration>("имя") {
                    @Override
                    public Object getFieldAt(SapforConfiguration object) {
                        return object.description;
                    }
                },
                new ColumnInfo<SapforConfiguration>("автор") {
                    @Override
                    public Object getFieldAt(SapforConfiguration object) {
                        return object.sender_name;
                    }
                },
                new ColumnInfo<SapforConfiguration>("") {
                    @Override
                    public Object getFieldAt(SapforConfiguration object) {
                        return object.printAuto();
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererConfigurationAuto.class;
                    }
                    @Override
                    public Class getEditorClass() {
                        return SwitcherConfigurationAuto.class;
                    }
                    @Override
                    public int getMinWidth() {
                        return 25;
                    }
                    @Override
                    public int getMaxWidth() {
                        return 25;
                    }
                },
                new ColumnInfo<SapforConfiguration>("ядра") {
                    @Override
                    public Object getFieldAt(SapforConfiguration object) {
                        return object.kernels;
                    }
                },
                new ColumnInfo<SapforConfiguration>("параметры") {
                    @Override
                    public Object getFieldAt(SapforConfiguration object) {
                        return ((ConfigurationCache) VisualCaches.GetCache(object)).settingsSummary;
                    }
                },
                new ColumnInfo<SapforConfiguration>("группы") {
                    @Override
                    public Object getFieldAt(SapforConfiguration object) {
                        return ((ConfigurationCache) VisualCaches.GetCache(object)).groupsSummary;
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererMultiline.class;
                    }
                },
                new ColumnInfo<SapforConfiguration>("тестов") {
                    @Override
                    public Object getFieldAt(SapforConfiguration object) {
                        return ((ConfigurationCache) VisualCaches.GetCache(object)).testsCount;
                    }
                },
                new ColumnInfo<SapforConfiguration>("эталон") {
                    @Override
                    public Object getFieldAt(SapforConfiguration object) {
                        return object.printEthalon();
                    }
                }
        );
    }
    @Override
    public PassCode_ getDeletePassCode() {
        return PassCode.DeleteSapforConfiguration;
    }
    @Override
    public DataMenuBar createMenuBar() {
        return new DataMenuBar(dataSource.getPluralDescription(),
                PassCode.PublishSapforConfiguration,
                PassCode.EditSapforConfiguration,
                PassCode.SetSapforConfigurationEthalon,
                PassCode.DropSapforConfigurationEthalon,
                PassCode.ShowCurrentSAPFORConfigurationTests,
                PassCode.SaveCurrentSAPFORConfiguration,
                PassCode.DeleteSapforConfiguration,
                PassCode.StartSelectedSAPFORConfigurations);
    }
    @Override
    protected DBObjectDialog getDialog() {
        return new SapforConfigurationDialog();
    }
    @Override
    protected void ShowCurrentObject() throws Exception {
        super.ShowCurrentObject();
        if (current.ethalon_id != CommonConstants.Nan) {
            Global.testingServer.db.sapforPackages.getUI().SetCurrentByPK(current.ethalon_id);
        } else {
            Global.testingServer.db.sapforPackages.getUI().ClearSelection();
        }
    }
    @Override
    protected boolean isObjectEditable(SapforConfiguration 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<SapforConfiguration>(dataSource, new MenuBarButton() {
                    {
                        setText("Свои");
                        setToolTipText("Отображать только конфигурации тестирования SAPFOR авторства пользователя");
                    }
                }, false) {
                    @Override
                    protected boolean validate(SapforConfiguration object) {
                        return Global.mainModule.getAccount().email.equals(object.sender_address);
                    }
                }
        );
    }
}
