package Visual_DVM_2021.Passes;
import Common.Database.Tables.iDBTable;
import _VisualDVM.Global;
import _VisualDVM.Repository.Server.ServerCode;
import _VisualDVM.Repository.Server.ServerExchangeUnit_2021;
import _VisualDVM.TestingSystem.Common.TestingPackage.TestingPackage;
import Visual_DVM_2021.Passes.Server.TestingSystemPass;
import javafx.util.Pair;

import java.util.Vector;
public abstract class ActualizeTestingPackages<P extends TestingPackage> extends TestingSystemPass<Vector<Pair<Integer, Long>>> {
    Class p;
    public ActualizeTestingPackages(Class<P> p_in) {
        p = p_in;
    }
    iDBTable<P> getTable() {
        return (iDBTable<P>) Global.testingServer.db.tables.get(p);
    }
    protected abstract ServerCode getCheckCode();
    @Override
    protected boolean needsAnimation() {
        return false;
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        target = new Vector<>();
        for (P testingPackage : getTable().Data.values()) {
            if (testingPackage.state.isActive()) {
                target.add(new Pair(testingPackage.id, testingPackage.ChangeDate));
            }
        }
        return !target.isEmpty();
    }
    @Override
    protected void ServerAction() throws Exception {
        Command(new ServerExchangeUnit_2021(getCheckCode(), "", target));
    }
    @Override
    protected void performDone() throws Exception {
        Vector<P> res = (Vector<P>) response.object;
        Global.testingServer.db.BeginTransaction();
        for (P actual : res)
            Global.testingServer.db.UpdateWithCheck(actual);
        Global.testingServer.db.Commit();
    }
    @Override
    protected void showDone() throws Exception {
        getTable().RefreshUI();
    }
}
