package com.module.backstage.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.module.backstage.R
import com.module.backstage.bean.SimBean
import com.module.backstage.bean.SimNetWorkResult
import com.module.backstage.repo.ErrorCheckFragmentRepo
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
import com.quyunshuo.androidbaseframemvvm.base.utils.MD5Util
import com.quyunshuo.androidbaseframemvvm.base.utils.SystemUtil
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import java.util.TreeMap
import javax.inject.Inject

/**
 * HomeFragment 的 VM 层
 *
 * @author Qu Yunshuo
 * @since 2021/8/5 11:11 下午
 */
@HiltViewModel//@HiltViewModel 注解的作用是将 ViewModel 标记为可以由 Hilt 管理的依赖项。当你将 @HiltViewModel 注解添加到 ViewModel 类上时，Hilt 会自动处理 ViewModel 的创建和注入过程。这样，你就不需要手动调用 ViewModelProvider 来获取 ViewModel 实例了。
class ErrorCheckFragmentVM @Inject constructor(private val mRepo: ErrorCheckFragmentRepo) :
    BaseViewModel() {

    /**
     * 当前已获取的文章页码
     */
    private var mCurrArticlePage = 1


    /**
     * 文章列表数据加载 first:是否是加载更多、second:加载更多是否成功
     */
    val articleLoadLD = MutableLiveData<Pair<Boolean, Boolean>>()


    val _networkResult = MutableLiveData<SimNetWorkResult>()
    val networkResult : MutableLiveData<SimNetWorkResult> get() = _networkResult
    /**
     * 是否已经加载了全部
     */
    val isLoadAllLD = MutableLiveData<Boolean>()
    var sim: Array<String> = arrayOf<String>(
        UiUtil.getStringRes(R.string.base_sim_card_is_identified),
        UiUtil.getStringRes(R.string.base_network_available),
        UiUtil.getStringRes(R.string.base_sim_status)
    )


    fun getSimFunction(): Array<String> {
        return sim
    }

    var network: Array<String> = arrayOf<String>(
        UiUtil.getStringRes(R.string.base_network_function1)
    )

    fun getNetworkFunction(): Array<String> {
        return network
    }

    var nayax: Array<String> = arrayOf<String>(
        UiUtil.getStringRes(R.string.base_nayax_function1),
        UiUtil.getStringRes(R.string.base_nayax_function2),
        UiUtil.getStringRes(R.string.base_nayax_function3),
        UiUtil.getStringRes(R.string.base_nayax_function4)
    )

    fun getNayaxFunction(): Array<String> {
        return nayax
    }

    /**
     * 获取 banner 数据
     */
    suspend fun getBanners() {
    }

    suspend fun checkSim() {

        val simSerialNumber: String = SystemUtil.getSimSerialNumber()
        val paramTreeMap = TreeMap<String, String>()
        paramTreeMap["appid"] = "102420155796"
        if (simSerialNumber.isNotEmpty()) {
            paramTreeMap["iccid"] = simSerialNumber
        }

        paramTreeMap["timestamp"] = System.currentTimeMillis().toString() + ""
        val sign: String = MD5Util.calcSign(paramTreeMap, "25e96cb36f631281e4e14cb9f489624e")
        paramTreeMap["sign"] = sign
        val gson = Gson()
        val s = gson.toJson(paramTreeMap)
        Log.d("sign ", "main: $s")
        mRepo.getData(paramTreeMap)
            .catch {
                _networkResult.value = it.message?.let { it1 -> SimNetWorkResult(false, it1) }
                // setSimStatus(it.message, false)
                Log.d("qqq", "getData: $it")
            }
            .collect {
                Log.d("backinfo","进入解析："+it)
                val simBean: SimBean = gson.fromJson(it, SimBean::class.java)
                val stringBuilder = StringBuilder()
                stringBuilder.append("$simSerialNumber:")
                if ("0" == simBean.getCode()) {
                    val data1: SimBean.DataBean = simBean.getData()
                    val deviceStatus: String = data1.getDeviceStatus()
                    val imeiStatus: String = data1.getImeiStatus()
                    val status: String = data1.getStatus()
                    var isStatus = true
                    when (deviceStatus) {
                        "TEST_READY_NAME" -> stringBuilder.append("卡在运营商的状态:可测试,")
                        "INVENTORY_NAME" -> stringBuilder.append("卡在运营商的状态:库存,")
                        "ACTIVATION_READY_NAME" -> stringBuilder.append("卡在运营商的状态:可激活,")
                        "ACTIVATED_NAME" -> stringBuilder.append("卡在运营商的状态:已激活,")
                        "DEACTIVATED_NAME" -> {
                            stringBuilder.append("卡在运营商的状态:已停卡,")
                            isStatus = false
                        }

                        "RETIRED_NAME" -> {
                            stringBuilder.append("卡在运营商的状态:已销卡,")
                            isStatus = false
                        }

                        "PURGED_NAME" -> {
                            stringBuilder.append("卡在运营商的状态:已清除,")
                            isStatus = false
                        }
                    }
                    when (imeiStatus) {
                        "NOT_SET" -> stringBuilder.append("机卡绑定状态:未开启,")
                        "NOT_BIND" -> stringBuilder.append("机卡绑定状态:待绑定,")
                        "NORMAL" -> stringBuilder.append("机卡绑定状态:正常,")
                        "SPLIT" -> {
                            stringBuilder.append("机卡绑定状态:机卡分离,")
                            isStatus = false
                        }
                    }
                    when (status) {
                        "testing" -> stringBuilder.append("状态status:测试中")
                        "inventory" -> stringBuilder.append("状态status:库存")
                        "pending-activation" -> stringBuilder.append("状态status:待激活")
                        "activation" -> stringBuilder.append("状态status:已激活")
                        "deactivation" -> {
                            isStatus = false
                            stringBuilder.append("状态status:已停卡")
                        }

                        "retired" -> {
                            isStatus = false
                            stringBuilder.append("状态status:已销卡")
                        }
                    }
                    _networkResult.value = SimNetWorkResult(isStatus, stringBuilder.toString())
                    // setSimStatus(stringBuilder.toString(), isStatus)
                } else {
                    stringBuilder.append(simBean.getDetail() + "," + simBean.getMessage())
                    _networkResult.value = SimNetWorkResult(false, stringBuilder.toString())
                    // setSimStatus(stringBuilder.toString(), false)
                }
            }
    }

    /**
     * 获取文章数据
     */
    fun getArticleData() {

    }
}