package com.grkj.iscs_mc.view.widget

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.appcompat.content.res.AppCompatResources
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.grkj.iscs_mc.R
import com.grkj.iscs_mc.databinding.LayoutExpandableTabLayoutBinding
import com.grkj.iscs_mc.util.ToastUtils
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder

/**
 * 伸缩/展开 Tab列表
 */
class ExpandableTabLayout(private val ctx: Context, attrs: AttributeSet) :
    RelativeLayout(ctx, attrs) {

    companion object {
        const val DOWNWARDS = 0
        const val UPWARDS = 1
    }

    private var mBinding: LayoutExpandableTabLayoutBinding
    private var isCollapse = true
    private var mDataList = mutableListOf<ItemData>()
    private var mOnSelectListener: OnSelectListener? = null
    private var isInteractive = false
    private var mSelectIndex = 0
    private var mDirection: Int = DOWNWARDS

    init {
        val root = View.inflate(ctx, R.layout.layout_expandable_tab_layout, this)
        mBinding = LayoutExpandableTabLayoutBinding.bind(root)

        val attrSet = ctx.obtainStyledAttributes(attrs, R.styleable.ExpandableTabLayout)

        mDirection = attrSet.getInt(R.styleable.ExpandableTabLayout_expand_direction, DOWNWARDS)
        if (mDirection == UPWARDS) {
            mBinding.ivAction.rotation = 180f
            mBinding.root.setBackgroundResource(R.drawable.expandable_tab_bg_bottom)
        } else {
            mBinding.root.setBackgroundResource(R.drawable.expandable_tab_bg_top)
        }

        // 是否要联动下划线
        isInteractive = attrSet.getBoolean(R.styleable.ExpandableTabLayout_interactive, false)

        val title = attrSet.getString(R.styleable.ExpandableTabLayout_expand_title)
        title?.let {
            mBinding.tvTitle.text = title
        } ?: let {
            mBinding.tvTitle.visibility = View.GONE
        }

        attrSet.recycle()

        mBinding.rvTab.adapter =
            object : CommonAdapter<ItemData>(
                ctx,
                R.layout.item_rv_expandable_tab,
//                mutableListOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56","57", "58", "59", "60", "61")
                mDataList
            ) {
                override fun convert(holder: ViewHolder, data: ItemData, position: Int) {
                    holder.setVisible(R.id.iv_indicator, isInteractive && position == mSelectIndex)
                    data.icon?.let {
                        holder.getView<ImageView>(R.id.iv_icon).setImageResource(it)
                    }
                    data.iconUrl?.let {
                        Glide.with(this@ExpandableTabLayout).load(it).into(holder.getView(R.id.iv_icon))
                    }
                    data.title?.let {
                        holder.setText(R.id.tv_name, it)
                    }
                    holder.setOnClickListener(R.id.root) {
                        if (isInteractive) {
                            mOnSelectListener?.onSelect(position)
                            mBinding.rvTab.adapter?.notifyDataSetChanged()
                            mSelectIndex = position
                        }
                    }
                }
            }
        mBinding.rvTab.isNestedScrollingEnabled = false

        mBinding.ivAction.setOnClickListener {
            if (isCollapse) {
                mBinding.root.layoutParams = mBinding.root.layoutParams.apply {
                    height = ctx.resources.getDimensionPixelSize(R.dimen.expandable_tab_expand_height)
                }
                mBinding.root.requestLayout()

                val layoutManager = GridLayoutManager(ctx, 5)
                mBinding.rvTab.layoutManager = layoutManager

                mBinding.llContainer.orientation = LinearLayout.VERTICAL
            } else {
                mBinding.root.layoutParams = mBinding.root.layoutParams.apply {
                    height = ctx.resources.getDimensionPixelSize(R.dimen.expandable_tab_collapse_height)
                }

                val layoutManager = LinearLayoutManager(ctx)
                layoutManager.orientation = LinearLayoutManager.HORIZONTAL
                mBinding.rvTab.layoutManager = layoutManager

                mBinding.llContainer.orientation = LinearLayout.HORIZONTAL
            }
            isCollapse = !isCollapse

            mBinding.ivAction.rotation = if (isCollapse) {
                if (mDirection == DOWNWARDS) 0f else 180f
            } else {
                if (mDirection == DOWNWARDS) 180f else 0f
            }
        }
    }

    fun setData(list: MutableList<ItemData>) {
        mDataList.clear()
        mDataList.addAll(list)
        mBinding.rvTab.adapter?.notifyDataSetChanged()
    }

    fun setOnSelectListener(onSelectListener: OnSelectListener) {
        mOnSelectListener = onSelectListener
    }

    interface OnSelectListener {
        fun onSelect(position: Int)
    }

    data class ItemData(val title: String?, val icon: Int? = null, val iconUrl: String? = null)
}