package _VisualDVM.Repository.Subscribes;
import Common.Database.Objects.DBObject;
import Common.Database.Tables.DBTable;
import Common.Database.Tables.FKBehaviour;
import Common.Database.Tables.FKCurrentObjectBehaviuor;
import Common.Database.Tables.FKDataBehaviour;
import Common.Visual.DataSetControlForm;
import Common.Visual.Windows.Dialog.DBObjectDialog;
import _VisualDVM.Current;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.Account.AccountRole;
import _VisualDVM.Repository.SubscriberWorkspace.SubscriberWorkspace;
import _VisualDVM.Repository.Subscribes.UI.SubscriberFields;
import _VisualDVM.Repository.Subscribes.UI.SubscriberForm;

import java.util.LinkedHashMap;
public class SubsribersDBTable extends DBTable<String, Subscriber> {
    public SubsribersDBTable() {
        super(String.class, Subscriber.class);
    }
    @Override
    public String getSingleDescription() {
        return "Адресат";
    }
    @Override
    protected DataSetControlForm createUI() {
        return new DataSetControlForm(this) {
            @Override
            public boolean hasCheckBox() {
                return true;
            }
            @Override
            protected void AdditionalInitColumns() {
                boolean admin = Global.mainModule.getAccount().role.equals(AccountRole.Admin);
                columns.get(0).setVisible(admin);
                columns.get(1).setVisible(Global.mainModule.getBugReport() != null);
                columns.get(3).setVisible(admin);
                columns.get(4).setVisible(admin);
            }
        };
    }
    @Override
    public String[] getUIColumnNames() {
        return new String[]{"Имя", "Роль", "Рассылка"};
    }
    @Override
    public Object getFieldAt(Subscriber object, int columnIndex) {
        switch (columnIndex) {
            case 2:
                return object.name;
            case 3:
                return object.role.getDescription();
            case 4:
                return (object.mailOn == 0) ? "выключена" : "включена";
        }
        return object.name;
    }
    @Override
    public Current CurrentName() {
        return Current.Subscriber;
    }
    @Override
    public DBObjectDialog<Subscriber, SubscriberFields> getDialog() {
        return new SubscriberForm();
    }
    @Override
    public LinkedHashMap<Class<? extends DBObject>, FKBehaviour> getFKDependencies() {
        LinkedHashMap<Class<? extends DBObject>, FKBehaviour> res = new LinkedHashMap<>();
        //-
        res.put(SubscriberWorkspace.class, new FKBehaviour(FKDataBehaviour.DELETE, FKCurrentObjectBehaviuor.ACTIVE));
        return res;
    }
    @Override
    public Class getMenuBarClass() {
        return SubscribersMenuBar.class;
    }
}
