package Visual_DVM_2021.Passes.All;
import Common.Utils.Utils_;
import _VisualDVM.Global;
import _VisualDVM.Visual.UI;
import _VisualDVM.GlobalData.Settings.SettingName;
import _VisualDVM.ProjectData.SapforData.Functions.FuncCall;
import _VisualDVM.ProjectData.SapforData.Functions.FuncInfo;
import Visual_DVM_2021.Passes.PassCode;
import Visual_DVM_2021.Passes.SapforTransformation;

import java.util.Vector;
public class SPF_InlineProcedures extends SapforTransformation {
    @Override
    protected PassCode necessary() {
        return PassCode.SPF_GetGraphFunctions;
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        if (super.canStart(args)) {
            Vector<String> Result = new Vector<>();
            for (FuncInfo fi : target.allFunctions.values()) {
                Vector<FuncCall> selected_children = new Vector<>();
                for (String calls_file : fi.own_calls.keySet()) {
                    for (FuncCall call : fi.own_calls.get(calls_file)) {
                        if (call.isSelected())
                            selected_children.add(call);
                    }
                }
                if (!selected_children.isEmpty()) {
                    Result.add(fi.funcName);
                    Result.add(String.valueOf(selected_children.size()));
                    for (FuncCall fc : selected_children) {
                        Result.add(fc.file);
                        Result.add(String.valueOf(fc.parent_offset));
                    }
                }
            }
            if (Result.isEmpty()) {
                Log.Writeln_(
                        (callsCount > 1) ? "Не отмечено ни одной точки вызова процедур для подстановки" :
                                "Для точечной подстановки следует отметить\nнужные точки вызовов процедур в дереве."
                );
                return false;
            }
            Options = Utils_.toU(String.join("|", Result));
            Global.changeSetting(SettingName.PARSE_FOR_INLINE, "1");
            SPF_ParseFilesWithOrder.silent = true;
            return  Global.mainModule.getPass(PassCode.SPF_ParseFilesWithOrder).Do(false);
        }
        return false;
    }
    @Override
    protected void FocusBeforeStart() {
        UI.getMainWindow().getProjectWindow().FocusPoints();
    }
    @Override
    protected void performFinish() throws Exception {
        SPF_ParseFilesWithOrder.silent = false;
        super.performFinish();
    }
}
