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

import android.content.Context
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 repository: PlayerRepository
) : ViewModel(), PlayerTimerUtil.Listener {

    init {
        PlayerTimerUtil.addListener(this)
    }

    private var countDown: CountDown? = null

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

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

    public fun loadConfig(context: Context) = viewModelScope.launch {
        repository.findHasCountDown("").first()?.let {

        }
    }

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

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

    /* 开启定时器 */
    public 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")

        if (type == 0) {

        } else {

            val countdown = countDown?.copy(
                courseId = "",
                type = type,
                value = value
            ) ?: CountDown(
                courseId = "",
                type = type,
                value = value
            )
            setCountDown(countdown)
            saveCountDown(countdown)
        }
    }

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

}

class PlayerViewModelFactory(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(repository) as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}