package _VisualDVM.ComponentsServer.UserAccount;
import Common.Database.Objects.DBObject;
import Common.Database.Tables.FKBehaviour;
import Common.Database.Tables.FKCurrentObjectBehaviuor;
import Common.Database.Tables.FKDataBehaviour;
import Common.Database.Tables.iDBTable;
import Common.Utils.Utils_;
import Common.Visual.DataSetControlForm;
import _VisualDVM.ComponentsServer.Recipient.Json.UserAccountJson;
import _VisualDVM.ComponentsServer.Recipient.Json.UserAccountsJson;
import _VisualDVM.ComponentsServer.SubscriberWorkspace.SubscriberWorkspace;
import _VisualDVM.ComponentsServer.UserAccount.UI.UserAccountsForm;

import javax.swing.*;
import java.util.LinkedHashMap;
import java.util.Vector;
public class UserAccountsDBTable extends iDBTable<UserAccount> {
    public UserAccountsDBTable() {
        super(UserAccount.class);
    }
    public UserAccount getByKey(String key_in) throws Exception {
        for (UserAccount userAccount : Data.values()) {
            if (userAccount.getKey().equals(key_in))
                return userAccount;
        }
        return null;
    }
    public UserAccount getByEmail(String email_in) {
        for (UserAccount userAccount : Data.values()) {
            if (userAccount.email.equals(email_in))
                return userAccount;
        }
        return null;
    }
    @Override
    protected DataSetControlForm createUI(JPanel mountPanel) {
        return new UserAccountsForm(this, mountPanel);
    }
    @Override
    public String getSingleDescription() {
        return "учётная запись";
    }
    @Override
    public String getPluralDescription() {
        return "учётные записи";
    }
    @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;
    }
    public String getPackedActiveRecipients() {
        UserAccountsJson res = new UserAccountsJson();
        Vector<String> active = new Vector<>();
        for (UserAccount userAccount : Data.values()) {
            if ((userAccount.subscribe_active != 0) && !active.contains(userAccount.email)) {
                active.add(userAccount.email);
                res.values.add(new UserAccountJson(userAccount.email, userAccount.name));
            }
        }
        return Utils_.gson.toJson(res);
    }
    public Vector<String> getActiveMails() {
        Vector<String> res = new Vector<>();
        for (UserAccount account : Data.values()) {
            if ((account.subscribe_active != 0) && !res.contains(account.email))
                res.add(account.email);
        }
        return res;
    }
}

