package com.yyrh.imei;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;

import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;


import com.yyrh.constant.Constants;
import com.yyrh.constant.SDKSettings;
import com.yyrh.utils.Utils;
import com.yythird.sdk.ChannelSDK;


public class ImeiUtil {
    public static String getImei(Context context) {

        TelephonyManager telephonyManager = null;
        String imei = "";
        try {
            telephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);


            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.

                imei = Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);


                if (judgeStrNull(imei)){
                    return userSimulateImei(context);
                }else{
                    SDKSettings.ifa_type = "3";
                    return imei;
                }




            }

            if (telephonyManager.getDeviceId() != null) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    imei =  telephonyManager.getImei();
                }else{
                    imei = telephonyManager.getDeviceId();
                }
                SDKSettings.ifa_type = "1";

            } else {

                imei = Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
                SDKSettings.ifa_type = "3";

            }

            } catch (Exception e) {
                System.out.println(e.toString());
            }



            if (judgeStrNull(imei)){

                return userSimulateImei(context);


            }else{
                Utils.setSharedPreferences("yyrh","simulateImei",imei,context);
            }

            return imei;


    }

     static String userSimulateImei(Context context){

        SharedPreferences sp = context.getSharedPreferences("yyrh", Activity.MODE_PRIVATE);
        String  simulateImei = sp.getString("simulateImei", "");

        if (!judgeStrNull(simulateImei)){
            return simulateImei;
        }else {


            // 用putString的方法保存数据
            String encryptMD5to16 ="YYR" + encryptMD5to16();
            SDKSettings.ifa_type = "0";
            Utils.setSharedPreferences("yyrh","simulateImei",encryptMD5to16,context);


            return encryptMD5to16;
        }
    }
    public static boolean judgeStrNull(String str) {
        return (str == null || str.equals(""));
    }

    public static String encryptMD5to16() {
        return simulateUniqueId().substring(8, 24);

    }


    //获得独一无二的Psuedo ID
    public static String simulateUniqueId() {
        String serial = null;

        String m_szDevIDShort = "" +
                Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +

                Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +

                Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +

                Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +

                Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +

                Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +

                Build.USER.length() % 10; //13 位

        String  time = m_szDevIDShort + (System.currentTimeMillis());
        return  getMD5(time);
    }

    public static String getMD5(String content) {
        String s = null;
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            java.security.MessageDigest md = java.security.MessageDigest
                    .getInstance("MD5");
            md.update(content.getBytes());
            byte tmp[] = md.digest();
            char str[] = new char[16 * 2];
            int k = 0;
            for (int i = 0; i < 16; i++) {
                byte byte0 = tmp[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            s = new String(str);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }

    public void  getOaid(final Context context){
       ChannelSDK.getInstance().SDKGetOaid();
    }


}
