package _VisualDVM.Passes.Testing;
import Common.Database.Tables.iDBTable;
import Common.Utils.Pair;
import _VisualDVM.ComponentsServer.UserAccount.AccountRole;
import _VisualDVM.ComponentsServer.UserAccount.UserAccount;
import _VisualDVM.Global;
import _VisualDVM.Passes.Server.TestingServerPass;
import _VisualDVM.Repository.Server.ServerCode;
import _VisualDVM.TestingSystem.Common.TestingPackage.TestingPackage;

import java.util.Vector;
public abstract class ActualizeTestingPackages<P extends TestingPackage> extends TestingServerPass<Vector<Pair<Integer, Long>>> {
    Class p;
    public ActualizeTestingPackages(Class<P> p_in) {
        p = p_in;
    }
    iDBTable<P> getTable() {
        return (iDBTable<P>) Global.testingServer.db.getTable(p);
    }
    protected abstract ServerCode getCheckCode();
    @Override
    protected boolean requestNeedsAnimation() {
        return false;
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        UserAccount account = Global.mainModule.getAccount();
        target = new Vector<>();
        for (P testingPackage : getTable().Data.values()) {
            if (testingPackage.state.isActive()) {
                target.add(new Pair(testingPackage.id, testingPackage.ChangeDate));
            }
        }
        return (account.role.equals(AccountRole.Admin) || account.role.equals(AccountRole.Developer)) &&
                !target.isEmpty() && SendRequest(getCheckCode(), "", target);
    }
    @Override
    protected void performDone() throws Exception {
        Vector<P> res = (Vector<P>) request.server_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().getUI().RedrawControl();
    }
}
