package com.base.scanqr.ui.main

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.base.scanqr.bean.AdScanBean
import com.base.scanqr.bean.EmailUIBean
import com.base.scanqr.bean.FunctionUIBean
import com.base.scanqr.bean.ProductUIBean
import com.base.scanqr.bean.ScanBean
import com.base.scanqr.bean.TextUIBean
import com.base.scanqr.bean.WebsiteUIBean
import com.base.scanqr.bean.WifiUIBean
import com.base.scanqr.utils.LogEx
import com.base.scanqr.utils.SpJsonUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.concurrent.atomic.AtomicBoolean

class HistoryViewModel() : ViewModel() {
    private val TAG = "HistoryViewModel"

    var historyList = arrayListOf<ScanBean>()

    var dataRefreshFinish = AtomicBoolean(false)
    var dataRefreshBack: (() -> Unit)? = null

    fun initData() = viewModelScope.launch(Dispatchers.IO) {
        historyList.clear()

        val wifiList = SpJsonUtils.getSpJsonList<WifiUIBean>(FunctionUIBean.KEY_WIFI)
        wifiList.forEach { it.listContent = it.ssid }
        val textList = SpJsonUtils.getSpJsonList<TextUIBean>(FunctionUIBean.KEY_TEXT)
        textList.forEach { it.listContent = it.content }
        val emailList = SpJsonUtils.getSpJsonList<EmailUIBean>(FunctionUIBean.KEY_EMAIL)
        emailList.forEach { it.listContent = it.address }
        val websiteList = SpJsonUtils.getSpJsonList<WebsiteUIBean>(FunctionUIBean.KEY_WEBSITE)
        websiteList.forEach { it.listContent = it.http }
        val productList = SpJsonUtils.getSpJsonList<ProductUIBean>(FunctionUIBean.KEY_PRODUCT)
        productList.forEach { it.listContent = it.content }

        historyList.addAll(wifiList)
        historyList.addAll(textList)
        historyList.addAll(emailList)
        historyList.addAll(websiteList)
        historyList.addAll(productList)

        historyList.forEach {
            LogEx.logDebug(TAG, "${it.scanType} ${it.createTime} ${it.createType} ${it.listContent}")
        }
        dataRefreshBack?.invoke()
        dataRefreshFinish.set(true)
    }

    fun removeList(tab: Int) {
        val removeList = historyList.filter { it.createType == tab }
        removeList.forEach { removeBean(it) }
    }

    fun removeBean(bean: ScanBean) {

        when (bean.scanType) {
            FunctionUIBean.KEY_WIFI -> {
                SpJsonUtils.removeJsonBean<WifiUIBean>(bean.scanType, bean as WifiUIBean, removeIf = { it1, it2 ->
                    it1.createTime == it2.createTime
                })
            }

            FunctionUIBean.KEY_TEXT -> {
                SpJsonUtils.removeJsonBean<TextUIBean>(bean.scanType, bean as TextUIBean, removeIf = { it1, it2 ->
                    it1.createTime == it2.createTime
                })
            }

            FunctionUIBean.KEY_EMAIL -> {
                SpJsonUtils.removeJsonBean<EmailUIBean>(bean.scanType, bean as EmailUIBean, removeIf = { it1, it2 ->
                    it1.createTime == it2.createTime
                })
            }

            FunctionUIBean.KEY_CONTACT -> {

            }

            FunctionUIBean.KEY_WEBSITE -> {
                SpJsonUtils.removeJsonBean<WebsiteUIBean>(bean.scanType, bean as WebsiteUIBean, removeIf = { it1, it2 ->
                    it1.createTime == it2.createTime
                })
            }

            FunctionUIBean.KEY_PRODUCT -> {
                SpJsonUtils.removeJsonBean<ProductUIBean>(bean.scanType, bean as ProductUIBean, removeIf = { it1, it2 ->
                    it1.createTime == it2.createTime
                })
            }

        }

        historyList.remove(bean)
    }


}