package com.atmob.keyboard_android.component

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.atmob.keyboard_android.R
import com.atmob.keyboard_android.component.base.BaseUIComponent
import com.atmob.keyboard_android.component.item.AiKeyboardKeyViewBinder
import com.atmob.keyboard_android.ext.click
import com.atmob.keyboard_android.model.AiKeyboardKeyModel
import com.atmob.keyboard_android.util.recyclerview.GridDivider
import com.blankj.utilcode.util.ConvertUtils
import me.drakeet.multitype.Items
import me.drakeet.multitype.MultiTypeAdapter

/**
 * AI键盘内容组件
 */
class AiKeyboardContentComponent @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseUIComponent(context, attrs, defStyleAttr) {
    private lateinit var vKeyList: RecyclerView
    private lateinit var vDeleteBtn: View
    private lateinit var vClearBtn: View
    private lateinit var vSendBtn: View

    private lateinit var mListItems: Items
    private lateinit var mListAdapter: MultiTypeAdapter

    override fun onInflateViewId(): Int {
        return R.layout.component_ai_keyboard_content
    }

    override fun findView(view: View) {
        vKeyList = view.findViewById(R.id.key_list)
        vDeleteBtn = view.findViewById(R.id.delete_btn)
        vClearBtn = view.findViewById(R.id.clear_btn)
        vSendBtn = view.findViewById(R.id.send_btn)
    }

    override fun bindView(view: View) {
        setupKeyList()
        setupActionBtn()
        setData()
    }

    /**
     * 设置列表
     */
    private fun setupKeyList() {
        // 列数
        val spanCount = 3
        vKeyList.apply {
            mListItems = Items()
            mListAdapter = MultiTypeAdapter(mListItems).apply {
                register(AiKeyboardKeyModel::class.java, AiKeyboardKeyViewBinder {
                    // 点击键盘按键
                })
            }
            layoutManager = GridLayoutManager(context, spanCount)
            adapter = mListAdapter
            // 分割线
            addItemDecoration(GridDivider(spanCount, ConvertUtils.dp2px(6f)))
        }
    }

    /**
     * 设置操作按钮
     */
    private fun setupActionBtn() {
        vDeleteBtn.click {
        }
        vClearBtn.click {
        }
        vSendBtn.click {
        }
    }

    /**
     * 填充数据
     */
    @SuppressLint("NotifyDataSetChanged")
    private fun setData() {
        mListItems.apply {
            add(AiKeyboardKeyModel("暧昧推拉"))
            add(AiKeyboardKeyModel("欲拒还迎"))
            add(AiKeyboardKeyModel("哄女友"))
            add(AiKeyboardKeyModel("暧昧推拉"))
            add(AiKeyboardKeyModel("甜蜜暴击"))
            add(AiKeyboardKeyModel("暧昧推拉"))
            add(AiKeyboardKeyModel("彩虹屁专家"))
            add(AiKeyboardKeyModel("恋爱脑"))
            add(AiKeyboardKeyModel("调皮回复"))
        }
        mListAdapter.notifyDataSetChanged()
    }
}