package _VisualDVM.TestingSystem.SAPFOR.SapforConfiguration;
import _VisualDVM.Current;
import Common.Visual.DataSetControlForm;
import Common.Visual.Tables.TableEditors;
import Common.Visual.Tables.TableRenderers;
import _VisualDVM.ServerObjectsCache.ConfigurationCache;
import _VisualDVM.ServerObjectsCache.VisualCaches;
import Common.Visual.Windows.Dialog.DBObjectDialog;
import Common.Utils.Vector_;
import Common.Database.Tables.iDBTable;
import _VisualDVM.TestingSystem.SAPFOR.SapforConfiguration.UI.SapforConfigurationFields;

import java.util.Vector;
public class SapforConfigurationDBTable extends iDBTable<SapforConfiguration> {
    public SapforConfigurationDBTable() {
        super(SapforConfiguration.class);
    }
    @Override
    public Current CurrentName() {
        return Current.SapforConfiguration;
    }
    @Override
    public String getSingleDescription() {
        return "конфигурация тестирования SAPFOR";
    }
    @Override
    public String getPluralDescription() {
        return "конфигурации";
    }
    @Override
    protected DataSetControlForm createUI() {
        return new DataSetControlForm(this) {
            @Override
            public boolean hasCheckBox() {
                return true;
            }
            @Override
            protected void AdditionalInitColumns() {
                columns.get(4).setRenderer(TableRenderers.RendererAutoConfiguration);
                columns.get(4).setEditor(TableEditors.EditorAutoConfiguration);
                columns.get(4).setMinWidth(25);
                columns.get(4).setMaxWidth(25);
                columns.get(6).setMaxWidth(500);
                columns.get(7).setRenderer(TableRenderers.RendererMultiline);
                columns.get(7).setMaxWidth(500);
            }
        };
    }
    @Override
    public String[] getUIColumnNames() {
        return new String[]{
                "имя",
                "автор",
                "",
                "ядра",
                "параметры",
                "группы",
                "тестов"
        };
    }
    @Override
    public Object getFieldAt(SapforConfiguration object, int columnIndex) {
        ConfigurationCache cache = (ConfigurationCache) VisualCaches.GetCache(object);
        switch (columnIndex) {
            case 2:
                return object.description;
            case 3:
                return object.sender_name;
            case 4:
                return object.printAuto();
            case 5:
                return object.kernels;
            case 6:
                return cache.settingsSummary;
            case 7:
                return cache.groupsSummary;
            case 8:
                return cache.getTestsCount();
            default:
                return null;
        }
    }
    //--
    @Override
    public DBObjectDialog<SapforConfiguration, SapforConfigurationFields> getDialog() {
        return new DBObjectDialog<SapforConfiguration, SapforConfigurationFields>(SapforConfigurationFields.class) {
            @Override
            public int getDefaultHeight() {
                return 200;
            }
            @Override
            public int getDefaultWidth() {
                return 450;
            }
            @Override
            public void validateFields() {
            }
            @Override
            public void fillFields() {
                fields.tfName.setText(Result.description);
                fields.sKernels.setValue(Result.kernels);
            }
            @Override
            public void ProcessResult() {
                Result.description = fields.tfName.getText();
                Result.kernels = (int) fields.sKernels.getValue();
            }
            @Override
            public void SetReadonly() {
                fields.tfName.setEnabled(false);
                fields.sTransformationMaxtime.setEnabled(false);
            }
        };
    }
    public Vector<SapforConfiguration> getAutoConfigurations() {
        Vector<SapforConfiguration> res = new Vector_<>();
        for (SapforConfiguration configuration : Data.values()) {
            if (configuration.autoTesting != 0)
                res.add(configuration);
        }
        return res;
    }
    //патч.потом удалить.
    public SapforConfiguration getConfigurationByDescription(String description){
        for (SapforConfiguration sapforConfiguration: Data.values())
            if (sapforConfiguration.description.equals(description))
                return sapforConfiguration;
        return null;
    }
}
