package com.ym.zxhcsdk;

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

import java.net.URLEncoder;


public class AppliContext {

    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.SDK_INT + "";
                    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_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);
                    ZXHCConstant.device_geo_lat = PhoneUtils.getInstance().getLocationInfo(context, 2);
                    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 "";
    }
}