package _VisualDVM.GlobalData.Settings;
import Common.Database.Objects.DBObject;
import Common.Utils.Utils_;
import Common.Visual.Controls.StableMenuItem;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Repository.Component.ComponentType;
import com.sun.org.glassfish.gmbal.Description;

import javax.swing.*;
import java.awt.event.ActionEvent;
public class DBSetting extends DBObject {
    @Description("PRIMARY KEY,UNIQUE")
    public SettingName Name;
    public String Value;
    public ComponentType Owner;
    @Description("IGNORE")
    public boolean Visible = true;
    @Description("IGNORE")
    public SettingType settingType = SettingType.Undefined;
    private JMenuItem menu_item = null;
    public DBSetting() {
    }
    public DBSetting(SettingName Name_,
                     Object Value_,
                     SettingType type_in,
                     ComponentType owner_in,
                     boolean Visible_in) {
        Name = Name_;
        Value = Value_.toString();
        settingType = type_in;
        Owner = owner_in;
        Visible = Visible_in;
    }
    public DBSetting(SettingName Name_, String Value_) {
        Name = Name_;
        Value = Value_;
    }
    public DBSetting(SettingName Name_,
                     Object Value_,
                     SettingType type_in,
                     ComponentType owner_in
    ) {
        this(Name_, Value_, type_in, owner_in, true);
    }
    public JMenuItem getMenuItem() {
        if (menu_item == null) {
            menu_item = new StableMenuItem(Name.getDescription());
            menu_item.addActionListener(new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Global.mainModule.getPass(PassCode.UpdateSetting).Do(Name);
                }
            });
            Mark();
        }
        return menu_item;
    }
    /*
     * булевского типа не надо. только 0 или 1
     */
    //заводить разные типы настроек не надо.
    //иначе придется ковыряться с типами и таблицами в бд
    public void Mark() {
        switch (settingType) {
            case SapforFlag:
                getMenuItem().setIcon(Utils_.getIcon(toBoolean() ? "/icons/Pick.png" : "/icons/NotPick.png"));
                break;
            case PercentField:
                getMenuItem().setText(Name.getDescription() + " : " + this + "%");
                break;
            case StringField:
                String valueToShow = Value.isEmpty() ? "не задано" : Utils_.Quotes(toString());
                getMenuItem().setText(Name.getDescription() + " : " + valueToShow);
                break;
            case IntField:
                getMenuItem().setText(Name.getDescription() + " : " + this);
                break;
        }
    }
    public int toInt32() {
        return Integer.parseInt(Value);
    }
    public boolean toBoolean() {
        return toInt32() != 0;
    }
    @Override
    public String toString() {
        return Value;
    }
    @Override
    public Object getPK() {
        return Name;
    }
}