package com.virjar.g4proxy.utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.virjar.g4proxy.Constant;
import com.virjar.g4proxy.MainActivity;

public class DeviceInfoHolder {


    private static String imei;

    private static String androidId;

    public static void initDeviceInfo(MainActivity activity) {
        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_FINE_LOCATION)) {
                Log.e(Constant.TAG, "checkSelfPermission ACCESS_FINE_LOCATION did not show.");
            } else {
                ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, AccessAuthority.ACCESS_FINE_LOCATION_REQUEST_CODE.getRequestCode());
            }
            return;
        }

        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                Log.e(Constant.TAG, "checkSelfPermission WRITE_EXTERNAL_STORAGE did not show.");
            } else {
                ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, AccessAuthority.WRITE_EXTERNAL_STORAGE_REQUEST_CODE.getRequestCode());
            }
            return;
        }

        if (restoreInfo(activity)) {
            return;
        }

        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_PHONE_STATE)) {
                Log.e(Constant.TAG, "checkSelfPermission READ_PHONE_STATE did not show.");
            } else {
                ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, AccessAuthority.READ_PHONE_STATE_REQUEST_CODE.getRequestCode());
            }
            return;
        }
        try {
            TelephonyManager telephonyManager = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
            if (telephonyManager == null) {
                Log.e(Constant.TAG, "can not get telephonyManager instance");
                return;
            }
            @SuppressLint("HardwareIds") String imeiCopy = telephonyManager.getDeviceId();
            if (!TextUtils.isEmpty(imeiCopy)) {
                imei = imeiCopy;
            }

            androidId = Settings.System.getString(activity.getContentResolver(), Settings.Secure.ANDROID_ID);

            save(activity);
        } catch (Exception e) {
            Log.e(Constant.TAG, "getIMEI failed!", e);
        }
    }

    private static boolean restoreInfo(Context context) {

        if (imei != null && androidId != null) {
            return true;
        }

        SharedPreferences sharedPreferences = context.getSharedPreferences(Constant.sharedPreferenceFile, Context.MODE_PRIVATE);

        String imei = sharedPreferences.getString("imei", null);
        if (imei == null) {
            return false;
        }
        DeviceInfoHolder.imei = imei;

        String androidId = sharedPreferences.getString("androidId", null);
        if (androidId == null) {
            return false;
        }

        DeviceInfoHolder.androidId = androidId;
        return true;

    }

    private static void save(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(Constant.sharedPreferenceFile, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();

        edit.putString("imei", imei);
        edit.putString("androidId", androidId);
        edit.apply();
    }

    public static String getCachedImei(Context context) {
        restoreInfo(context);
        return imei;
    }

    public static String getCachedAndroidId(Context context) {
        restoreInfo(context);
        return androidId;
    }
}
