package com.bytedance.apm.agent.tracing;

import X.0HH;
import X.80W;
import X.Q39;
import com.bytedance.apm.agent.logging.AgentLog;
import com.bytedance.apm.agent.logging.AgentLogManager;
import com.bytedance.covode.number.Covode;
import java.util.HashSet;
import java.util.Stack;
import org.json.JSONObject;

public class TraceMachine {
    public static AgentLog log = AgentLogManager.getAgentLog();
    public static HashSet<String> sMethodSet = new HashSet<>(32);
    public static ThreadLocal<Stack<Trace>> traceStackThreadLocal = new ThreadLocal<>();

    public static void exitMethod() {
        exitMethod(Q39.LIZJ);
    }

    static {
        Covode.recordClassIndex(23100);
    }

    public static void exitMethod(String str) {
        if (traceStackThreadLocal.get() != null) {
            Stack stack = traceStackThreadLocal.get();
            if (!stack.isEmpty()) {
                Trace trace = (Trace) stack.pop();
                trace.setExitTime(System.currentTimeMillis());
                trace.setExtraData(str);
                handleCost(trace);
            }
        }
    }

    public static void handleCost(Trace trace) {
        boolean z;
        try {
            long j = trace.entryTime;
            long j2 = trace.exitTime;
            long longValue = Long.valueOf(j2 - j).longValue();
            if (longValue > 0) {
                JSONObject jSONObject = new JSONObject();
                jSONObject.put("begin_time", j);
                jSONObject.put("end_time", j2);
                JSONObject jSONObject2 = new JSONObject();
                if ("page_load".equals(trace.traceType)) {
                    jSONObject2.put(trace.methodName, longValue);
                } else {
                    jSONObject2.put(trace.className + "#" + trace.methodName, longValue);
                }
                JSONObject jSONObject3 = new JSONObject();
                if ("page_load".equals(trace.traceType)) {
                    jSONObject3.put("scene", trace.scene);
                    HashSet<String> hashSet = sMethodSet;
                    if (!hashSet.contains(trace.className + trace.methodName)) {
                        z = true;
                    } else {
                        z = false;
                    }
                    jSONObject3.put("is_first", z);
                    HashSet<String> hashSet2 = sMethodSet;
                    hashSet2.add(trace.className + trace.methodName);
                } else {
                    jSONObject3.put("is_main", 80W.LIZ());
                }
                jSONObject.put("extra_data", trace.extraData);
            }
        } catch (Exception e) {
            0HH.LIZ(e);
        }
    }

    public static void enterMethod(String str, String str2, String str3) {
        enterMethod(str, str, str2, str3);
    }

    public static void enterMethod(String str, String str2, String str3, String str4) {
        String str5 = str;
        Trace trace = new Trace(str5, str2, str3, System.currentTimeMillis(), str4);
        if (traceStackThreadLocal.get() == null) {
            Stack stack = new Stack();
            stack.push(trace);
            traceStackThreadLocal.set(stack);
            return;
        }
        traceStackThreadLocal.get().push(trace);
    }
}
