package com.adealink.weparty.setting.viewmodel

import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.adealink.frame.base.Rlt
import com.adealink.frame.base.fastLazy
import com.adealink.frame.coroutine.dispatcher.Dispatcher
import com.adealink.frame.image.imageService
import com.adealink.frame.locale.language.data.Language
import com.adealink.frame.log.XLogHelper
import com.adealink.frame.log.data.ReportLogReason
import com.adealink.frame.mvvm.livedata.OnceMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.weparty.App
import com.adealink.weparty.commonui.mediapicker.imageselect.util.clearAllProcessedImage
import com.adealink.weparty.commonui.mediapicker.imageselect.util.getProcessedImageCacheSize
import com.adealink.weparty.module.setting.viewmodel.ISettingViewModel
import com.adealink.weparty.module.webview.WebModule
import com.adealink.weparty.setting.datasource.remote.SettingHttpService
import com.adealink.weparty.setting.report.ReportLanguageReq
import com.adealink.weparty.update.listener.IUpdateListener
import com.adealink.weparty.update.updateManager
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class SettingViewModel : BaseViewModel(), ISettingViewModel, IUpdateListener {

    private val settingHttpService by fastLazy {
        App.instance.networkService.getHttpService(SettingHttpService::class.java)
    }

    init {
        updateManager.addListener(this)
    }

    override fun onCleared() {
        super.onCleared()
        updateManager.removeListener(this)
    }

    val newVersionLD = MutableLiveData<Boolean>()
    fun checkAppUpdate(activity: FragmentActivity) {
        viewModelScope.launch {
            newVersionLD.send(updateManager.hasNewVersionUpdate())
            updateManager.checkUpdate(activity)
        }
    }

    fun getCacheSize(): LiveData<Long> {
        val liveData: LiveData<Long> = OnceMutableLiveData()
        viewModelScope.launch {
            withContext(Dispatcher.WENEXT_THREAD_POOL) {
                var totalSize: Long = 0
                totalSize += imageService.getCacheSize()
                totalSize += getProcessedImageCacheSize()
                totalSize += WebModule.getCacheSize()
                liveData.send(totalSize)
            }
        }
        return liveData
    }

    fun clearCache() {
        viewModelScope.launch {
            withContext(Dispatcher.WENEXT_THREAD_POOL) {
                imageService.clearCache()
                clearAllProcessedImage()
                XLogHelper.clearLogFiles()
                WebModule.clearCache()
            }
        }
    }

    override fun notifyHasNewVersion(newVersion: Boolean) {
        newVersionLD.send(newVersion)
    }

    override fun reportLanguage(language: Language) {
        viewModelScope.launch {
            settingHttpService.reportLanguage(
                ReportLanguageReq(language.code)
            )
        }
    }

    fun uploadLog(reason: ReportLogReason): LiveData<Rlt<String>> {
        val liveData = OnceMutableLiveData<Rlt<String>>()
        viewModelScope.launch {
            liveData.send(App.instance.logService.uploadLog(reason))
        }
        return liveData
    }

}