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

import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
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

class PlayerViewModel(
    private val repository: PlayerRepository
) : ViewModel() {
    private var countDown: CountDown? = null

    private val countDownLiveData: MutableLiveData<Long> = 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) {

        }
    }
}

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