package Visual_DVM_2021.Passes.All;
import Common.Current;
import Common.UI.UI;
import Repository.Server.ServerCode;
import Repository.Server.ServerExchangeUnit_2021;
import SapforTestingSystem.SapforTasksPackage.SapforTasksPackage;
import TestingSystem.TasksPackage.TasksPackage;
import TestingSystem.TasksPackage.TasksPackageState;
import TestingSystem.TestingServer;
import Visual_DVM_2021.Passes.PassCode_2021;
import Visual_DVM_2021.Passes.TestingSystemPass;
import javafx.util.Pair;

import java.util.Vector;
public class ActualizePackages extends TestingSystemPass<Object> {
    boolean needsSynchronize;
    Vector<TasksPackage> dvmPackages;
    Vector<SapforTasksPackage> spfPackages;
    @Override
    protected void ServerAction() throws Exception {
        for (TasksPackage p : dvmPackages) {
            TasksPackageState oldState = p.state;
            Command(new ServerExchangeUnit_2021(ServerCode.GetAccountObjectCopyByPK, Current.getAccount().email, new Pair<>(TasksPackage.class, p.id)));
            TasksPackage actual_package = (TasksPackage) response.object;
            //-
            p.SynchronizeFields(actual_package);
            if (!oldState.equals(p.state) && p.state.equals(TasksPackageState.Done))
                needsSynchronize = true; //состояние изменилось, и на Done значит нужно скачивать задачи.
            server.account_db.Update(p);
        }
        //---
        for (SapforTasksPackage p : spfPackages) {
            TasksPackageState oldState = p.state;
            Command(new ServerExchangeUnit_2021(ServerCode.GetAccountObjectCopyByPK, Current.getAccount().email, new Pair<>(SapforTasksPackage.class, p.id)));
            SapforTasksPackage actual_package = (SapforTasksPackage) response.object;
            //-
            p.SynchronizeFields(actual_package);
            if (!oldState.equals(p.state) && p.state.equals(TasksPackageState.Done))
                needsSynchronize = true; //состояние изменилось, и на Done значит нужно скачивать задачи.
            server.account_db.Update(p);
        }
    }
    @Override
    protected boolean needsAnimation() {
        return false;
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        needsSynchronize = false;
        dvmPackages = server.account_db.getActivePackages();
        spfPackages = server.account_db.getActiveSapforPackages();
        return !dvmPackages.isEmpty() || !spfPackages.isEmpty();
    }
    @Override
    protected void performCanNotStart() throws Exception {
        TestingServer.TimerOff();
    }
    @Override
    protected void showCanNotStart() throws Exception {
        UI.getMainWindow().getTestingWindow().ShowAutoActualizeTestsState();
    }
    @Override
    protected void performPreparation() throws Exception {
        server.account_db.SaveLastSelections();
    }
    @Override
    protected void showPreparation() throws Exception {
        server.account_db.packages.ClearUI();
        server.account_db.sapforTasksPackages.ClearUI();
    }
    @Override
    protected void showDone() throws Exception {
        server.account_db.packages.ShowUI();
        server.account_db.sapforTasksPackages.ShowUI();
        //--->>
        if (!TestingServer.checkTasks) UI.getMainWindow().getTestingWindow().ShowAutoActualizeTestsState();
        server.account_db.RestoreLastSelections();
    }
    @Override
    protected void performDone() throws Exception {
        super.performDone();
        if (needsSynchronize)
            passes.get(PassCode_2021.SynchronizeTestsTasks).Do();
    }
}
