package _VisualDVM.TestingSystem.Common.Group;
import Common.Visual.UI_;
import _VisualDVM.Current;
import Common.Visual.DataSetControlForm;
import Common.Visual.Windows.Dialog.DBObjectDialog;
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.Visual.DBObjectFilter;
import Common.Visual.DataSetFilter;
import _VisualDVM.ProjectData.LanguageName;
import _VisualDVM.TestingSystem.Common.Group.UI.GroupFields;
import _VisualDVM.TestingSystem.Common.Test.Test;
import _VisualDVM.TestingSystem.Common.Test.TestType;

import java.util.LinkedHashMap;
//-
public class GroupsDBTable extends iDBTable<Group> {
    public static boolean filterMyOnly = false;
    public GroupsDBTable() {
        super(Group.class);
    }
    //------------------------------------------------>>>
    @Override
    protected void createFilters() {
        filters.add(new Common.Visual.DataSetFilter<Group>("Тип", this) {
            @Override
            public void fill() {
                for (TestType type : TestType.values())
                    field_filters.add(new Common.Visual.DBObjectFilter<Group>(dataSet, type.getDescription()) {
                        @Override
                        protected boolean validate(Group object) {
                            return object.type.equals(type);
                        }
                    });
            }
        });
        filters.add(new DataSetFilter<Group>("Язык", this) {
            @Override
            public void fill() {
                for (LanguageName languageName : LanguageName.values()) {
                    field_filters.add(new DBObjectFilter<Group>(dataSet, languageName.getDescription()) {
                        @Override
                        protected boolean validate(Group object) {
                            return object.language.equals(languageName);
                        }
                    });
                }
            }
        });
    }

    @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(Test.class, new FKBehaviour(FKDataBehaviour.DELETE, FKCurrentObjectBehaviuor.ACTIVE));
        return res;
    }
    @Override
    protected DataSetControlForm createUI() {
        return new DataSetControlForm(this) {
            @Override
            public boolean hasCheckBox() {
                return true;
            }
        };
    }
    @Override
    public String[] getUIColumnNames() {
        return new String[]{
                "имя",
                "автор",
                "тип",
                "язык"
        };
    }
    @Override
    public Object getFieldAt(Group object, int columnIndex) {
        switch (columnIndex) {
            case 2:
                return object.description;
            case 3:
                return object.sender_name;
            case 4:
                return object.type.getDescription();
            case 5:
                return object.language.getDescription();
            default:
                return null;
        }
    }
    @Override
    public Current CurrentName() {
        return Current.Group;
    }
    @Override
    public DBObjectDialog<Group, GroupFields> getDialog() {
        return new DBObjectDialog<Group, GroupFields>(GroupFields.class) {
            @Override
            public int getDefaultHeight() {
                return 250;
            }
            @Override
            public int getDefaultWidth() {
                return 400;
            }
            @Override
            public void validateFields() {
            }
            @Override
            public void fillFields() {
                fields.tfName.setText(Result.description);
                UI_.TrySelect(fields.cbType, Result.type);
                UI_.TrySelect(fields.cbLanguage, Result.language);
            }
            @Override
            public void ProcessResult() {
                Result.description = fields.tfName.getText();
                Result.type = (TestType) fields.cbType.getSelectedItem();
                Result.language = (LanguageName) fields.cbLanguage.getSelectedItem();
            }
        };
    }
    public boolean containsGroupWithDescription(String description_in) {
        for (Group group : Data.values()) {
            if (group.description.equalsIgnoreCase(description_in))
                return true;
        }
        return false;
    }
    public Group getGroupByDescription(LanguageName language_in,String description_in) {
        for (Group group : Data.values()) {
            if (group.sender_address.equals("vmk-post@yandex.ru")&&
                    group.language.equals(language_in)&&group.description.equalsIgnoreCase(description_in))
                return group;
        }
        return null;
    }
}
