package com.tencent.liteav.demo.player.viewmodel

import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.tencent.liteav.demo.player.util.PlayerTimerUtil
import com.tencent.liteav.demo.superplayer.SuperPlayerModel
import com.tencent.liteav.demo.superplayer.database.entity.CountDown
import com.tencent.liteav.demo.superplayer.database.entity.History
import com.tencent.liteav.demo.superplayer.database.repo.PlayerRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.*

class PlayerViewModel(
    private val courseId: String,
    private val repository: PlayerRepository
) : ViewModel(), PlayerTimerUtil.Listener {

    init {
        PlayerTimerUtil.addListener(this)
    }

    private var countDown: CountDown? = null

    private val countDownLiveData: MutableLiveData<Long> = MutableLiveData()

    public val toastStr: MutableLiveData<String> = MutableLiveData()

    public fun loadConfig(context: Context) = viewModelScope.launch {
        val common = context.getSharedPreferences("player_timer", Context.MODE_PRIVATE)
        val defaultType = common.getInt("type", 0);
        val defaultValue = common.getInt("value", 0);
        val default = CountDown(
            courseId = courseId,
            type = defaultType,
            value = defaultValue,
        )
        val special = repository.findHasCountDown(courseId).first()
    }

    /* 保存一条观看时间段的记录 */
    public fun saveHistory(history: History) = viewModelScope.launch(Dispatchers.IO) {
        repository.insertHistory(history)
    }

    /* 保存对该门课程单独设置的定时 */
    private fun saveCountDown(countDown: CountDown) = viewModelScope.launch(Dispatchers.IO) {
        repository.insertCountDown(countDown)
    }

    /* 开启定时器 */
    private fun setCountDown(countDown: CountDown) {
        if (countDown.type == CountDown.TYPE_EPISODE) {

        } else if (countDown.type == CountDown.TYPE_DURATION) {

        }
    }

    fun changeModel(model: SuperPlayerModel) {
        val history = History(
            start = 0,
            end = 10,
            courseId = "0",
            sectionId = model.title!!,
            date = SimpleDateFormat("yyyy-MM-dd").format(Date())
        )

        saveHistory(history)
    }

    override fun onListen(type: Int, value: Int) {
        Log.d("PVM", "onListen: $type , $value")

        countDown = if (countDown == null) {
            CountDown(
                courseId = courseId,
                type = type,
                value = value
            )
        } else {
            countDown!!.copyWith(
                courseId = courseId,
                type = type,
                value = value
            )
        }

        countDown!!.let {
            setCountDown(it)
            saveCountDown(it)
        }
    }

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

}

class PlayerViewModelFactory(
    private val courseId: String,
    private val repository: PlayerRepository,
) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(PlayerViewModel::class.java)) {
            @Suppress("UNCHECKED_CAST")
            return PlayerViewModel(courseId, repository) as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}