package _VisualDVM.Passes.All;
import Common.Passes.Pass;
import Common.Utils.Utils_;
import Common.Visual.UI;
import _VisualDVM.Current;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.ProjectData.Project.db_project_info;
public class DeleteVersion extends Pass<db_project_info> {
    db_project_info parent;
    boolean current;
    @Override
    public String getIconPath() {
        return "/Common/icons/Delete.png";
    }
    @Override
    protected boolean canStart(Object... args) {
        if (args.length > 0) {
            target = (db_project_info) args[0];
            current = (Global.mainModule.getVersion() != null) && Global.mainModule.getVersion().Home.equals(target.Home);
            return true;
        } else {
            if (((target = Global.mainModule.getVersion()) != null) && (!UI.isActive() ||
                    UI.Warning("Удалить " +
                            ((Global.mainModule.HasProject() && target.Home.equals(Global.mainModule.getProject().Home)) ? "текущий проект" : "версию ")
                            + Utils_.Brackets(target.name)))) {
                current = true;
                return true;
            }
        }
        return false;
    }
    @Override
    protected void performPreparation() throws Exception {
        if (target.parent != null)
            target.parent.checkLastModification(target);
        if (Global.mainModule.HasProject()) {
            if ((Global.mainModule.getProject().Home.getAbsolutePath().startsWith(target.Home.getAbsolutePath())))
                Global.mainModule.getPass(PassCode.CloseCurrentProject).Do();
        }
        if (UI.isActive()) {
            Global.mainModule.getUI().getVersionsWindow().RemoveVersionFromComparison(target);
        }
    }
    @Override
    protected void body() throws Exception {
        Utils_.forceDeleteWithCheck(target.Home);
    }
    @Override
    protected void performDone() throws Exception {
        parent = target.parent;
        if (current)
            Global.mainModule.set(Current.Version, null);
        if (parent != null) {
            Global.mainModule.getUI().getVersionsWindow().getVersionsForm().getTree().RemoveNode(target.node);
            parent.versions.remove(target.name);
        } else
            Global.mainModule.set(Current.Root, null);
    }
}
