package com.cheng.blzb.ui.fragment.guide.adapter

import android.annotation.SuppressLint
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cheng.blzb.R
import com.cheng.blzb.bean.AreaEntity
import com.example.base.decoration.GridSpaceItemDecoration
import com.example.base.extensions.onClick
import com.example.base.utils.DensityUtils

class GuideItem2Area2Adapter(): BaseQuickAdapter<AreaEntity, BaseViewHolder>(R.layout.listitem_guide_area2) {
    private var mListener: (() -> Unit)? = null

    @SuppressLint("NotifyDataSetChanged")
    override fun convert(holder: BaseViewHolder, item: AreaEntity) {
        val tvName = holder.getView<TextView>(R.id.tv_name)
        val tvCheckAll = holder.getView<TextView>(R.id.tv_check_all)
        val rvCity = holder.getView<RecyclerView>(R.id.rv_city)

        tvName.text = item.name

        if (rvCity.adapter == null) {
            val adapter = GuideItem2CityAdapter()
            rvCity.adapter = adapter
            rvCity.addItemDecoration(GridSpaceItemDecoration(2, DensityUtils.dp2px(10f), DensityUtils.dp2px(10f)))
        }
        val cityAdapter = rvCity.adapter as GuideItem2CityAdapter
        cityAdapter.setList(item.children)

        if (item.id == 0) {
            updateCheckState(tvCheckAll, data.all { area -> area.children.all { it.isChecked } })
        } else {
            updateCheckState(tvCheckAll, cityAdapter.data.all { it.isChecked })
        }

        cityAdapter.setOnItemClickListener { _, _, position ->
            val cityItem = cityAdapter.getItem(position)
            cityItem.isChecked = !cityItem.isChecked
            notifyDataSetChanged()
            mListener?.invoke()
        }

        tvCheckAll.onClick {
            if (item.id == 0) {
                if (data.all { area -> area.children.all { it.isChecked } }) {
                    data.forEach { area ->
                        area.children.forEach { it.isChecked = false }
                    }
                } else {
                    data.forEach { area ->
                        area.children.forEach { it.isChecked = true }
                    }
                }
            } else {
                if (cityAdapter.data.all { it.isChecked }) {
                    cityAdapter.data.forEach { it.isChecked = false }
                } else {
                    cityAdapter.data.forEach { it.isChecked = true }
                }
            }
            notifyDataSetChanged()
            mListener?.invoke()
        }
    }

    private fun updateCheckState(textView: TextView, isCheckAll: Boolean) {
        if (isCheckAll) {
            textView.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_check_true, 0, 0, 0)
        } else {
            textView.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_check_false, 0, 0, 0)
        }
    }

    fun setOnCityClickListener(listener: () -> Unit) {
        this.mListener = listener
    }
}