package _VisualDVM.TestingSystem.Common.Test;
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.DataSetControlForm;
import _VisualDVM.Constants;
import _VisualDVM.TestingSystem.Common.Group.Group;
import _VisualDVM.TestingSystem.Common.Test.UI.TestsForm;
import _VisualDVM.TestingSystem.Common.TestFile.TestFile;
import _VisualDVM.TestingSystem.DVM.DVMConfigurationTest.DVMConfigurationTest;
import _VisualDVM.TestingSystem.SAPFOR.SapforConfigurationTest.SapforConfigurationTest;

import javax.swing.*;
import java.util.LinkedHashMap;
import java.util.Vector;
public class TestDBTable extends iDBTable<Test> {
    public TestDBTable() {
        super(Test.class);
    }
    @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(TestFile.class, new FKBehaviour(FKDataBehaviour.DELETE, FKCurrentObjectBehaviuor.ACTIVE));
        res.put(DVMConfigurationTest.class, new FKBehaviour(FKDataBehaviour.DELETE, FKCurrentObjectBehaviuor.PASSIVE));
        res.put(SapforConfigurationTest.class, new FKBehaviour(FKDataBehaviour.DELETE, FKCurrentObjectBehaviuor.PASSIVE));
        return res;
    }
    @Override
    protected DataSetControlForm createUI(JPanel mountPanel) {
        return new TestsForm(this, mountPanel);
    }
    public boolean containsTestWithDescription(String description_in) {
        for (Test test : Data.values()) {
            if (test.description.equalsIgnoreCase(description_in))
                return true;
        }
        return false;
    }
    public Test getStandardTestByDescription(int group_id_in, String description_in) {
        for (Test test : Data.values()) {
            if (test.sender_address.equals(Constants.MailAddress) &&
                    (test.group_id == group_id_in) && (test.description.equalsIgnoreCase(description_in)))
                return test;
        }
        return null;
    }
    public Vector<Test> getSelectedGroupTests(Group group) {
        Vector<Test> allTests = new Vector<>();
        Vector<Test> selectedTests = new Vector<>();
        //--
        for (Test test : Data.values()) {
            if (test.group_id == group.id) {
                allTests.add(test);
                if (test.isSelected()) selectedTests.add(test);
            }
        }
        return selectedTests.isEmpty() ? allTests : selectedTests;
    }
}