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.EmptyPlaceholderViewBinder
import com.atmob.keyboard_android.component.item.KeyboardSelectViewBinder
import com.atmob.keyboard_android.constant.Constants
import com.atmob.keyboard_android.ext.click
import com.atmob.keyboard_android.model.EmptyPlaceholderModel
import com.atmob.keyboard_android.model.KeyboardSelectModel
import com.atmob.keyboard_android.util.recyclerview.GridDivider
import com.blankj.utilcode.util.ConvertUtils
import me.drakeet.multitype.Items
import me.drakeet.multitype.MultiTypeAdapter

/**
 * 键盘选择页
 */
class KeyboardSelectComponent @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseUIComponent(context, attrs, defStyleAttr) {
    private lateinit var vBackBtn: View
    private lateinit var vList: RecyclerView
    private lateinit var vSaveBtn: View

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

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

    override fun findView(view: View) {
        vBackBtn = view.findViewById(R.id.back_btn)
        vList = view.findViewById(R.id.list)
        vSaveBtn = view.findViewById(R.id.save_btn)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun bindView(view: View) {
        vBackBtn.click {
            // 关闭选择页
        }
        vSaveBtn.click {
            // 保存设置
        }

        // 配置列表
        vList.apply {
            mListItems = Items()
            mListAdapter = MultiTypeAdapter(mListItems).apply {
                // 空占位条目
                register(EmptyPlaceholderModel::class.java, EmptyPlaceholderViewBinder())
                // 键盘条目
                register(KeyboardSelectModel::class.java, KeyboardSelectViewBinder { item ->
                    // 先全部取消选中，再选中当前设置的键盘
                    mListItems.forEachIndexed { index, item ->
                        if (item is KeyboardSelectModel) {
                            item.isSelected = false
                        }
                    }
                    val targetPosition = mListItems.indexOf(item)
                    val targetItem = mListItems[targetPosition] as KeyboardSelectModel
                    targetItem.isSelected = true
                    notifyDataSetChanged()
                })
            }
            layoutManager = GridLayoutManager(context, Constants.KEYBOARD_SELECT_SPAN_COUNT)
            adapter = mListAdapter
            addItemDecoration(
                GridDivider(
                    spanCount = Constants.KEYBOARD_SELECT_SPAN_COUNT,
                    spacing = ConvertUtils.dp2px(9f)
                )
            )
        }

        setData()
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun setData() {
        // TODO: 加载键盘列表
        val emptyPlaceholderItemHeight = ConvertUtils.dp2px(60f)
        mListItems.apply {
            // 添加空占位条目
            add(EmptyPlaceholderModel(emptyPlaceholderItemHeight))
            add(EmptyPlaceholderModel(emptyPlaceholderItemHeight))
            add(EmptyPlaceholderModel(emptyPlaceholderItemHeight))
            // 添加键盘条目
            add(
                KeyboardSelectModel(
                    "",
                    "通用键盘",
                    isSelected = true,
                    iconDefaultResId = R.mipmap.ic_common_keyboard_icon,
                    isCommonKeyboard = true
                )
            )
            add(KeyboardSelectModel("", "小蕊"))
            add(KeyboardSelectModel("", "小胡"))
            add(KeyboardSelectModel("", "自己&小蕊"))
            add(KeyboardSelectModel("", "迪丽热巴"))
            add(KeyboardSelectModel("", "哇咔咔"))
            add(KeyboardSelectModel("", "小明"))
            add(KeyboardSelectModel("", "小红"))
            add(KeyboardSelectModel("", "龙哥"))
        }
        mListAdapter.notifyDataSetChanged()
    }
}