package com.cr.data

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.graphics.Typeface
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.RequiresApi
import com.cr.dialog.DialogNormal
import dji.v5.utils.common.ContextUtil
import dji.v5.utils.common.StringUtils
import java.text.SimpleDateFormat
import java.util.*
import java.util.regex.Pattern

const val DEFAULT_STR = "N/A"
const val DEFAULT_RES_ID = -1
const val IN_INNER_NETWORK_STR = "IN_INNER"
const val IN_OUT_NETWORK_STR = "IN_OUT"
const val NO_NETWORK_STR = "NO_NETWORK"
const val ONLINE_STR = "ONLINE"
const val MAIN_FRAGMENT_PAGE_TITLE = "MAIN_FRAGMENT_PAGE_TITLE"
const val MEDIA_FILE_DETAILS_STR = "MEDIA_FILE_DETAILS"
const val REGISTER_SUCCESS = "Cr"
const val REGISTER_FAILED = "Err"
const val TAG: String = "CrUAV"
const val RES_ERR_FLAG = "RES_ERR"
const val RES_NULL_FLAG = "RES_NO"
const val RES_SUCCESS_FLAG = "RES_OK"
const val RES_CONTENT = "data"
const val RES_ERR_DES = "msg"
const val RES_ERR_CODE = "code"
const val RES_HEAD = "res"

/**
 * 公共类
 */
