package com.casic.electric.detector.extensions

import android.content.Context
import android.widget.TextView
import com.casic.electric.detector.callback.OnImageCompressListener
import com.casic.electric.detector.model.ErrorMessageModel
import com.casic.electric.detector.utils.FileType
import com.casic.electric.detector.utils.LocaleConstant
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.pengxh.kt.lite.extensions.createCompressImageDir
import com.pengxh.kt.lite.utils.SaveKeyValues
import org.json.JSONObject
import top.zibin.luban.Luban
import top.zibin.luban.OnCompressListener
import java.io.File

/**
 * String扩展方法
 */
fun String.separateResponseState(): Boolean {
    if (this.isBlank()) {
        return false
    }
    return JSONObject(this).getBoolean("success")
}

fun String.toErrorMessage(): String {
    val errorModel = Gson().fromJson<ErrorMessageModel>(
        this, object : TypeToken<ErrorMessageModel>() {}.type
    )
    return errorModel.message.toString()
}

/**
 * 下载路径为 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.IMAGE -> "$httpConfig/ems${this}"
    }
}

fun String.appendExcelDownloadUrl(): 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}"
    //http://139.198.18.188:8091/ems/xls/marker1.xls
    //http://139.198.18.188:8091/ems/xls/smallMarker1.xls
    return "$httpConfig/ems${this}"
}

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(textView: TextView) {
    SaveKeyValues.putValue(this, textView.text.toString())
}

//根据控件名字取默认值
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.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"
    }
}