package com.develop.common.food_sdk

import android.annotation.SuppressLint
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.appcompat.widget.AppCompatTextView
import com.develop.base.ext.load
import com.blankj.utilcode.util.ScreenUtils.getScreenHeight
import com.blankj.utilcode.util.ScreenUtils.getScreenWidth
import com.develop.base.ext.globalApp
import com.develop.base.ext.navigateTo
import com.develop.base.ext.resId2Dimension
import com.develop.base.util.ThreadUtils
import com.develop.common.R
import com.develop.common.dialog.CancelConfirmDialog
import com.develop.common.event.CookStepEvent
import com.develop.common.router.Screens
import com.develop.common.tag.MODE_TYPE
import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevStatus
import com.kuyuntech.cofarcooking.device.sdk.eventbus.event.DevStatusEvent
import com.lzf.easyfloat.EasyFloat
import com.lzf.easyfloat.enums.ShowPattern
import com.lzf.easyfloat.enums.SidePattern
import org.apache.commons.lang3.StringUtils
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe

@SuppressLint("StaticFieldLeak")
object FloatWindowManager {
    private var filterClass: Array<Class<*>>
    private var recipeNumber = ""
    private var step = 0
    private var coverPath: Any? = null
    private var modeName = ""
    private var isMode = false

    private var ivFood: ImageView? = null
    private var tvStep: AppCompatTextView? = null

    private val confirmCancelDialog by lazy {
        CancelConfirmDialog()
    }

    init {
        filterClass = arrayOf(
            getFilterClass(FilterClass.COOK_MODES),
            getFilterClass(FilterClass.COOK_STEP),
        )
        

        confirmCancelDialog.onDialogClickListener =
            object : CancelConfirmDialog.OnDialogClickListener {
                override fun onConfirm() {
                    //确认

                }

                override fun onCancel() {
                    //取消
                }

            }
    }

    fun showStepFlowWindow(event: CookStepEvent) {
        recipeNumber = event.recipeNumber
        coverPath = event.coverPath
        step = event.step
        modeName = event.modeName
        isMode = event.isMode
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        EasyFloat.with(globalApp())
            .setShowPattern(ShowPattern.ALL_TIME)
            .setFilter(*filterClass)
            .setLayout(R.layout.step_float_window) {
                ivFood = it.findViewById(R.id.iv_food)
                tvStep = it.findViewById(R.id.tv_step_count)
                ivFood?.load(coverPath)
                if (isMode) {
                    //当前是模式
                    tvStep?.text = modeName
                } else {
                    //当前是菜谱
                    tvStep?.text = "Step ${step + 1}"
                }
                it.findViewById<RelativeLayout>(R.id.content_layout).setOnClickListener {
                    if (isMode) {
                        globalApp().navigateTo(Screens.Cook.COOK_MODES) {
                            withString(MODE_TYPE, modeName)
                        }
                    } else {
                        globalApp().navigateTo(Screens.Cook.COOK_STEP2) {
                            withString("number", recipeNumber)
                            withInt("stepIndex", step)
                        }
                    }
                }
            }
            .setSidePattern(SidePattern.RESULT_SIDE)
            .setImmersionStatusBar(true)
            .setLocation(
                getScreenWidth() - com.develop.base.R.dimen.convert_420px.resId2Dimension().toInt(),
                getScreenHeight() - com.develop.base.R.dimen.convert_206px.resId2Dimension().toInt()

            )
            .show()
    }

    fun hideStepFlowWindow() {
        
        if (EasyFloat.isShow()) {
            EasyFloat.dismiss()
        }
    }

    @Subscribe
    fun onDevStateEvent(event: DevStatusEvent) {
        event.devInfo?.apply {
            ThreadUtils.runOnMainThread {
                if (runningRecipeId != null && runningRecipeId?.isNotEmpty() == true) {
                    recipeNumber = runningRecipeId
                    tvStep?.text = modeName
                } else {
                    step = if (runningRecipeExt == "") 0 else runningRecipeExt.toInt()
                    tvStep?.text = "Step ${step + 1}"
                }
                if (status.toByte() == DevStatus.STOP) {
                    hideStepFlowWindow()
                }
            }
        }
    }

    @Subscribe
    fun onCookStepEvent(event: CookStepEvent) {
        recipeNumber = event.recipeNumber
        coverPath = event.coverPath
        step = event.step
        isMode = event.isMode
        modeName = event.modeName
        ivFood?.load(coverPath)
        if (isMode) {
            tvStep?.text = modeName
        } else {
            tvStep?.text = "Step ${step + 1}"
        }
    }

//    @Subscribe
//    fun onCookDevMsgEvent(event: DevPromptEvent) {
//        TopResumedActivityHolder.getCurrentActivity()?.apply {
//            confirmCancelDialog.title
//            confirmCancelDialog.cancelStr = event.cancelBtnText
//            confirmCancelDialog.confirmStr = event.cancelBtnText
//            confirmCancelDialog.title = event.msg
//            confirmCancelDialog.show(this.supportFragmentManager, "")
//        }
//    }


    private fun getFilterClass(filterClass: FilterClass): Class<*> {
        return when (filterClass) {
            FilterClass.COOK_STEP -> {
                Class.forName("com.develop.step.ui.cook_step.CookStepActivity").newInstance()::class.java
            }
            else -> {
                Class.forName("com.develop.step.ui.ModesDetailActivity")
                    .newInstance()::class.java
            }
        }
    }

    enum class FilterClass {
        COOK_STEP,
        COOK_MODES
    }
}