package Visual_DVM_2021.Passes;
import Common.Constants;
import Common.Global;
import Common.Utils.Utils;
import TestingSystem.Common.TestingPackage.TestingPackage;
import TestingSystem.Common.TestingServer;

import java.io.File;
import java.util.Vector;
public abstract class DeleteTestingPackages<P extends TestingPackage> extends DeleteServerObjects<TestingServer, P>{
    boolean delete_draft;
    public DeleteTestingPackages(Class<P> p) {
        super(Global.testingServer, p);
    }
    public boolean checkActivity() {
            /*
        for (Object key : target) {
            DVMPackage tasksPackage = server.db.dvmPackages.get(key);
            if (!tasksPackage.state.equals(TasksPackageState.Done) &&
                    !tasksPackage.state.equals(TasksPackageState.Aborted)
            ) {
                Log.Writeln_("Нельзя удалить активный пакет " + key + " !");
                return false;
            }
        }
             */
        return true;
    }
    protected abstract File getPackagesHome();
    @Override
    protected boolean canStart(Object... args) throws Exception {
        delete_draft = false;
        Vector<Object> target_ = new Vector<>();
        if (super.canStart(args) && checkActivity()) {
            for (Object key : target) {
                int id = (int) key;
                if (id == Constants.Nan) {
                    delete_draft = true;
                } else {
                    target_.add(id);
                }
            }
            //-
            if (delete_draft) {
                if (target.size() == 1) {
                    DeleteDraftOnly();
                    return false;
                } else {
                    target = target_;
                    return true;
                }
            }
            return true;
        }
        return false;
    }
    void DeleteDraftOnly() throws Exception {
        showPreparation();
        getDb().tables.get(d).Data.remove(Constants.Nan);
        getDb().tables.get(d).ShowUI();
    }
    @Override
    protected void performDone() throws Exception {
        super.performDone();
        for (Object object : target) {
            int id = (int) object;
            File workspace = new File(getPackagesHome(), String.valueOf(id));
            Utils.forceDeleteWithCheck(workspace);
        }
    }
}
