package com.base.scanqrclear.ui.main

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.base.scanqrclear.bean.ContractUIBean
import com.base.scanqrclear.bean.EmailUIBean
import com.base.scanqrclear.bean.EventUIBean
import com.base.scanqrclear.bean.FunctionUIBean
import com.base.scanqrclear.bean.MessageUIBean
import com.base.scanqrclear.bean.ProductUIBean
import com.base.scanqrclear.bean.ScanBean
import com.base.scanqrclear.bean.ScanBean.Companion.CREATE_TYPE_SCAN
import com.base.scanqrclear.bean.ScanBean.Companion.CREATE_TYPE_USER
import com.base.scanqrclear.bean.TelephoneUIBean
import com.base.scanqrclear.bean.TextUIBean
import com.base.scanqrclear.bean.WebsiteUIBean
import com.base.scanqrclear.bean.WifiUIBean
import com.base.scanqrclear.utils.LogEx
import com.base.scanqrclear.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 }
        val eventList = SpJsonUtils.getSpJsonList<EventUIBean>(FunctionUIBean.KEY_EVENT)
        eventList.forEach { it.listContent = it.summary }
        val telephoneList = SpJsonUtils.getSpJsonList<TelephoneUIBean>(FunctionUIBean.KEY_TELEPHONE)
        telephoneList.forEach { it.listContent = it.tel }
        val contractList = SpJsonUtils.getSpJsonList<ContractUIBean>(FunctionUIBean.KEY_CONTACT)
        contractList.forEach { it.listContent = it.n }
        val messageList = SpJsonUtils.getSpJsonList<MessageUIBean>(FunctionUIBean.KEY_MESSAGE)
        messageList.forEach { it.listContent = it.msg }

        historyList.addAll(wifiList)
        historyList.addAll(textList)
        historyList.addAll(emailList)
        historyList.addAll(websiteList)
        historyList.addAll(productList)
        historyList.addAll(eventList)
        historyList.addAll(telephoneList)
        historyList.addAll(contractList)
        historyList.addAll(messageList)

        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 -> {
                SpJsonUtils.removeJsonBean<ContractUIBean>(bean.scanType, bean as ContractUIBean, removeIf = { it1, it2 ->
                    it1.createTime == it2.createTime
                })
            }

            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
                })
            }

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

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

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

        historyList.remove(bean)
    }


}