package _VisualDVM.TestingSystem.SAPFOR.SapforSettings.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.TestingSystem.SAPFOR.SapforSettings.SapforSettings;
import _VisualDVM.Visual.RDataSetControlForm;

import javax.swing.*;
public class SapforSettingsForm extends RDataSetControlForm<SapforSettings> {
    public SapforSettingsForm(DataSet<?, SapforSettings> dataSource_in, JPanel mountPanel_in) {
        super(dataSource_in, mountPanel_in);
    }
    @Override
    protected void createColumns() {
        AddColumns(
                new ColumnInfo<SapforSettings>("имя") {
                    @Override
                    public Object getFieldAt(SapforSettings object) {
                        return object.description;
                    }
                },
                new ColumnInfo<SapforSettings>("автор") {
                    @Override
                    public Object getFieldAt(SapforSettings object) {
                        return object.sender_name;
                    }
                },
                new ColumnInfo<SapforSettings>("флаги") {
                    @Override
                    public Object getFieldAt(SapforSettings object) {
                        return object.flags;
                    }
                }
        );
    }
    @Override
    protected void createFilters() {
        AddFilters(new HeaderTextFilter<>(dataSource, 2, 65),
                new FilterFlag<SapforSettings>(dataSource, new MenuBarButton() {
                    {
                        setText("Свои");
                        setToolTipText("Отображать только параметры тестирования SAPFOR авторства пользователя");
                    }
                }, false) {
                    @Override
                    protected boolean validate(SapforSettings object) {
                        return Global.mainModule.getAccount().email.equals(object.sender_address);
                    }
                });
    }
    @Override
    public PassCode_ getDeletePassCode() {
        return PassCode.DeleteSapforSettings;
    }
    @Override
    public DataMenuBar createMenuBar() {
        return new DataMenuBar(dataSource.getPluralDescription(),
                PassCode.PublishSapforSettings,
                PassCode.CloneSapforSettings,
                PassCode.EditSapforSettings,
                PassCode.DeleteSapforSettings);
    }
    @Override
    protected DBObjectDialog getDialog() {
        return new SapforSettingsDialog();
    }
    @Override
    protected boolean isObjectEditable(SapforSettings 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;
    }
}
