package com.develop.food.base.manager

import android.app.Activity
import android.util.Log
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.appcompat.widget.AppCompatTextView
import com.bumptech.glide.Glide
import com.develop.base.R
import com.develop.food.base.common.MODE_TYPE
import com.develop.food.base.event.CookStepEvent
import com.develop.food.base.router.Screens
import com.develop.food.base.router.navigateTo
import com.develop.food.base.utils.*
import com.develop.food.base.widgets.CancelConfirmDialog
import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevStatus
import com.kuyuntech.cofarcooking.device.sdk.eventbus.event.DevPromptEvent
import com.kuyuntech.cofarcooking.device.sdk.eventbus.event.DevStatusEvent
import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK
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

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

    val confirmCancelDialog by lazy {
        CancelConfirmDialog()
    }

    init {
        filterClass = arrayOf(
            getFilterClass(FilterClass.COOK_MODES),
            getFilterClass(FilterClass.COOK_STEP),
        )
//        EventBus.getDefault().register(this)

        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?.apply {
                    Glide.with(GlobalApp()).load(coverPath).into(this)
                }
                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(
                DensityUtil.getScreenWidth(GlobalApp()) - ResDimension(R.dimen.convert_420px).toInt(),
                DensityUtil.getScreenHeight(GlobalApp()) - ResDimension(R.dimen.convert_206px).toInt()

            )
            .show()
    }

    fun hideStepFlowWindow() {
        EventBus.getDefault().unregister(this)
        EasyFloat.dismiss()
    }

    @Subscribe
    fun onDevStateEvent(event: DevStatusEvent) {
        event.devInfo?.apply {
            ThreadUtils.runOnMainThread {
                Log.d("FloatWindowManager", toJson())
                if (runningRecipeId?.isNotEmpty() == true) {
                    recipeNumber = runningRecipeId
                }

                if (!StringUtils.isNotEmpty(runningRecipeId)) {
                    tvStep?.text = modeName
                } else {
                    step = if (runningRecipeExt == "") 0 else runningRecipeExt.toInt()
                    tvStep?.text = "Step ${step + 1}"
                }

                if (CofarSDK.devInfo().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?.apply {
            Glide.with(GlobalApp()).load(coverPath).into(this)
        }
        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.CookStepActivity2").newInstance()::class.java
            }
            else -> {
                Class.forName("com.develop.step.modes.ModesDetailActivity")
                    .newInstance()::class.java
            }
        }
    }

    enum class FilterClass {
        COOK_STEP,
        COOK_MODES
    }
}