package _VisualDVM.ComponentsServer.UserAccount.UI;
import Common.Database.Tables.DataSet;
import Common.Visual.DataSetControlForm;
import Common.Visual.Menus.DataMenuBar;
import Common.Visual.Tables.ColumnInfo;
import Common.Visual.Windows.Dialog.DBObjectDialog;
import _VisualDVM.ComponentsServer.UserAccount.AccountRole;
import _VisualDVM.ComponentsServer.UserAccount.UserAccount;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;

import javax.swing.*;
public class UserAccountsForm extends DataSetControlForm<UserAccount> {
    public UserAccountsForm(DataSet<?, UserAccount> dataSource_in, JPanel mountPanel_in) {
        super(dataSource_in, mountPanel_in);
    }
    @Override
    protected void createColumns() {
        AddColumns(
                new ColumnInfo<UserAccount>("Имя") {
                    @Override
                    public Object getFieldAt(UserAccount object) {
                        return object.name;
                    }
                },
                new ColumnInfo<UserAccount>("Почта") {
                    @Override
                    public Object getFieldAt(UserAccount object) {
                        return object.email;
                    }
                },
                new ColumnInfo<UserAccount>("Роль") {
                    @Override
                    public Object getFieldAt(UserAccount object) {
                        return object.role.getDescription();
                    }
                    @Override
                    public boolean isVisible() {
                        return Global.mainModule.getAccount().role.equals(AccountRole.Admin);
                    }
                },
                new ColumnInfo<UserAccount>("Рассылка") {
                    @Override
                    public Object getFieldAt(UserAccount object) {
                        return (object.subscribe_active == 0) ? "выключена" : "включена";
                    }
                }
        );
    }
    @Override
    protected DataMenuBar createMenuBar() {
        return new DataMenuBar(dataSource.getPluralDescription(), PassCode.SynchronizeCredentials,
                PassCode.PublishUserAccount,
                PassCode.EditUserAccount,
                PassCode.DeleteUserAccount);
    }
    @Override
    protected DBObjectDialog getDialog() {
        return new UserAccountDialog();
    }
}
