package _VisualDVM.TestingSystem.Common.Test;
import Common.Database.Tables.iDBTable;
import Common.Visual.DataSetControlForm;
import Common.Visual.Windows.Dialog.DBObjectDialog;
import _VisualDVM.Current;
import _VisualDVM.Global;
import _VisualDVM.TestingSystem.Common.Group.Group;
import _VisualDVM.TestingSystem.Common.Test.UI.TestFields;

import java.util.Vector;
public class TestDBTable extends iDBTable<Test> {
    public TestDBTable() {
        super(Test.class);
    }
    @Override
    public String getSingleDescription() {
        return "тест DVM";
    }
    @Override
    public String getPluralDescription() {
        return "тесты";
    }
    @Override
    protected DataSetControlForm createUI() {
        return new DataSetControlForm(this) {
            @Override
            protected void AdditionalInitColumns() {
                //columns.get(0).setVisible(false);
            }
            @Override
            public boolean hasCheckBox() {
                return true;
            }
        };
    }
    @Override
    public Object getFieldAt(Test object, int columnIndex) {
        switch (columnIndex) {
            case 2:
                return object.description;
            case 3:
                return object.min_dim;
            case 4:
                return object.max_dim;
            case 5:
                return object.getFilesForTable();
            default:
                return null;
        }
    }
    @Override
    public String[] getUIColumnNames() {
        return new String[]{
                "имя",
                "min_dim",
                "max_dim",
                "файлы"
        };
    }
    @Override
    public Current CurrentName() {
        return Current.Test;
    }
    @Override
    public DBObjectDialog<Test, TestFields> getDialog() {
        return new DBObjectDialog<Test, TestFields>(TestFields.class) {
            @Override
            public int getDefaultHeight() {
                return 200;
            }
            @Override
            public int getDefaultWidth() {
                return 400;
            }
            @Override
            public void validateFields() {
                if (!edit) {
                    if (!Global.mainModule.getGroup().language.equals(Global.mainModule.getProject().languageName))
                        Log.Writeln_("В текущую группу могут войти только тесты на языке " + Global.mainModule.getGroup().language);
                }
            }
            @Override
            public void fillFields() {
                fields.tfName.setText(Result.description);
                fields.sMinDim.setValue(Result.min_dim);
                fields.sMaxDim.setValue(Result.max_dim);
            }
            @Override
            public void ProcessResult() {
                Result.description = fields.tfName.getText();
                Result.min_dim = (int) fields.sMinDim.getValue();
                Result.max_dim = (int) fields.sMaxDim.getValue();
                if (!edit) {
                    Result.sender_name = Global.mainModule.getAccount().name;
                    Result.sender_address = Global.mainModule.getAccount().email;
                }
            }
        };
    }
    public boolean containsTestWithDescription(String description_in) {
        for (Test test : Data.values()) {
            if (test.description.equalsIgnoreCase(description_in))
                return true;
        }
        return false;
    }
    public Test getTestByDescription(int group_id_in, String description_in) {
        for (Test test : Data.values()) {
            if (test.sender_address.equals("vmk-post@yandex.ru") &&
                    (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;
    }
    @Override
    public Class getMenuBarClass() {
        return TestsMenuBar.class;
    }
}