package _VisualDVM.TestingSystem.DVM.DVMSettings.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.Windows.Dialog.DBObjectDialog;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.ServerObjectsCache.DVMSettingsCache;
import _VisualDVM.ServerObjectsCache.VisualCaches;
import _VisualDVM.TestingSystem.DVM.DVMSettings.DVMSettings;
import _VisualDVM.Visual.RDataSetControlForm;

import javax.swing.*;
public class DVMSettingsForm extends RDataSetControlForm<DVMSettings> {
    public DVMSettingsForm(DataSet<?, DVMSettings> dataSource_in, JPanel mountPanel_in) {
        super(dataSource_in, mountPanel_in);
    }
    @Override
    protected void createColumns() {
        AddColumns(
                new ColumnInfo<DVMSettings>("имя") {
                    @Override
                    public Object getFieldAt(DVMSettings object) {
                        return object.description;
                    }
                },
                new ColumnInfo<DVMSettings>("автор") {
                    @Override
                    public Object getFieldAt(DVMSettings object) {
                        return object.sender_name;
                    }
                },
                new ColumnInfo<DVMSettings>("флаги") {
                    @Override
                    public Object getFieldAt(DVMSettings object) {
                        return ((DVMSettingsCache) VisualCaches.GetCache(object)).optionsSummary;
                    }
                },
                new ColumnInfo<DVMSettings>("окружение") {
                    @Override
                    public Object getFieldAt(DVMSettings object) {
                        return ((DVMSettingsCache) VisualCaches.GetCache(object)).environmentsSummary;
                    }
                },
                new ColumnInfo<DVMSettings>("usr.par") {
                    @Override
                    public Object getFieldAt(DVMSettings object) {
                        return object.printUsrPar();
                    }
                },
                new ColumnInfo<DVMSettings>("куб") {
                    @Override
                    public Object getFieldAt(DVMSettings object) {
                        return object.cube;
                    }
                },
                new ColumnInfo<DVMSettings>("max") {
                    @Override
                    public Object getFieldAt(DVMSettings object) {
                        return object.max_proc_count;
                    }
                },
                new ColumnInfo<DVMSettings>("min dim") {
                    @Override
                    public Object getFieldAt(DVMSettings object) {
                        return object.min_dim_proc_count;
                    }
                },
                new ColumnInfo<DVMSettings>("max dim") {
                    @Override
                    public Object getFieldAt(DVMSettings object) {
                        return object.max_dim_proc_count;
                    }
                }
        );
    }
    @Override
    protected void createFilters() {
        AddFilters(new HeaderTextFilter<>(dataSource, 2, 65),
                new FilterFlag<DVMSettings>(dataSource, new MenuBarButton() {
                    {
                        setText("Свои");
                        setToolTipText("Отображать только параметры тестирования DVM авторства пользователя");
                    }
                }, false) {
                    @Override
                    protected boolean validate(DVMSettings object) {
                        return Global.mainModule.getAccount().email.equals(object.sender_address);
                    }
                });
    }
    @Override
    public PassCode_ getDeletePassCode() {
        return PassCode.DeleteDVMSettings;
    }
    @Override
    public DataMenuBar createMenuBar() {
        return new DataMenuBar(dataSource.getPluralDescription(),
                PassCode.PublishDVMSettings,
                PassCode.CloneDVMSettings,
                PassCode.EditDVMSettings,
                PassCode.DeleteDVMSettings);
    }
    @Override
    protected DBObjectDialog getDialog() {
        return new DVMSettingsDialog();
    }
    @Override
    protected boolean isObjectEditable(DVMSettings 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;
    }
}
