package com.casic.dcms.utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import androidx.core.app.ActivityCompat;

import com.casic.dcms.ui.BigImageActivity;
import com.casic.dcms.ui.CaseProcessActivity;
import com.casic.dcms.ui.DisplayMapActivity;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

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

public class OtherUtils {
    private static final String TAG = "OtherUtils";
    private static QMUITipDialog loadingDialog;

    //选图
    public static void selectPicture(Activity activity) {
        if (activity == null) {
            return;
        }
        PictureSelector.create(activity)
                .openGallery(PictureMimeType.ofImage())
                .isWeChatStyle(true)
                .isCamera(false)
                .isCompress(true)
                .compressQuality(90)
                .compressSavePath(FileUtils.getImageCompressPath())
                .imageEngine(GlideLoadEngine.createGlideEngine())
                .maxSelectNum(2)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    //拍照
    public static void takePicture(Activity activity) {
        if (activity == null) {
            return;
        }
        PictureSelector.create(activity)
                .openCamera(PictureMimeType.ofImage())
                .isCompress(true)
                .compressQuality(90)
                .compressSavePath(FileUtils.getImageCompressPath())
                .imageEngine(GlideLoadEngine.createGlideEngine())
                .maxSelectNum(1)
                .forResult(PictureConfig.REQUEST_CAMERA);
    }

    //拍照
    public static void takeVideo(Activity activity) {
        if (activity == null) {
            return;
        }
        PictureSelector.create(activity)
                .openCamera(PictureMimeType.ofVideo())
                .isCompress(true)
                .compressQuality(90)
                .compressSavePath(FileUtils.getImageCompressPath())
                .imageEngine(GlideLoadEngine.createGlideEngine())
                .maxSelectNum(1)
                .recordVideoSecond(15)
                .forResult(PictureConfig.PREVIEW_VIDEO_CODE);
    }

    public static void showLoadingDialog(Context context, String message) {
        loadingDialog = new QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord(message)
                .create();
        loadingDialog.show();
    }

    public static void dismissLoadingDialog() {
        if (loadingDialog != null) {
            if (loadingDialog.isShowing()) {
                loadingDialog.dismiss();
            }
        }
    }

    //查看地图位置
    public static void displayOnMap(Context context, String eorc, String lng, String lat) {
        Intent intent = new Intent(context, DisplayMapActivity.class);
        intent.putExtra("caseType", eorc);
        if (TextUtils.isEmpty(lng)) {
            ToastHelper.showToast("案卷经度异常", ToastHelper.ERROR);
            return;
        }
        intent.putExtra("lng", Double.parseDouble(lng));
        if (TextUtils.isEmpty(lat)) {
            ToastHelper.showToast("案卷纬度异常", ToastHelper.ERROR);
            return;
        }
        intent.putExtra("lat", Double.parseDouble(lat));
        context.startActivity(intent);
    }

    //查看大图
    public static void showBigImage(Context context, int index, ArrayList<String> imageList) {
        Intent intent = new Intent(context, BigImageActivity.class);
        intent.putExtra("index", index);
        intent.putStringArrayListExtra("images", imageList);
        context.startActivity(intent);
    }

    //查看案卷处理记录
    public static void showCaseProcess(Context context, String processId) {
        Intent intent = new Intent(context, CaseProcessActivity.class);
        intent.putExtra("processId", processId);
        context.startActivity(intent);
    }

    //获取SimSerialNumber
    @SuppressLint({"HardwareIds"})
    public static String obtainSimCardSerialNumber(Context context) {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            //Android 10改为获取Android_ID
            return Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        } else {
            TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Class<?> telephonyClass;
            try {
                telephonyClass = Class.forName(telephony.getClass().getName());
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    return "";
                }
                String imei = telephony.getDeviceId();
                if (TextUtils.isEmpty(imei)) {
                    Method m = telephonyClass.getMethod("getSimSerialNumber", int.class);
                    //主卡，卡1
                    String mainCard = (String) m.invoke(telephony, 0);
                    //副卡，卡2
                    String otherCard = (String) m.invoke(telephony, 1);
                    if (TextUtils.isEmpty(mainCard)) {
                        return otherCard;
                    } else {
                        return mainCard;
                    }
                } else {
                    return imei;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "";
    }
}
