package TestingSystem.Common;
import Common.Constants;
import Common.Database.SQLITE.SQLiteDatabase;
import TestingSystem.Common.Group.GroupsDBTable;
import TestingSystem.Common.Test.TestDBTable;
import TestingSystem.Common.TestingPackageToKill.TestingPackagesToKillDBTable;
import TestingSystem.DVM.Configuration.ConfigurationDBTable;
import TestingSystem.DVM.DVMPackage.DVMPackage;
import TestingSystem.DVM.DVMPackage.DVMPackageDBTable;
import TestingSystem.DVM.DVMTasks.DVMRunTasksSet;
import TestingSystem.SAPFOR.SapforConfiguration.SapforConfigurationDBTable;
import TestingSystem.SAPFOR.SapforConfigurationCommand.SapforConfigurationCommandsDBTable;
import TestingSystem.SAPFOR.SapforPackage.SapforPackage;
import TestingSystem.SAPFOR.SapforPackage.SapforPackageDBTable;
import TestingSystem.SAPFOR.ServerSapfor.ServerSapforsDBTable;
import Visual_DVM_2021.Passes.PassCode_2021;

import java.nio.file.Paths;
public class TestsDatabase extends SQLiteDatabase {
    public ConfigurationDBTable configurations;
    public TestDBTable tests;
    public GroupsDBTable groups;
    public DVMPackageDBTable dvmPackages;
    public SapforPackageDBTable sapforPackages;
    //--
    public TestingPackagesToKillDBTable testingPackagesToKill;
    //--
    public SapforConfigurationDBTable sapforConfigurations;
    public SapforConfigurationCommandsDBTable sapforConfigurationCommands;
    //----
    public ServerSapforsDBTable serverSapfors;
    //---
    public DVMRunTasksSet dvmRunTasks = new DVMRunTasksSet(); //задачи текущего пакета тестирования DVM
    //--
    public TestsDatabase() {
        super(Paths.get(System.getProperty("user.dir"), "Data", Constants.tests_db_name + ".sqlite").toFile());
    }
    @Override
    protected void initAllTables() throws Exception {
        addTable(configurations = new ConfigurationDBTable());
        addTable(groups = new GroupsDBTable());
        addTable(tests = new TestDBTable());
        addTable(dvmPackages = new DVMPackageDBTable());
        addTable(sapforPackages = new SapforPackageDBTable());
        addTable(testingPackagesToKill = new TestingPackagesToKillDBTable());
        //-
        addTable(sapforConfigurations = new SapforConfigurationDBTable());
        addTable(sapforConfigurationCommands = new SapforConfigurationCommandsDBTable());
        addTable(serverSapfors = new ServerSapforsDBTable());
    }
    @Override
    public PassCode_2021 getSynchronizePassCode() {
        return PassCode_2021.SynchronizeTests;
    }
    public DVMPackage getFirstActiveDVMPackage() {
        DVMPackage first_active = null;
        DVMPackage first_queued = null;
        if (!dvmPackages.Data.isEmpty()) {
            for (DVMPackage p : dvmPackages.Data.values()) {
                switch (p.state) {
                    case Done:
                    case Aborted:
                        break;
                    case Queued:
                        if (first_queued == null) first_queued = p;
                        break;
                    default:
                        if (first_active == null) first_active = p; //это и будет первый активный.
                        break;
                }
            }
            if (first_active != null) return first_active;
            if (first_queued != null) {
                first_queued.state = TasksPackageState.TestsSynchronize;
                try {
                    Update(first_queued);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            return first_queued;
        }
        return null;
    }
    public SapforPackage getFirstActiveSapforPackage() {
        SapforPackage first_active = null;
        SapforPackage first_queued = null;
        if (!sapforPackages.Data.isEmpty()) {
            for (SapforPackage p : sapforPackages.Data.values()) {
                switch (p.state) {
                    case Done:
                    case Aborted:
                        break;
                    case Queued:
                        if (first_queued == null) first_queued = p;
                        break;
                    default:
                        if (first_active == null) first_active = p; //это и будет первый активный.
                        break;
                }
            }
            if (first_active != null) return first_active;
            if (first_queued != null) {
                first_queued.state = TasksPackageState.TestsSynchronize;
                try {
                    Update(first_queued);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            return first_queued;
        }
        return null;
    }
}
