package com.gem.media.splash.base.crash;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.util.Log;

import com.gem.media.splash.base.utils.ALog;
import com.or.ange.dot.utils.ThreadManager;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class CrashHandler implements Thread.UncaughtExceptionHandler {

    private static final String TAG = "CrashHandler";
    private static final String DEX_EX = "dx_crs_ex";
    private Thread.UncaughtExceptionHandler defUncaughtExceptionHandler;
    private volatile boolean isInit = false;

    private String appId;

    private String appVersionName;
    private String appVersionCode;

    private String reportUrl;

    private final CrashReportRunnable carshReportRunnable;

    private static class Holder {
        private static final CrashHandler INSTANCE = new CrashHandler();
    }

    private CrashHandler() {
        carshReportRunnable = new CrashReportRunnable();
    }

    public static CrashHandler getInstance() {
        return Holder.INSTANCE;
    }



    public void setup(Context context, String url, String appId) {
        if (isInit) {
            return;
        }
        isInit = true;

        this.appId = appId;

        appVersionName = getAppVersionName(context);
        appVersionCode = getAppVersionCode(context);

        this.reportUrl = url;
        try {

            defUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
            Thread.setDefaultUncaughtExceptionHandler(this);
        } catch (Exception e) {
            //
        }
    }

    @Override
    public void uncaughtException(Thread thread, Throwable throwable) {
        String exception = Log.getStackTraceString(throwable);
        ALog.d(TAG, "exception---" + exception);
        Map<String, String> map = new HashMap<>();
        map.put("ex", exception);
        Event event = generateEvent(DEX_EX, map);
        carshReportRunnable.setEvent(event);
        carshReportRunnable.setAppId(appId);
        carshReportRunnable.setUrl(reportUrl);
        ThreadManager.getNormalExecutor().submit(carshReportRunnable);
//            defUncaughtExceptionHandler.uncaughtException(thread, throwable);
    }


    private Event generateEvent(String eventId, Map<String, String> params) {
        Event event = new Event();
        event.setEventId(eventId);
        event.setVersionName(appVersionName);
        event.setVersionCode(appVersionCode);
        event.setTimestamp(System.currentTimeMillis());
        if (params != null) {
            event.setParams(new JSONObject(params).toString());
        }
        return event;
    }

    private static String getAppVersionName(Context context2) {
        try {
            PackageInfo pi = context2.getPackageManager().getPackageInfo(context2.getPackageName(), 0);
            if (pi != null) {
                return pi.versionName;
            }
            return "error:version";
        } catch (Exception e) {
            e.printStackTrace();
            return "error:version";
        }
    }

    private static String getAppVersionCode(Context context2) {
        try {
            PackageInfo pi = context2.getPackageManager().getPackageInfo(context2.getPackageName(), 0);
            if (pi != null) {
                return String.valueOf(pi.versionCode);
            }
            return "-1";
        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }
    }

}
