package com.gh.gamecenter.category2

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.common.util.LogUtils
import com.gh.gamecenter.entity.CategoryEntity
import com.gh.gamecenter.entity.SidebarsEntity
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class CategoryV2ViewModel(
    application: Application,
    private val mCategoryId: String,
    val categoryTitle: String
) : AndroidViewModel(application) {

    private val api = RetrofitManager.getInstance().api
    var sidebarsLiveData = MutableLiveData<SidebarsEntity>()
    var directories = ArrayList<CategoryEntity>()
    var directoriesLiveData = MutableLiveData<List<CategoryEntity>>()
    var selectedCount = 0
    var selectedCountLiveData = MutableLiveData<Int>()
    var categoryPositionLiveData = MutableLiveData<Pair<Int, Int>>()

    var selectedCategoryName: String = ""
    var selectedCategoryPosition: Int = 0
    var selectedCategoryList = ArrayList<CategoryEntity>()
    var entrance: String = ""

    init {
        getSidebars()
        getCategoryDirectories()
    }

    @SuppressLint("CheckResult")
    fun getSidebars() {
        api.getSidebars(mCategoryId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<SidebarsEntity>() {
                override fun onSuccess(data: SidebarsEntity) {
                    sidebarsLiveData.postValue(data)
                }

                override fun onFailure(exception: Exception) {
                    super.onFailure(exception)
                    sidebarsLiveData.postValue(null)
                }
            })
    }

    @SuppressLint("CheckResult")
    fun getCategoryDirectories() {
        api.getCategoryDirectories(mCategoryId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<List<CategoryEntity>>() {
                override fun onSuccess(data: List<CategoryEntity>) {
                    directories = ArrayList(data)
                    postCategoryDirectoryList()
                }
            })
    }

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

    fun postCategoryPosition(primaryIndex: Int, subIndex: Int) {
        categoryPositionLiveData.postValue(Pair(primaryIndex, subIndex))
    }

    fun postCategoryDirectoryList() {
        directoriesLiveData.postValue(directories)
    }

    fun resetDirectoryList() {
        selectedCount = 0
        selectedCategoryList.clear()
        directories.forEach {
            it.data?.forEach { entity ->
                entity.selected = false
            }
        }
        postSelectedCount()
        postCategoryDirectoryList()
    }

    fun logAppearance() {
        LogUtils.logCategoryV2AppearanceEvent(entrance, categoryTitle)
    }

    fun logClickSide() {
        LogUtils.logCategoryV2ClickSideEvent(entrance, categoryTitle, selectedCategoryName, selectedCategoryPosition)
    }

    fun logClickClassification(primaryIndex: Int, categoryName: String, position: Int) {
        LogUtils.logCategoryV2ClickClassificationEvent(
            entrance,
            categoryTitle,
            selectedCategoryName,
            directories[primaryIndex].name,
            categoryName,
            primaryIndex,
            position
        )
    }

    fun logClickClassificationDelete(primaryIndex: Int, categoryName: String, location: String) {
        LogUtils.logCategoryV2ClickClassificationDeleteEvent(
            entrance,
            categoryTitle,
            directories[primaryIndex].name,
            categoryName,
            location
        )
    }

    fun logClickDetermine() {
        val categoryName = StringBuilder()
        selectedCategoryList.forEachIndexed { index, s ->
            categoryName.append(s.name)
            if (index != selectedCategoryList.size - 1) {
                categoryName.append("+")
            }
        }
        LogUtils.logCategoryV2ClickDetermineEvent(entrance, categoryTitle, categoryName.toString())
    }

    fun logClickReset(location: String) {
        val categoryName = StringBuilder()
        selectedCategoryList.forEachIndexed { index, s ->
            categoryName.append(s.name)
            if (index != selectedCategoryList.size - 1) {
                categoryName.append("+")
            }
        }
        LogUtils.logCategoryV2ClickResetEvent(entrance, categoryTitle, categoryName.toString(), location)
    }

    class Factory(
        private val categoryId: String,
        private val categoryTitle: String
    ) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return CategoryV2ViewModel(
                HaloApp.getInstance().application,
                categoryId,
                categoryTitle
            ) as T
        }
    }
}