package _VisualDVM.Passes.All;
import Common.Utils.Utils_;
import Common.Visual.Selectable;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Passes.Sapfor.SapforTransformation;
import _VisualDVM.ProjectData.SapforData.Includes.Analysis.FileInfo;
import _VisualDVM.ProjectData.SapforData.Includes.Analysis.Include;

import java.util.Vector;
import java.util.stream.Collectors;
public class SPF_InsertIncludesPass extends SapforTransformation {
    @Override
    protected PassCode necessary() {
        return PassCode.SPF_GetIncludeDependencies;
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        if (super.canStart(args)) {
            if (target.numAddicted <= 0) {
                Log.Writeln_("Не найдено файлов, имеющих зависимости по включению.");
                return false;
            }
            Vector<String> Result = new Vector<>();
            for (FileInfo fileInfo : target.addictedFiles.values()) {
                Vector<Include> selected_children = fileInfo.dependencies.stream().filter(Selectable::isSelected).collect(Collectors.toCollection(Vector::new));
                if (!selected_children.isEmpty()) {
                    Result.add(fileInfo.file);
                    Result.add(String.valueOf(selected_children.size()));
                    for (Include di : selected_children) {
                        Result.add(String.valueOf(di.line));
                        Result.add(di.dependencyFileName);
                    }
                }
            }
            if (Result.isEmpty()) {
                Log.Writeln_("Не отмечено ни одного заголовка для подстановки");
                return false;
            }
            Options = Utils_.toU(String.join("|", Result));
            return true;
        }
        return false;
    }
    /*
    if (super.canStart(args)) {
        if (target.numAddicted <= 0) {
            Log.Writeln_("Не найдено файлов, имеющих зависимости по включению.");
            return false;
        }
        IncludesToInsertJson includes = new IncludesToInsertJson();
        for (FileInfo fileInfo : target.addictedFiles.values()) {
            Vector<Include> selected_children = fileInfo.dependencies.stream().filter(Selectable::isSelected).collect(Collectors.toCollection(Vector::new));
            if (!selected_children.isEmpty())
                for (Include di : selected_children)
                    includes.allIncludes.add(new IncludeToInsertJson(di.dependencyFileName,fileInfo.file, di.line));
        }
        if (includes.allIncludes.isEmpty()) {
            Log.Writeln_("Не отмечено ни одного заголовка для подстановки");
            return false;
        }
        Options = Utils_.gson.toJson(includes);
        return true;
    }
    return false;

     */
    @Override
    protected void FocusBeforeStart() {
        Global.mainModule.getUI().getMainWindow().getProjectWindow().FocusDependencies();
    }
}
