package _VisualDVM.Passes.All;
import Common.MainModule_;
import Common.Utils.Utils_;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.User.User;
import _VisualDVM.GlobalData.User.UserState;
import _VisualDVM.Passes.ProcessPass;
import _VisualDVM.Utils;
import org.apache.commons.io.FileUtils;

import java.io.File;
public class LocalInitaliseUser extends ProcessPass<User> {
    @Override
    protected boolean canStart(Object... args) throws Exception {
        target = MainModule_.instance.getDb().getTable(User.class).getUI().getCurrent();
        return true;
    }
    @Override
    protected void body() throws Exception {
        File workspace = new File(Utils_.getHomeDirectory(), "User");
        target.workspace = workspace.getAbsolutePath();
        Utils_.CheckAndCleanDirectory(workspace);
        FileUtils.forceMkdir(target.getLocalProjectsDir());
        FileUtils.forceMkdir(target.getLocalModulesDir());
        //-
        if (!Utils_.isWindows()) {
            File headerCode = target.getHeaderCodeFile();
            //-
            File starterCode = target.getStarterCodeFile();
            //-
            File launcherCode = target.getLauncherCodeFile();
            //-
            Utils.CreateResourceFile(headerCode);
            Utils.CreateResourceFile(starterCode);
            Utils.CreateResourceFile(launcherCode);
            //-
            PerformScript(
                    String.join("\n",
                            "cd " + Utils_.DQuotes(target.getLocalModulesDir()),
                            "g++ starter -o starter",
                            "chmod 0777 starter"
                    ));
            PerformScript(String.join("\n",
                    "cd " + Utils_.DQuotes(target.getLocalModulesDir()),
                    "g++ launcher.cpp  -o launcher",
                    "chmod 0777 launcher"
            ));
        }
        //-
        target.state = UserState.ready_to_work;
        Global.mainModule.getDb().Update(target);
    }
}
