package _VisualDVM.Passes.Testing;
import Common.CommonConstants;
import Common.Utils.Utils_;
import _VisualDVM.Global;
import _VisualDVM.Passes.Server.DeleteServerObjects;
import _VisualDVM.TestingSystem.Common.TestingPackage.TestingPackage;
import _VisualDVM.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() {
        /* //todo
        for (Object key : target) {
            TestingPackage 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 == CommonConstants.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().getTable(d).Data.remove(CommonConstants.Nan);
        getDb().getTable(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);
        }
    }
}
