package com.wisewoods.base.utils;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.FragmentActivity;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.permissionx.guolindev.PermissionX;

import java.util.ArrayList;

public class $ {

    public static String getSp(String key) {
        return SPStaticUtils.getString(key);
    }


    public static boolean spNoNull(String key) {
        return !TextUtils.isEmpty(getSp(key));
    }


    public static void toast(String msg) {
        ToastUtils.showShort(msg);
    }

    public static void fixStatusBarView(View view) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = BarUtils.getStatusBarHeight();
        view.setLayoutParams(layoutParams);
    }

    public static void mockNet(Context context, Runnable r) {
        mockNet(context, r, true);
    }

    public static void mockNet(Context context, Runnable r, boolean isShowDialog) {
        Dialog dialog = null;
        if (isShowDialog) {
            dialog = DialogUtils.showMD(context,null);
        }
        Dialog finalDialog = dialog;
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            r.run();
            if (isShowDialog) {
                DialogUtils.dissmiss(finalDialog);
            }
        }, 1000);
    }

    public static boolean coin() {
        long currentTimeMillis = System.currentTimeMillis();
        return currentTimeMillis % 2 == 0;
    }

    public static ArrayList<String> getTestData() {
        return getTestData(10);
    }

    public static ArrayList<String> getTestData(int total) {
        return Mock.getTestData(total);
    }

    public static ArrayList<String> getTestImgData() {
        return Mock.getTestImgData();
    }

    public static void requestStorage(FragmentActivity activity, Runnable runnable) {
        requestPermission(activity, "读写存储空间", runnable,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
        );
    }

    public static void requestLive(FragmentActivity activity, Runnable runnable) {
        requestPermission(activity, "摄像头与麦克风", runnable,
                Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO);
    }

    public static void requestLocation(FragmentActivity activity, Runnable runnable) {
        requestPermission(activity, "定位", runnable,
                Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION);
    }

    public static void requestPhone(FragmentActivity activity, Runnable runnable) {
        requestPermission(activity, "拨打电话", runnable,
                Manifest.permission.CALL_PHONE);
    }

    public static void requestPermission(FragmentActivity activity, String label, Runnable runnable, String... permissions) {
        PermissionX.init(activity)
                .permissions(permissions)
                .onExplainRequestReason((scope, deniedList) ->
                        scope.showRequestReasonDialog(deniedList, "当前功能需要" + label + "的权限",
                                "去设置", "取消"))
                .onForwardToSettings((scope, deniedList) ->
                        scope.showForwardToSettingsDialog(deniedList, "你需要手动在设置中允许" + label + "的权限",
                                "去设置", "取消"))
                .request((allGranted, grantedList, deniedList) -> {
                    if (allGranted) {
                        runnable.run();
                    } else {
                        $.toast("请先打开" + label + "权限");
                    }
                });
    }

    public static String timeFormat2(String time) {
        String result = "";
        try {
            result = TimeUtils.date2String(
                    TimeUtils.string2Date(time, "yyyy-MM-dd'T'HH:mm:ss"),
                    "yyyy年MM月dd日"
            );
        } catch (Exception e) {

        }
        return result;
    }

    public static String timeFormat7(String time) {
        String result = "";
        try {
            result = TimeUtils.date2String(
                    TimeUtils.string2Date(time, "yyyy-MM-dd'T'HH:mm:ss"),
                    "yyyy年MM月dd日 HH时mm分ss秒"
            );
        } catch (Exception e) {

        }
        return result;
    }

    public static String timeFormat(String time) {
        String result = "";
        try {
            result = TimeUtils.date2String(
                    TimeUtils.string2Date(time, "yyyy-MM-dd"),
                    "yyyy年MM月dd日"
            );
        } catch (Exception e) {

        }
        return result;
    }

    public static String timeFormat3(String time) {
        String result = "";
        try {
            result = TimeUtils.date2String(
                    TimeUtils.string2Date(time),
                    "HH:mm:ss"
            );
        } catch (Exception e) {

        }

        return result;
    }

    public static String timeFormat4(String time) {
        String result = "";
        try {
            result = TimeUtils.date2String(
                    TimeUtils.string2Date(time), "M月d日"
            );
        } catch (Exception e) {

        }

        return result;
    }

    public static String timeFormat5(String time) {
        String result = "";
        try {
            result = TimeUtils.date2String(
                    TimeUtils.string2Date(time), "yyyy年"
            );
        } catch (Exception e) {

        }

        return result;
    }

    public static String timeFormat6(String time) {
        String result = "";
        try {
            result = TimeUtils.date2String(
                    TimeUtils.string2Date(time), "mm:ss"
            );
        } catch (Exception e) {

        }

        return result;
    }

    public static String weekFormat(String time) {
        String result = "";
        try {
            result = TimeUtils.getChineseWeek(TimeUtils.string2Date(time));
        } catch (Exception e) {

        }
        return result;
    }
}
