package _VisualDVM.Passes.All;
import Common.Passes.PassState;
import Common.Visual.Controls.PassControl;
import Common.Visual.UI_;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Passes.Sapfor.SilentSapforPass;
import _VisualDVM.ProjectData.Files.DBProjectFile;
public class SPF_GetGCovInfo extends SilentSapforPass {
    @Override
    public String getIconPath() {
        return "/icons/NotPick.png";
    }
    protected String getDoneIconPath() {
        return "/icons/Pick.png";
    }
    @Override
    public boolean hasStats() {
        return true;
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        if (UI_.Question("Все анализы будут сброшены.Продолжить")) {
            SPF_ParseFilesWithOrder.silent = true;
            return super.canStart(args) && Global.mainModule.getPass(PassCode.GCOV).Do() && Global.mainModule.getPass(PassCode.SPF_ParseFilesWithOrder).Do();
        }
        return false;
    }
    @Override
    protected void performFinish() throws Exception {
        SPF_ParseFilesWithOrder.silent = false;
        super.performFinish();
    }
    @Override
    protected void performPreparation() throws Exception {
        sapfor.cd(target.Home);
        target.CleanInterruptFile();
    }
    @Override
    protected void body() throws Exception {
        sapfor.RunAnalysis(
                getSapforPassName(),
                -Global.messagesServer.getPort(),
                Global.packSapforSettings(),
                target.getProjFile().getAbsolutePath());
    }
    @Override
    protected void performDone() throws Exception {
        if (!sapfor.getResult().isEmpty())
            unpack(sapfor.getResult());
    }
    protected void unpack(String packed) throws Exception {
        String[] byFiles = packed.split("@");
        for (int z = 0; z < byFiles.length; ++z) {
            DBProjectFile cur = target.db.files.get((byFiles[z++]).replace("/", "\\"));
            String[] inFile = byFiles[z].split(" ");
            for (int k = 0; k < (inFile.length / 2) * 2; k += 2) {
                int line = Integer.parseInt(inFile[k]);
                long execution = Long.parseLong(inFile[k + 1]);
                if (line > 0) {
                    int v_line = line - 1;
                    if (!cur.gcov_info.line_info.containsKey(v_line)) {
                        cur.gcov_info.add_line(v_line,
                                (execution >= 0) ? execution : 0);
                    }
                }
            }
            //завешение настроек.
            cur.gcov_info.setup();
        }
    }
    @Override
    public void Reset() {
        if (state == PassState.Done) {
            state = PassState.Inactive;
            for (PassControl control : controls)
                control.setIcon(getIconPath());
        }
    }
    @Override
    protected void showDone() throws Exception {
        if (Global.mainModule.HasFile())
            Global.mainModule.getFile().form.ShowGCOV();
        for (PassControl control : controls)
            control.setIcon(getDoneIconPath());
    }
    @Override
    protected void FocusResult() {
        if (Global.mainModule.HasFile())
            Global.mainModule.getFile().form.FocusGCOVLog();
    }
}
