package com.secspace.lib.common.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class SimUtil {
    private static final String TAG = "SimUtil";
    private static final List<String> TELECOM_NUMERICS = new ArrayList<String>() {
        {
            add("46003");
            add("46005");
            add("46011");
        }
    };
    private static final List<String> UNICOM_NUMERICS = new ArrayList<String>() {
        {
            add("46001");
            add("46006");
            add("46009");
        }
    };
    private static final List<String> MOBILE_NUMERICS = new ArrayList<String>() {
        {
            add("46000");
            add("46002");
            add("46004");
            add("46007");
            add("46008");
        }
    };

    public static boolean isTelecom(String numeric) {
        if (TELECOM_NUMERICS.contains(numeric)) {
            return true;
        }
        return false;
    }

    public static boolean isUnicom(String numeric) {
        if (UNICOM_NUMERICS.contains(numeric)) {
            return true;
        }
        return false;
    }

    public static boolean isMobile(String numeric) {
        if (MOBILE_NUMERICS.contains(numeric)) {
            return true;
        }
        return false;
    }


    /**
     * 返回460035861141177  getSimOperator()方法如果是电信返回的46011 ,该方法比较准确
     *
     * @param context
     * @return
     */
    @SuppressLint("MissingPermission")
    public static String getSubscriberId(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getSubscriberId();
    }

    /**
     * 获取指定卡槽的stubId
     * 返回460035861141177(电信),460022224344409（移动）
     *
     * @param context
     * @param subId
     * @return
     */
    public static String getSubscriberId(Context context, int subId) {
        String result = null;
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Class<?> aClass = Class.forName(TelephonyManager.class.getName());
            Method getSimOperator = aClass.getMethod("getSubscriberId", new Class[]{int.class});
            result = (String) getSimOperator.invoke(telephonyManager, new Object[]{subId});
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取卡槽的numeric
     */
    public static String getSimNumeric(Context context) {
        String numeric = getSubscriberId(context);
        if (TextUtils.isEmpty(numeric)) {
            return numeric;
        }
        return numeric.substring(0, 5);
    }

    public static String getSimNumeric(Context context, int subId) {
        String numeric = getSubscriberId(context, subId);
        if (TextUtils.isEmpty(numeric)) {
            return numeric;
        }
        return numeric.substring(0, 5);
    }


    /**
     * 返回的是默认卡槽的numeric
     *
     * @param context
     * @return
     */
    @Deprecated
    public static String getSimOperatorNumeric(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String simOperator = telephonyManager.getSimOperator();
        return simOperator;
    }


    /**
     * 获取指定卡槽的numeric
     *
     * @param context
     * @param subId
     * @return
     */
    @Deprecated
    public static String getSimOperatorNumeric(Context context, int subId) {
        String simOperator = null;
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Class<?> aClass = Class.forName(TelephonyManager.class.getName());
            Method getSimOperator = aClass.getMethod("getSimOperator", new Class[]{int.class});
            simOperator = (String) getSimOperator.invoke(telephonyManager, new Object[]{subId});
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return simOperator;
    }

    /**
     * 判断是否包含SIM卡
     *
     * @return 状态
     */
    public static boolean isSimAvailable(Context context) {
        TelephonyManager telMgr = (TelephonyManager)
                context.getSystemService(Context.TELEPHONY_SERVICE);
        int simState = telMgr.getSimState();
        boolean result = false;
        switch (simState) {
            case TelephonyManager.SIM_STATE_READY:
                result = true;
                break;
        }
        return result;
    }

    /**
     * 判断SIM卡是否可用
     *
     * @return 状态
     */
    public static boolean isSimAvailable(Context context, int slot) {
        boolean available = false;
        int simState = TelephonyManager.SIM_STATE_UNKNOWN;
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (tm != null) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                simState = tm.getSimState(slot);
            }
            switch (simState) {
                case TelephonyManager.SIM_STATE_READY:
                    available = true;
                    break;
            }
        }
        Log.i(TAG, String.format("isSimAvailable: slot(%d) simState(%d) available(%s)", slot, simState, available));
        return available;
    }

    /**
     * 两张SIM卡可用
     *
     * @return
     */
    public static boolean isTwoSimAllReady(Context context) {
        return isSimAvailable(context, 0) && isSimAvailable(context, 1);
    }
}
