package _VisualDVM.Passes.All;
import Common.Passes.PassState;
import Common.Utils.Utils_;
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;
import _VisualDVM.ProjectData.SapforData.GCOV.FileGCOVJson;
import _VisualDVM.ProjectData.SapforData.GCOV.GCOVJson;
import _VisualDVM.ProjectData.SapforData.GCOV.LineGCOVJson;
public class SPF_GetGCovInfo extends SilentSapforPass {
    @Override
    public String getIconPath() {
        return "/Common/icons/NotPick.png";
    }
    protected String getDoneIconPath() {
        return "/Common/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(),
                target.sapforProperties.pack(),
                target.getProjFile().getAbsolutePath());
    }
    @Override
    protected void performDone() throws Exception {
        if (!sapfor.getResult().isEmpty())
            unpack(sapfor.getResult());
    }
    protected void unpack(String packed) throws Exception {
        GCOVJson allJson = Utils_.gson.fromJson(packed, GCOVJson.class);
        for (FileGCOVJson fileJson : allJson.allGCov) {
            fileJson.file = Utils_.toW(fileJson.file);
            //--
            DBProjectFile projectFile = target.db.files.get(fileJson.file);
            for (LineGCOVJson lineJson : fileJson.lines) {
                if (lineJson.line > 0) {
                    int v_line = lineJson.line - 1;
                    long v_execution = (lineJson.execution >= 0) ? lineJson.execution : 0;
                    if (!projectFile.gcov_info.line_info.containsKey(v_line)) {
                        projectFile.gcov_info.add_line(v_line, v_execution);
                    }
                }
            }
            //завешение настроек.
            projectFile.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();
    }
    @Override
    protected boolean isGoodCode() {
        return sapfor.getErrorCode() > 0;
    }
}
