package com.atmob.keyboard_android.component.child.impl

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
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.base.animator.impl.TransitionAnimator
import com.atmob.keyboard_android.component.child.IAiChatComponent
import com.atmob.keyboard_android.component.item.AiChatViewBinder
import com.atmob.keyboard_android.component.item.EmptyPlaceholderViewBinder
import com.atmob.keyboard_android.constant.Constants
import com.atmob.keyboard_android.ext.click
import com.atmob.keyboard_android.model.AiChatModel
import com.atmob.keyboard_android.model.EmptyPlaceholderModel
import com.atmob.keyboard_android.util.InputMethodUtil
import com.atmob.keyboard_android.util.KeyboardHolder
import com.atmob.keyboard_android.util.recyclerview.LinearDivider
import com.blankj.utilcode.util.ConvertUtils
import me.drakeet.multitype.Items
import me.drakeet.multitype.MultiTypeAdapter

/**
 * Ai对话组件
 */
class AiChatComponent @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseUIComponent<IAiChatComponent>(context, attrs, defStyleAttr), IAiChatComponent {
    private lateinit var vBackBtn: View
    private lateinit var vReGenerateBtn: View
    private lateinit var vChatList: RecyclerView

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

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

    override fun findView(view: View) {
        vBackBtn = view.findViewById(R.id.back_btn)
        vReGenerateBtn = view.findViewById(R.id.re_generate_btn)
        vChatList = view.findViewById(R.id.chat_list)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun bindView(view: View) {
        vBackBtn.click {
            // 关闭面板
            hide()
        }
        vReGenerateBtn.click {
            // 重新生成
        }
        vChatList.apply {
            mListItems = Items()
            mListAdapter = MultiTypeAdapter(mListItems).apply {
                // 空占位条目
                register(EmptyPlaceholderModel::class.java, EmptyPlaceholderViewBinder())
                // 生成内容条目
                register(AiChatModel::class.java, AiChatViewBinder { item ->
                    // 先全部取消选中
                    mListItems.filterIsInstance<AiChatModel>().map {
                        it.isSelected = false
                    }
                    // 再选中当前选择的
                    val targetPosition = mListItems.indexOf(item)
                    val targetItem = mListItems[targetPosition]
                    if (targetItem is AiChatModel) {
                        targetItem.isSelected = true
                        notifyDataSetChanged()
                        // 设置文本到输入框中
                        KeyboardHolder.getKeyboardService()?.asInputMethodService()?.let {
                            InputMethodUtil.inputText(it.currentInputConnection, targetItem.text)
                        }
                    }
                })
            }
            // 方向
            val orientation = RecyclerView.VERTICAL
            layoutManager = LinearLayoutManager(context, orientation, false)
            adapter = mListAdapter
            // 分割线
            addItemDecoration(LinearDivider(ConvertUtils.dp2px(8f), orientation, true))
        }

        setupViewModel()
        setData()
    }

    override fun getComponentInterfaceClazz(): Class<IAiChatComponent> {
        return IAiChatComponent::class.java
    }

    override fun getComponentAnimator(): Class<*> {
        return if (Constants.COMPONENT_ANIMATOR_ENABLE) {
            TransitionAnimator::class.java
        } else {
            super.getComponentAnimator()
        }
    }

    /**
     * 配置ViewModel
     */
    private fun setupViewModel() {
    }

    override fun onComponentShow() {
        super.onComponentShow()
        KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
            ?.updateAiChatPageShowing(true)
    }

    override fun onComponentHide() {
        super.onComponentHide()
        KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
            ?.updateAiChatPageShowing(false)
    }

    /**
     * 设置数据
     */
    @SuppressLint("NotifyDataSetChanged")
    private fun setData() {
        mListItems.apply {
            // 空占位条目
            add(EmptyPlaceholderModel(ConvertUtils.dp2px(40f)))
            // 生成内容条目
            add(AiChatModel("巴拉巴拉小魔仙？呵，这种无聊的东西，以后不准再提。", true))
            add(AiChatModel("巴拉巴拉小魔仙？呵，这种无聊的东西"))
            add(AiChatModel("巴拉巴拉小魔仙？"))
            add(AiChatModel("巴拉巴拉小魔仙，好看好看"))
            add(AiChatModel("巴拉巴拉小魔仙，什么呀，没听过"))
        }
        mListAdapter.notifyDataSetChanged()
    }
}