class CrUtil {
    companion object {
        @JvmStatic
        val FontPath: String = "fonts/iconfont.ttf"

        @JvmStatic
        var toast: Toast? = null

        // define: 2023/3/31 工程目录
        val PROJECT_NAME: String = "lsquav"

        // define: 2023/3/31 存储主目录 随着App卸载删除
        val CARD_PATH: String = ContextUtil.getContext().getExternalFilesDir(null).toString()

        // define: 2023/3/31 工程目录
        val PROJECT_PATH: String = "$CARD_PATH/$PROJECT_NAME"

        // define: 2023/3/31 工程缓存目录
        val PROJECT_CACHE_PATH: String = "$PROJECT_PATH/Cache"

        // define: 2023/3/31 图片存储目录
        val IMAGE_PATH: String = "$PROJECT_PATH/img/"

        // define: 2023/3/31 配置文件存储路径
        val CONFIG_PATH: String = "$PROJECT_PATH/config/"

        // define: 2023/4/3 数据库路径
        val DATABASE_PATH = "$CONFIG_PATH/dinfo.db"

        // define: 2023/4/13 网络下载的飞行库文件名称
        val DATA_FILE_NAME = "data.sqlite"

        // define: 2023/4/13 网络下载的配置库文件名称
        val CONFIG_FILE_NAME = "config.sqlite"

        // define: 2023/4/13 可编辑地图数据集文件名称
        val MAP_EDIT_FILE_NAME = "map.geodatabase"

        // define: 2023/4/13 基础地图数据集文件名称
        val MAP_BASE_FILE_NAME = "base.geodatabase"

        // define: 2023/4/13 配置库文件路径
        val DATABASE_PATH_CONFIG = "${CONFIG_PATH}/${CONFIG_FILE_NAME}"

        // define: 2023/4/13 飞行库文件路径
        val DATABASE_PATH_AIR = "${CONFIG_PATH}/${DATA_FILE_NAME}"

        // define: 2023/4/13 基础地图数据集完整路径
        val MAP_PATH_BASE = "${CONFIG_PATH}/${MAP_BASE_FILE_NAME}"

        // define: 2023/4/13 可编辑地图数据集完整路径
        val MAP_PATH_EDIT = "${CONFIG_PATH}/${MAP_EDIT_FILE_NAME}"


        /**
         * 根据等级数值获取等级
         * @param level Int
         * @return Level
         */
        @JvmStatic
        fun getLevel(level: Int): Level {
            if (level in 1..30) {
                return Level.LEVEL1
            } else if (level in 31..60) {
                return Level.LEVEL2
            } else if (level in 61..80) {
                return Level.LEVEL3
            } else if (level in 81..100) {
                return Level.LEVEL4
            } else {
                return Level.LEVEL1
            }
        }

        /**
         * 获取链路等级
         * @param level Int
         * @return Level
         */
        fun getLinkSignalQuality(level: Int): Level {
            if (level in 1 until 40) {
                return Level.LEVEL2
            } else if (level in 40 until 60) {
                return Level.LEVEL3
            } else if (level in 60..100) {
                return Level.LEVEL4
            } else {
                return Level.LEVEL1
            }
        }

        /**
         * Logcat输出消息
         * @param message String
         */
        @JvmStatic
        fun print(message: String) {
            Log.i(TAG, SimpleDateFormat("HH:mm:ss").format(Date()) + " " + message)
        }

        /**
         * 设置显示的字体内容
         * @param context Context 上下文
         * @param textView TextView 文本控件
         * @param fontId Int 字体Id
         */
        @JvmStatic
        fun setFont(context: Context, textView: TextView, fontId: Int) {
            var font: Typeface? = Typeface.createFromAsset(context.assets, CrUtil.FontPath)
            textView!!.typeface = font
            textView!!.text = StringUtils.getResStr(fontId)
        }

        /**
         * 获取字体
         * @param context Context
         * @return Typeface?
         */
        @JvmStatic
        fun getFont(context: Context): Typeface? {
            return Typeface.createFromAsset(context.assets, "fonts/tx.ttf")
        }

        /**
         * 多线程显示提示消息
         * @param message String 消息内容
         */
        @JvmStatic
        fun showMessage(message: String) {
            var mainHandler = Handler(Looper.getMainLooper())
            mainHandler.post(Runnable {
                if (toast != null) {
                    toast!!.cancel()
                }
                toast = Toast.makeText(ContextUtil.getContext(), message, Toast.LENGTH_LONG)
                toast!!.duration = Toast.LENGTH_LONG
                toast!!.setText(message)
                toast!!.show()
            })
        }

        /**
         * 获取计算值
         * @param dimenId Int
         * @return Float
         */
        @JvmStatic
        fun getDimens(dimenId: Int): Float {
            return ContextUtil.getContext().resources.getDimension(dimenId)
        }

        /**
         * 检查Ip地址
         * @param strIP String?
         * @return Boolean?
         */
        fun checkIP(strIP: String?): Boolean? {
            val ip = ("^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$") //限定输入格式
            val p = Pattern.compile(ip)
            val m = p.matcher(strIP)
            return m.matches()
        }

        /**
         * 检查端口
         * @param strCOM String?
         * @return Boolean?
         */
        fun checkCOM(strCOM: String?): Boolean? {
            val ip = "^\\d{2,6}$" //限定输入格式
            val p = Pattern.compile(ip)
            val m = p.matcher(strCOM)
            return m.matches()
        }

        /**
         * 获取版本名称
         * @param context Context 上下文
         * @return String
         */
        fun getVersionName(context: Context): String {
            var packageInfo: PackageInfo = getPackageInfo(context)
            return packageInfo.versionName
        }

        /**
         * 获取版本号
         * @param context Context 上下文
         * @return Long
         */
        @RequiresApi(Build.VERSION_CODES.P)
        fun getVersionCode(context: Context): Long {
            var packageInfo: PackageInfo = getPackageInfo(context)
            return packageInfo.longVersionCode
        }

        /**
         * 获取包信息
         * @param context Context 上下文
         * @return PackageInfo
         */
        fun getPackageInfo(context: Context): PackageInfo {
            var pm: PackageManager = context.packageManager
            var packageInfo: PackageInfo =
                pm.getPackageInfo(context.packageName, PackageManager.GET_CONFIGURATIONS)
            return packageInfo
        }

        /**
         * 扩展函数 判断TextView内容是否为空
         * @receiver TextView 文本框
         * @param message String 错误消息
         * @return String?
         */
        fun TextView.checkBlank(message: String): String? {
            val text = this.text.toString()
            if (text.isBlank()) {
                DialogNormal(this.context, "警告", message).show()
                return null
            } else {
                return text
            }
        }

        /**
         * 扩展函数
         * 启动窗体
         * @receiver Context
         */
        inline fun <reified T : Activity> Context.onStart() {
            startActivity(Intent(this, T::class.java))
        }
    }
}

/**
 * 等级枚举
 * @property index Int
 * @constructor
 */
enum class Level(var index: Int) {
    // todo: 2023/3/9 0-30
    LEVEL1(1),

    // todo: 2023/3/9 30-60
    LEVEL2(2),

    // todo: 2023/3/9 60-80
    LEVEL3(3),

    // todo: 2023/3/9 80-100
    LEVEL4(4)
}