package com.ym.zxhcsdk;

import android.app.Activity;
import android.content.Context;
import android.content.IntentFilter;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebView;

import java.net.URLEncoder;


public class HcAppliContext {

    public static Context mAppContext;
    private static AppInstallReceive installedReceiver;

    public static void init(Activity context) {
        if (mAppContext == null) {
            mAppContext = context.getApplicationContext();
            context.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ZXHCConstant.app_name = PhoneUtils.getInstance().getAppName(context);
                    ZXHCConstant.app_package = PhoneUtils.getInstance().getAppPackage();
                    ZXHCConstant.app_version = PhoneUtils.getInstance().getAppVersionName(context);
                    ZXHCConstant.device_imei = PhoneUtils.getInstance().getImei(context);
                    ZXHCConstant.device_adid = PhoneUtils.getInstance().getAndroidID(context);
                    ZXHCConstant.device_mac = PhoneUtils.getInstance().getMac(context);
                    ZXHCConstant.device_height = PhoneUtils.getInstance().getScreenHeight(context) + "";
                    ZXHCConstant.device_width = PhoneUtils.getInstance().getScreenWidth(context) + "";
                    ZXHCConstant.device_imsi = PhoneUtils.getInstance().getIMSI(context);
                    ZXHCConstant.device_network = PhoneUtils.getInstance().getNetwork(context);
                    ZXHCConstant.device_os = "Android";
                    ZXHCConstant.device_os_version = Build.VERSION.RELEASE + ".0.0";
                    ZXHCConstant.device_density = PhoneUtils.getInstance().getScreenDensity(context) + "";
                    ZXHCConstant.device_ppi = PhoneUtils.getInstance().getScreenDpi(context) + "";
                    ZXHCConstant.screen_size = PhoneUtils.getInstance().getScreenSize(context) + "";

                    ZXHCConstant.device_ip = PhoneUtils.getInstance().getIPAddress(false) + "";
                    ZXHCConstant.device_report_ua =PhoneUtils.getInstance().getUserAgent(context);
                    ZXHCConstant.device_ua =toURLEncoded(PhoneUtils.getInstance().getUserAgent(context));
//                    ZXHCConstant.device_ua =toURLEncoded(new WebView(context).getSettings().getUserAgentString());
                    ZXHCConstant.device_type = "0";
                    ZXHCConstant.is_mobile = "1";
                    ZXHCConstant.device_model = PhoneUtils.getInstance().getModel();
                    ZXHCConstant.device_brand = Build.BRAND;
                    ZXHCConstant.device_geo_lon= PhoneUtils.getInstance().getLocationInfo(context, 1);
                    if (TextUtils.isEmpty(ZXHCConstant.device_geo_lon)){
                        ZXHCConstant.device_geo_lon="-999";
                    }
                    ZXHCConstant.device_geo_lat = PhoneUtils.getInstance().getLocationInfo(context, 2);
                    if (TextUtils.isEmpty(ZXHCConstant.device_geo_lat)){
                        ZXHCConstant.device_geo_lat="-999";
                    }
                    ZXHCConstant.device_geo_city = PhoneUtils.getInstance().getLocationInfo(context, 3);
                    installedReceiver = new AppInstallReceive();
                    IntentFilter filter = new IntentFilter();
                    filter.addAction("android.intent.action.PACKAGE_ADDED");
                    filter.addAction("android.intent.action.PACKAGE_REMOVED");
                    filter.addDataScheme("package");
                    context.registerReceiver(installedReceiver, filter);
                }
            });

        } else {
            //throw new IllegalStateException("set context duplicate");
        }
    }

    public static Context get() {
        if (mAppContext == null) {
            throw new IllegalStateException("forget init?");
        } else {
            return mAppContext;
        }
    }

    public static String toURLEncoded(String paramString) {
        if (paramString == null || paramString.equals("")) {
            return "";
        }
        try
        {
            String str = new String(paramString.getBytes(), "UTF-8");
            str = URLEncoder.encode(str, "UTF-8");
            return str;
        }
        catch (Exception localException)
        {
        }

        return "";
    }
}