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

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