package com.gh.gamecenter.gamedetail.dialog

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.entity.LibaoEntity
import com.gh.gamecenter.feature.entity.GameDetailServer
import com.gh.ndownload.suspendwindow.utils.NDownloadSuspendWindowHelper

class SpecialDownloadDialogViewModel(
    val gameEntity: GameEntity,
    val zoneUrl: String?,
    val libaoList: ArrayList<LibaoEntity>?,
    val server: GameDetailServer?
) : ViewModel() {

    private val _displayContentLiveData = MutableLiveData<ArrayList<Content>>()
    val displayContentLiveData: LiveData<ArrayList<Content>> = _displayContentLiveData

    fun updateDisplayContent(context: Context) {
        val contentList = ArrayList<Content>()
        if (!zoneUrl.isNullOrEmpty()) {
            contentList.add(Content(zoneUrl = zoneUrl))
        }
        if (libaoList != null) {
            contentList.add(Content(libaoList = libaoList))
        }
        if (server != null) {
            contentList.add(Content(server = server))
        }
        if (NDownloadSuspendWindowHelper.canDrawOverLayer(context) == false) {
            contentList.add(Content(isAlertPermissionGranted = false))
        }

        _displayContentLiveData.value = contentList
    }

    class Content(
        val zoneUrl: String? = null,
        val libaoList: ArrayList<LibaoEntity>? = null,
        val server: GameDetailServer? = null,
        val isAlertPermissionGranted: Boolean? = null
    )

    class Factory(
        private val gameEntity: GameEntity,
        private val zoneUrl: String?,
        private val libaoList: ArrayList<LibaoEntity>?,
        private val server: GameDetailServer?
    ) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return SpecialDownloadDialogViewModel(gameEntity, zoneUrl, libaoList, server) as T
        }
    }

}