package com.gh.gamecenter.wrapper

import android.app.Application
import androidx.lifecycle.*
import com.gh.common.util.ViewPagerFragmentHelper
import com.gh.gamecenter.common.utils.safelyGetInRelease
import com.gh.gamecenter.entity.MultiTabNav
import com.gh.gamecenter.livedata.Event
import com.halo.assistant.HaloApp
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.transformWhile

class SearchToolbarTabWrapperViewModel(
    application: Application,
    private val multiTabNavId: String,
    private val noTabLinkId: String,
    repository: MainWrapperRepository
) : TabWrapperViewModel(application, multiTabNavId, repository) {
    var noTabStyle = MultiTabNav.LinkMultiTabNav.TabStyle()
    var appBarOffset = 0

    private val _backgroundAlpha = MutableLiveData<Float>()
    val backgroundAlpha: LiveData<Float> = _backgroundAlpha
    fun updateBackgroundAlpha(alpha: Float) {
        _backgroundAlpha.value = alpha
    }

    val tabSelectLiveData =
        repository.tabSelectEventFlow
            .map { it as? MainSelectedEvent.SelectedTab }
            .transformWhile { originalEvent ->
                if (originalEvent?.navTabId == multiTabNavId) {
                    if (originalEvent.topTabIndex == -1 && originalEvent.topTabId.isNotEmpty()) {
                        multiTabLoadedFlow.receiveAsFlow().collect { tabList ->
                            val newTopTabIndex = tabList.indexOfFirst {
                                it.id == originalEvent.topTabId
                            }
                            emit(Event(MainSelectedEvent.SelectedTab(topTabIndex = newTopTabIndex)))
                        }
                    } else {
                        emit(Event(originalEvent))
                    }
                }
                true
            }
            .asLiveData()

    override fun overwriteDefaultIfNeeded(multiTabNav: MultiTabNav) {
        super.overwriteDefaultIfNeeded(multiTabNav)

        if (multiTabNav.id == repository.highPrioritySelectedNavId) {
            val newDefaultTabPosition = multiTabNav.linkMultiTabNav.indexOfFirst {
                it.id == repository.highPrioritySelectedTopTabId
            }

            if (newDefaultTabPosition != -1 && newDefaultTabPosition != defaultTabPosition) {
                defaultTabPosition = newDefaultTabPosition
                for ((index, tab) in multiTabNav.linkMultiTabNav.withIndex()) {
                    tab.default = index == defaultTabPosition
                }
            }
        }
    }

    fun isTabCustomPage(position: Int) =
        tabListLiveData.value?.safelyGetInRelease(position)?.link?.type == ViewPagerFragmentHelper.TYPE_CUSTOM_PAGE || noTabLinkId.isNotEmpty()

    fun getCustomPageTabEntity(pageId: String): MultiTabNav.LinkMultiTabNav? =
        tabListLiveData.value?.find { it.link?.type == ViewPagerFragmentHelper.TYPE_CUSTOM_PAGE && it.link.link == pageId }

    fun enableSlideBackgroundColor(pageId: String, isSlideBackgroundColorEnable: Boolean) {
        if (multiTabNavId.isNotEmpty()) {
            getCustomPageTabEntity(pageId)?.apply {
                style.isSlideBackgroundColorEnable = isSlideBackgroundColorEnable
                style.isSlideBackgroundColorShow = isSlideBackgroundColorEnable
                style.useLightStyle = isSlideBackgroundColorEnable
            }
        } else if (noTabLinkId.isNotEmpty()) {
            noTabStyle.isSlideBackgroundColorEnable = isSlideBackgroundColorEnable
            noTabStyle.isSlideBackgroundColorShow = isSlideBackgroundColorEnable
            noTabStyle.useLightStyle = isSlideBackgroundColorEnable
        }
    }

    class Factory(private val mMultiTabNavId: String, private val mNoTabLinkId: String) :
        ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return SearchToolbarTabWrapperViewModel(
                HaloApp.getInstance(),
                mMultiTabNavId,
                mNoTabLinkId,
                MainWrapperRepository.getInstance()
            ) as T
        }
    }
}