package com.gh.common.prioritychain

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.gh.gamecenter.feature.entity.FloatingWindowEntity
import com.gh.gamecenter.livedata.Event

class CustomFloatingWindowHandler(priority: Int) : PriorityChainHandler(priority) {

    private val data = arrayListOf<FloatingWindowEntity>()

    private val _showFloatingAction = MutableLiveData<Event<ArrayList<FloatingWindowEntity>>>()
    val showFloatingAction: LiveData<Event<ArrayList<FloatingWindowEntity>>> = _showFloatingAction

    fun setData(newData: List<FloatingWindowEntity>) {
        data.clear()
        data.addAll(newData)
        doPreProcess()
    }

    private fun doPreProcess() {
//        debugOnly {
//            data.clear()
//            data.add(
//                FloatingWindowEntity(
//                    id = "audire",
//                    image = "https://jira.shanqu.cc/secure/attachment/57822/57822_image-2023-12-01-17-53-04-492.png",
//                    link = WelcomeDialogEntity(
//                        id = null,
//                        time = null,
//                        packages = null,
//                        floatingWindowId = null,
//                        shouldShowExitAnimation = false
//                    ),
//                    pushType = "maiorum",
//                    expandable = false,
//                    expandedImage = "fermentum",
//                    bigPopupNotice = "dignissim"
//                )
//            )
//        }

        if (getStatus() == STATUS_PENDING) {
            if (data.isNotEmpty()) {
                updateStatus(STATUS_VALID)
                process()
            } else {
                processNext()
            }
        } else {
            if (data.isNotEmpty()) {
                updateStatus(STATUS_VALID)
            } else {
                updateStatus(STATUS_INVALID)
            }
        }
    }

    override fun onProcess(): Boolean {
        when (getStatus()) {
            STATUS_VALID -> {
                _showFloatingAction.postValue(Event(data))
                return true
//                    floatingWindowProvider.showFloatingWindowOnly(
//                        mFragment!!,
//                        mRecyclerView!!,
//                        mWindowList!!,
//                    ) {
//                        val welcomeDialog = WelcomeDialogFragment.getInstance(it, true, mFragment)
//                        welcomeDialog.show(mFragment!!.childFragmentManager, "WelcomeDialog")
//                    }
            }

            STATUS_INVALID -> {
                processNext()
            }
        }

        return false
    }

    fun dismiss() {
        if (getStatus() == STATUS_HANDLING || getStatus() == STATUS_VALID) {
            processNext()
        }
    }
}