package _VisualDVM.Passes.All;
import Common.Passes.Pass;
import Common.Passes.PassException;
import Common.Utils.Utils_;
import _VisualDVM.Current;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.Machine.Machine;
import _VisualDVM.GlobalData.Makefile.Makefile;
import _VisualDVM.GlobalData.Tasks.CompilationTask.CompilationTask;
import _VisualDVM.GlobalData.Tasks.TaskState;
import _VisualDVM.GlobalData.User.User;
import _VisualDVM.GlobalData.User.UserState;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.ProjectData.LanguageName;
import _VisualDVM.ProjectData.Project.db_project_info;
public class Compile extends Pass<db_project_info> {
    Pass subpass = null;
    CompilationTask compilationTask = null;
    Machine machine = null;
    User user = null;
    Makefile makefile = null;
    @Override
    protected PassCode necessary() {
        return Global.mainModule.getProject().languageName.equals(LanguageName.fortran) ? PassCode.SPF_ParseFilesWithOrder : null;
    }
    @Override
    public String getIconPath() {
        return "/Common/icons/BlueStart.png";
    }
    @Override
    public String getButtonText() {
        return "";
    }
    @Override
    protected boolean canStart(Object... args) {
        if (Global.mainModule.Check(Log, Current.Project) && Global.mainModule.getDb().CheckCurrent(Log, Machine.class, User.class, Makefile.class)) {
            target = Global.mainModule.getProject();
            //--
            machine = Global.mainModule.getDb().getTable(Machine.class).getUI().getCurrent();
            user = Global.mainModule.getDb().getTable(User.class).getUI().getCurrent();
            makefile = Global.mainModule.getDb().getTable(Makefile.class).getUI().getCurrent();
            //--
            subpass = null;
            compilationTask = null;
            if (user.state != UserState.ready_to_work)
                Log.Writeln_("Пользователь " +
                        Utils_.Brackets(
                                user.login
                        ) +
                        " не проинициализирован\nПерейдите на вкладку 'Настройки компиляции и запуска',\n" +
                        " и выполните команду 'Инициализация пользователя'\n");
            makefile.Validate(Log);
            return Log.isEmpty();
        }
        return false;
    }
    @Override
    protected void performPreparation() throws Exception {
        compilationTask = new CompilationTask();
        compilationTask.machine_id = machine.id;
        compilationTask.user_id = user.id;
        compilationTask.makefile_id = makefile.id;
        compilationTask.project_path = target.Home.getAbsolutePath();
        compilationTask.project_description = target.description;
        //------------------------------------------
        compilationTask.CompleteSummary(target.compilation_maxtime);
        compilationTask.state = TaskState.Inactive;
        Global.mainModule.getDb().Insert(compilationTask);
        Utils_.forceDeleteWithCheck(compilationTask.getLocalWorkspace());
    }
    @Override
    protected void showPreparation() throws Exception {
        Global.mainModule.getDb().compilationTasks.ShowUI(compilationTask.getPK());
    }
    @Override
    protected void body() throws Exception {
        switch (machine.type) {
            case Local:
                if (Utils_.isWindows()) {
                    subpass = Global.mainModule.getPass(PassCode.WindowsLocalCompilation);
                } else
                    subpass = Global.mainModule.getPass(PassCode.LinuxLocalCompilation);
                break;
            case Undefined:
            case MVS_cluster:
                throw new PassException("Компиляция не реализована для типа машины " +
                        Utils_.DQuotes(machine.type));
            default:
                subpass = Global.mainModule.getPass(PassCode.RemoteCompilation);
                break;
        }
        subpass.Do(compilationTask, Global.mainModule.getProject());
    }
    @Override
    protected boolean validate() {
        return (subpass != null) && subpass.isDone();
    }
    @Override
    protected void showFinish() throws Exception {
        Global.mainModule.getDb().compilationTasks.ShowUI(compilationTask.getPK());
        Global.mainModule.getUI().getDebugWindow().ShowLastCompilationTask();
    }
}
