package Common.Visual;
import Common.Database.Objects.DBObject;
import Common.Database.Tables.DataSet;
import Common.MainModule_;
import Common.Utils.Utils_;
import Common.Visual.Fonts.VisualiserFonts;

import javax.swing.*;
//фильтр флаг. либо в меню, либо на баре. текста нет.
public abstract class FilterFlag<D extends DBObject> implements DBObjectFilter_<D> {
    protected AbstractButton control = null;
    protected boolean active = true; //включен ли фильтр
    protected DataSet<?, D> dataSet = null; ///источник данных
    String description; //описание фильтра
    public FilterFlag(DataSet<?, D> dataSet_in, AbstractButton control_in, boolean active_in) {
        dataSet = dataSet_in;
        active = active_in;
        control = control_in;
        description = control.getText();
        control.addActionListener(e -> {
            active = !active;
            System.out.println("active=" + active);
            Mark();
            dataSet.ShowUI();
        });
        Mark();
        control.setFont(MainModule_.instance.getUI().getTheme().Fonts.get(VisualiserFonts.TreePlain));
    }
    protected String getNotActiveIconPath() {
        return "/Common/icons/NotPick.png";
    }
    protected String getActiveIconPath() {
        return "/Common/icons/Pick.png";
    }
    public AbstractButton getControl() {
        return control;
    }
    public void Mark() {
        control.setIcon(Utils_.getIcon(active ? getActiveIconPath() : getNotActiveIconPath()));
    }
    public boolean isActive() {
        return active;
    }
    public void setActive(boolean flag) {
        active = flag;
        Mark();
    }
    protected abstract boolean validate(D object);
    @Override
    public boolean Validate(D object) {
        return !active || validate(object);
    }
}
