package com.gh.gamecenter.subject

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.common.retrofit.JSONObjectResponse
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.entity.SubjectData
import com.gh.gamecenter.entity.SubjectSettingEntity
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.json.JSONObject
import retrofit2.HttpException

class SubjectViewModel(
    application: Application,
    var subjectData: SubjectData?
) : AndroidViewModel(application) {

    val subjectNameLD = MutableLiveData<String>()
    val subjectSettingLD = MutableLiveData<SubjectSettingEntity>()

    private val repository =
        SubjectRepositoryFactory.createRepo(subjectData?.subjectType ?: SubjectData.SubjectType.NORMAL)

    init {
        initData()
    }

    fun initData() {
        if (subjectData != null) {
            if (subjectData?.subjectName.isNullOrEmpty()) {
                loadSubjectName()
            } else {
                subjectNameLD.postValue(subjectData?.subjectName)
                loadSubjectType()
            }
        } else {
            subjectSettingLD.postValue(null)
        }
    }

    private fun loadSubjectName() {
        repository
            .getSubjectName(subjectData?.subjectId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : JSONObjectResponse() {
                override fun onResponse(response: JSONObject) {
                    val name = response.getString("name")
                    subjectData?.subjectName = name
                    subjectNameLD.postValue(name)
                    loadSubjectType()
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    subjectNameLD.postValue("专题")
                    subjectData?.subjectName = "专题"
                    loadSubjectType()
                }
            })
    }

    private fun loadSubjectType() {
        repository
            .getColumnSettings(subjectData?.subjectId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<SubjectSettingEntity>() {
                override fun onResponse(response: SubjectSettingEntity?) {
                    super.onResponse(response)
                    val content = response!!.typeEntity.content
                    content.add(0, "全部")
                    subjectData?.tagType = response.tag
                    subjectData?.briefStyle = response.briefStyle
                    subjectData?.isOrder = response.order
                    subjectData?.isAdData = response.adIconActive
                    subjectData?.subjectStyle = response.columnStyle
                    subjectData?.showDetailSubtitle = response.showDetailSubtitle
                    subjectData?.showDetailIconSubscript = response.showDetailIconSubscript
                    subjectSettingLD.postValue(response)
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    subjectSettingLD.postValue(null)
                }
            })
    }


    class Factory(
        private val mApplication: Application,
        private val subjectData: SubjectData?
    ) :
        ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return SubjectViewModel(mApplication, subjectData) as T
        }
    }
}