package Common.Visual;
import Common.Database.Objects.DBObject;
import Common.Database.Tables.DataSet;
import Common.Visual.Controls.StableMenuItem;
import Common.Visual.Menus.VisualiserMenu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
public abstract class DataSetFiltersMenu<D extends DBObject> extends VisualiserMenu implements DBObjectFilter_<D> {
    //--
    protected DataSet<?, D> dataSet;
    protected Vector<FilterFlag<D>> field_filters;
    public DataSetFiltersMenu(String name, DataSet dataSet_in) {
        super(name, "/Common/icons/Filter.png", true);
        dataSet = dataSet_in;
        field_filters = new Vector<>();
        fill();
        //-
        for (FilterFlag<D> filter : field_filters)
            add(filter.getControl());
        addSeparator();
        add(new StableMenuItem("Выбрать всё", "/Common/icons/SelectAll.png") {
            {
                addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        selectAll(true);
                        dataSet.ShowUI();
                    }
                });
            }
        });
        add(new StableMenuItem("Отменить всё", "/Common/icons/UnselectAll.png") {
            {
                addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        selectAll(false);
                        dataSet.ShowUI();
                    }
                });
            }
        });
    }
    //----
    @Override
    public boolean Validate(D object) {
        if (!isActive()) {
            //считаем без учета результатов
            for (FilterFlag<D> filter : field_filters)
                filter.Validate(object);
            return true;
        }
        boolean res = false;
        for (FilterFlag<D> filter : field_filters)
            if (filter.Validate(object))
                res |= true;
        return res;
    }
    //-------
    void selectAll(boolean flag) {
        for (FilterFlag filter : field_filters)
            filter.setActive(flag);
    }
    public abstract void fill();
    @Override
    public void DropMatchesCount() {
        for (FilterFlag<D> filter : field_filters)
            filter.DropMatchesCount();
    }
    @Override
    public void ShowMatchesCount() {
        for (FilterFlag<D> filter : field_filters)
            filter.ShowMatchesCount();
    }
    public boolean isActive() {
        for (FilterFlag<D> filter : field_filters) {
            if (filter.isActive())
                return true;
        }
        return false;
    }
}
