package com.remax.visualnovel.ui.chat

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Point
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.FrameLayout
import com.dylanc.viewbinding.nonreflection.inflate
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.ChatInputpanelBinding
import com.remax.visualnovel.ui.chat.ChatActivity.Companion.MODE_TEXT
import com.remax.visualnovel.ui.chat.ChatActivity.Companion.MODE_VOICE


class InputPanel @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
    : FrameLayout(context, attrs, defStyleAttr) {

    private var binding: ChatInputpanelBinding
    private var mMode = MODE_TEXT
    private lateinit var mEventListener: IEventListener


    interface IEventListener {
        fun onEnterCall()
        fun onEnterShortChat()
        fun onSendText(content: CharSequence)
        fun onMsgRvScroll2Bottom()
        fun onCheckVipStatus()
    }


    init {
        binding = inflate(ChatInputpanelBinding::inflate)
        binding.run {
            chatPopMenu.setMenuList(mutableListOf(
                PopMenuIconView.MenuItem(R.mipmap.chat_ai_talk) {
                    mEventListener.onEnterShortChat()
                },
                PopMenuIconView.MenuItem(R.mipmap.chat_ai_phone) {
                    mEventListener.onEnterCall()
                }
            ))

            ivChatModeSwitcher.setOnClickListener {
                switchMode(if (mMode == MODE_TEXT) MODE_VOICE else MODE_TEXT)
            }

            ivChatSend.setOnClickListener {
                chatEditView.clearText()
                mEventListener.onSendText(chatEditView.text as CharSequence)
            }

            ivGotoBottom.setOnClickListener {
                mEventListener.onMsgRvScroll2Bottom()
            }

            ivChatVip.setOnClickListener {
                mEventListener.onCheckVipStatus()
            }
        }

        switchMode(mMode)
    }

    fun setEventListener(listener: IEventListener) {
        this.mEventListener = listener
    }

    private fun switchMode(newMode: Int) {
        if (newMode != mMode) {
            mMode = newMode

            binding.run {
                when (mMode) {
                    MODE_TEXT-> {
                        ivChatModeSwitcher.setImageResource(R.mipmap.chat_voice_b)
                        ivChatSend.visibility = VISIBLE
                        ivHold2talk.visibility = GONE
                        chatEditView.visibility = VISIBLE
                    }

                    MODE_VOICE-> {
                        ivChatModeSwitcher.setImageResource(R.mipmap.chat_keyboard)
                        ivChatSend.visibility = GONE
                        ivHold2talk.visibility = VISIBLE
                        chatEditView.visibility = GONE
                    }
                }
            }
        }
    }


    @SuppressLint("ClickableViewAccessibility")
    fun holdToTalk(callback: () -> Unit, cancelCallback: () -> Unit, touchPosCallback: (point: Point) -> Unit) {
        binding.ivHold2talk.run {
            setOnTouchListener { v, event ->
                when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        callback.invoke()
                    }

                    MotionEvent.ACTION_UP -> {
                        cancelCallback.invoke()
                    }

                    MotionEvent.ACTION_MOVE -> {
                        touchPosCallback.invoke(Point(event.rawX.toInt(), event.rawY.toInt()))
                    }
                }
                true
            }
        }
    }


}