package com.gh.gamecenter.discovery.interestedgame

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.common.utils.singleToMain
import com.gh.gamecenter.common.utils.toRequestBody
import com.gh.gamecenter.entity.InterestedGameEntity
import com.gh.gamecenter.entity.InterestedGameEntity.TypeTag.Tag
import com.gh.gamecenter.entity.InterestedGamePostEntity
import com.gh.gamecenter.retrofit.RetrofitManager
import okhttp3.ResponseBody
import java.util.*

class InterestedGameViewModel(application: Application) : AndroidViewModel(application) {

    private val mApi = RetrofitManager.getInstance().newApi
    private var mTypeTags: List<InterestedGameEntity.TypeTag> = listOf()
    private val mSelectedTagList = arrayListOf<Tag>()
    private val mTempSelectedTagList = arrayListOf<Tag>()
    private var mSelectedCount = 0
    var interestedGameData = MutableLiveData<InterestedGameEntity>()
    var postResult = MutableLiveData<Boolean>()
    var selectedCountLiveData = MutableLiveData<Int>()
    var selectedTagLisLiveData = MutableLiveData<ArrayList<Tag>>()
    var isModify = false

    init {
        getInterestedGameData()
    }

    fun getTypeTags() = mTypeTags

    fun getSelectedCount() = mSelectedCount

    fun getSelectedTagList() = mSelectedTagList

    fun getTempSelectedTagList() = mTempSelectedTagList

    fun getSelectedTagIdList() = mSelectedTagList.map { it.id }

    fun getTagIndex(tag: Tag) = mSelectedTagList.indexOf(tag)

    @SuppressLint("CheckResult")
    fun getInterestedGameData() {
        mApi.interestedGame
            .compose(singleToMain())
            .subscribe(object : BiResponse<InterestedGameEntity>() {
                override fun onSuccess(data: InterestedGameEntity) {
                    mTypeTags = data.typeTag.filter { it.tags.isNotEmpty() }
                    initSelectedTags(data.tags)
                    interestedGameData.postValue(data)
                }
            })
    }

    @SuppressLint("CheckResult")
    fun postInterestedGameData(data: InterestedGamePostEntity) {
        mApi.postInterestedGame(data.toRequestBody())
            .compose(singleToMain())
            .subscribe(object : BiResponse<ResponseBody>() {
                override fun onSuccess(data: ResponseBody) {
                    postResult.postValue(true)
                }

                override fun onFailure(exception: Exception) {
                    postResult.postValue(false)
                }
            })
    }

    fun initSelectedTags(tagIdList: List<String>) {
        mTypeTags.forEach {
            it.tags.forEach { tag ->
                if (tagIdList.contains(tag.id)) {
                    addTag(tag)
                }
            }
        }
        mSelectedCount = mSelectedTagList.size
        if (mSelectedTagList.isNotEmpty()) postSelectedTagList()
    }

    fun postSelectedTagList() {
        selectedTagLisLiveData.postValue(mSelectedTagList)
    }

    fun postSelectedCount() {
        selectedCountLiveData.postValue(mSelectedCount)
    }

    fun increaseSelectedCount() {
        mSelectedCount++
        postSelectedCount()
    }

    fun decreaseSelectedCount() {
        if (mSelectedCount > 0) mSelectedCount--
        postSelectedCount()
    }

    fun addTag(tag: Tag) {
        if (mSelectedTagList.contains(tag)) return
        mSelectedTagList.add(tag)
    }

    fun removeTag(tag: Tag) {
        mSelectedTagList.remove(tag)
    }

    fun addTempTag(tag: Tag) {
        mTempSelectedTagList.add(tag)
    }

    fun removeTempTag(tag: Tag) {
        mTempSelectedTagList.remove(tag)
    }

    fun confirmTempList() {
        mSelectedTagList.clear()
        mSelectedTagList.addAll(mTempSelectedTagList)
        postSelectedTagList()
    }

    fun initTempTagList() {
        mTempSelectedTagList.clear()
        mTempSelectedTagList.addAll(mSelectedTagList)
        mSelectedCount = mTempSelectedTagList.size
        postSelectedCount()
    }

    fun resetTemp() {
        mTempSelectedTagList.clear()
        mSelectedCount = 0
        postSelectedCount()
    }
}