package com.adealink.weparty.playmate.list.comp

import androidx.core.graphics.toColorInt
import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.router.Router
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.drawabletoolbox.GradientBorderBuilder
import com.adealink.weparty.commonui.drawabletoolbox.GradientBorderDrawable
import com.adealink.weparty.commonui.ext.dpf
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.module.category.Category
import com.adealink.weparty.module.playmate.data.PlaymateCategoryData
import com.adealink.weparty.playmate.databinding.LayoutPlaymateHomeListTopBinding
import com.adealink.weparty.playmate.databinding.LayoutPlaymateHomeListTopItemBinding

class PlaymateTopCategoryComp(
    lifecycleOwner: LifecycleOwner,
    val binding: LayoutPlaymateHomeListTopBinding,
    val category: PlaymateCategoryData?,
    val onCategorySelected: (category: PlaymateCategoryData) -> Unit
) : ViewComponent(lifecycleOwner) {

    private val categoryViewMap =
        mutableMapOf<PlaymateCategoryData, LayoutPlaymateHomeListTopItemBinding>()

    override fun onCreate() {
        super.onCreate()
        initView()
        loadData()
    }

    private fun initView() {
        binding.clMore.onClick {
            clickMore()
        }

        binding.vItem1.ivItemFrame.background = SELECT_DRAWABLE
        binding.vItem2.ivItemFrame.background = SELECT_DRAWABLE
        binding.vItem3.ivItemFrame.background = SELECT_DRAWABLE
    }

    private fun loadData() {
        updateTopCategory(category?.subCategoryList)
    }

    fun selectCategory(category: PlaymateCategoryData?) {
        if (!isValid) {
            return
        }
        if (category == null) {
            for (itemBinding in categoryViewMap.values) {
                itemBinding.vSelected.gone()
                itemBinding.ivItemFrame.gone()
            }
            return
        }
        for (entry in categoryViewMap) {
            if (entry.key == category) {
                entry.value.vSelected.show()
                entry.value.ivItemFrame.show()
            } else {
                entry.value.vSelected.gone()
                entry.value.ivItemFrame.gone()
            }
        }
    }

    private fun updateTopCategory(category: List<PlaymateCategoryData>?) {
        if (category.isNullOrEmpty()) {
            binding.root.gone()
            categoryViewMap.clear()
            return
        }
        binding.root.show()
        categoryViewMap.clear()

        val item3 = category.getOrNull(2)
        if (item3 == null) {
            binding.vItem3.root.gone()
            binding.vItem3.root.setOnClickListener(null)
        } else {
            categoryViewMap[item3] = binding.vItem3

            binding.vItem3.root.show()
            binding.vItem3.root.onClick {
                onCategorySelected.invoke(item3)
            }
            binding.vItem3.ivItem.setImageUrl(item3.icon)
            binding.vItem3.tvItem.text = item3.name
        }

        val item2 = category.getOrNull(1)
        if (item2 == null) {
            binding.vItem2.root.gone()
            binding.vItem2.root.setOnClickListener(null)
        } else {
            categoryViewMap[item2] = binding.vItem2

            binding.vItem2.root.show()
            binding.vItem2.root.onClick {
                onCategorySelected.invoke(item2)
            }
            binding.vItem2.ivItem.setImageUrl(item2.icon)
            binding.vItem2.tvItem.text = item2.name
        }

        val item1 = category.getOrNull(0)
        if (item1 == null) {
            binding.vItem1.root.gone()
            binding.vItem1.root.setOnClickListener(null)
        } else {
            categoryViewMap[item1] = binding.vItem1

            binding.vItem1.root.show()
            binding.vItem1.root.onClick {
                onCategorySelected.invoke(item1)
            }
            binding.vItem1.ivItem.setImageUrl(item1.icon)
            binding.vItem1.tvItem.text = item1.name
        }
    }

    private fun clickMore() {
        val act = activity ?: return
        Router.build(act, Category.CategoryList.PATH).start()
    }

    companion object {
        val SELECT_DRAWABLE: GradientBorderDrawable = GradientBorderBuilder()
            .setRadii(
                floatArrayOf(
                    50.dpf(),
                    50.dpf(),
                    50.dpf(),
                    50.dpf(),
                    50.dpf(),
                    50.dpf(),
                    50.dpf(),
                    50.dpf()
                )
            )
            .setBorderWidth(1.dpf())
            .setBorderAngle(315)
            .setBorderColors(intArrayOf("#4ED2FF".toColorInt(), "#B1EF5D".toColorInt()))
            .build()
    }
}