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

import android.content.Context;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.util.Locale;

public final class CountryUtil {
    private static final String TAG = "CountryUtil";

    CountryUtil() {
    }

    public static String getCountryCode(Context context) {
        String countryCode = "";
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService("phone");
            if (telephonyManager != null) {
                countryCode = telephonyManager.getSimCountryIso();
                if (countryCode != null && countryCode.length() == 2) {
                    return countryCode.toUpperCase(Locale.US);
                }
                if (telephonyManager.getPhoneType() != 2) {
                    countryCode = telephonyManager.getNetworkCountryIso();
                    if (countryCode != null && countryCode.length() == 2) {
                        return countryCode.toUpperCase(Locale.US);
                    }
                }
            }
        } catch (Exception e) {
        }
        return countryCode;
    }

    public static String getSimCountryIso(Context context) {
        String countryCode = "";
        try {
            countryCode = ((TelephonyManager) context.getSystemService("phone")).getSimCountryIso();
            if (countryCode != null && countryCode.length() == 2) {
                return countryCode.toUpperCase(Locale.US);
            }
        } catch (Exception e) {
        }
        return countryCode;
    }

    public static String getNetworkCountryIso(Context context) {
        String countryCode = "";
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService("phone");
            if (telephonyManager.getPhoneType() != 2) {
                countryCode = telephonyManager.getNetworkCountryIso();
                if (countryCode != null && countryCode.length() == 2) {
                    return countryCode.toUpperCase(Locale.US);
                }
            }
        } catch (Exception e) {
        }
        return countryCode;
    }

    public static String getLocalCountry(Context context) {
        String countryCode = "";
        try {
            if (Build.VERSION.SDK_INT >= 24) {
                countryCode = context.getResources().getConfiguration().getLocales().get(0).getCountry();
            } else {
                countryCode = context.getResources().getConfiguration().locale.getCountry();
            }
        } catch (Exception e) {
        }
        return countryCode;
    }

    public static String getSimOperatorMcc(Context context) {
        try {
            String operator = ((TelephonyManager) context.getApplicationContext().getSystemService("phone")).getSimOperator();
            if (TextUtils.isEmpty(operator)) {
                return "";
            }
            return operator.substring(0, 3);
        } catch (Exception e) {
            return "";
        }
    }
}
