package com.casic.detector.extensions

import android.content.Context
import com.casic.detector.callback.OnImageCompressListener
import com.casic.detector.utils.FileType
import com.casic.detector.utils.LocaleConstant
import com.google.gson.JsonParser
import com.pengxh.kt.lite.extensions.createCompressImageDir
import com.pengxh.kt.lite.utils.SaveKeyValues
import top.zibin.luban.Luban
import top.zibin.luban.OnCompressListener
import java.io.File

/**
 * String扩展方法
 */
fun String.getResponseState(): Boolean {
    if (this.isBlank()) {
        return false
    }
    val element = JsonParser.parseString(this)
    val jsonObject = element.asJsonObject
    return jsonObject.get("success").asBoolean
}

fun String.getResponseMessage(): String {
    if (this.isBlank()) {
        return ""
    }
    val element = JsonParser.parseString(this)
    val jsonObject = element.asJsonObject
    return jsonObject.get("message").asString
}

/**
 * 下载路径为 http://xx.com/static/ 拼接downloadUrl
 * */
fun String.appendDownloadUrl(type: FileType): String {
    if (this.isEmpty()) return this
    val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String
    val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String
    val httpConfig = "http://${serverIp}:${serverPort}"
    return when (type) {
        FileType.APK -> "$httpConfig/ems/${this}"
        FileType.EXCEL -> "$httpConfig/ems/xls/marker${this}.xls"
        FileType.IMAGE -> "$httpConfig/ems${this}"
    }
}

fun String.toObjectType(): String {
    return if (this == LocaleConstant.POINT_TYPE_ARRAY[0]) {
        "1"
    } else if (this == LocaleConstant.POINT_TYPE_ARRAY[1]) {
        "2"
    } else if (this == LocaleConstant.POINT_TYPE_ARRAY[2]) {
        "3"
    } else {
        "4"
    }
}

fun String.toColor(): String {
    return if (this == LocaleConstant.COLOR_ARRAY[0]) {
        "0"
    } else if (this == LocaleConstant.COLOR_ARRAY[1]) {
        "1"
    } else if (this == LocaleConstant.COLOR_ARRAY[2]) {
        "2"
    } else if (this == LocaleConstant.COLOR_ARRAY[3]) {
        "3"
    } else if (this == LocaleConstant.COLOR_ARRAY[4]) {
        "4"
    } else if (this == LocaleConstant.COLOR_ARRAY[5]) {
        "5"
    } else {
        "6"
    }
}

fun String.compressImage(context: Context, listener: OnImageCompressListener) {
    Luban.with(context)
        .load(this)
        .ignoreBy(100)
        .setTargetDir(context.createCompressImageDir().toString())
        .setCompressListener(object : OnCompressListener {
            override fun onStart() {

            }

            override fun onSuccess(file: File) {
                listener.onSuccess(file)
            }

            override fun onError(e: Throwable) {
                listener.onError(e)
            }
        }).launch()
}

//根据控件名字存默认值
fun String.setDefaultValue(value: String) {
    SaveKeyValues.putValue(this, value)
}

//根据控件名字取默认值
fun String.getDefaultValue(): String {
    return SaveKeyValues.getValue(this, "") as String
}

fun String.hexToString(): String {
    val sb = StringBuilder()
    val temp: StringBuilder = StringBuilder()
    run {
        var i = 0
        while (i < this.length - 1) {
            // 每两个Hex为一组
            val output = this.substring(i, i + 2)
            val decimal = output.toInt(16)
            sb.append(decimal.toChar())
            temp.append(decimal)
            i += 2
        }
    }
    return sb.toString()
}

fun String.isNumber(): Boolean {
    val regex = Regex("[-+]?\\d+(\\.\\d+)?")
    return this.matches(regex)
}