package com.remax.visualnovel.ui.Chat

import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout
import android.widget.Toast
import com.dylanc.viewbinding.nonreflection.inflate
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.ChatInputpanelBinding


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

    companion object {
        private const val MODE_TEXT = 1;
        private const val MODE_VOICE = 2;
    }


    init {
        binding = inflate(ChatInputpanelBinding::inflate)
        binding.run {
            chatPopMenu.setMenuList(mutableListOf(
                PopMenuIconView.MenuItem(R.mipmap.chat_ai_talk) {
                    Toast.makeText(context, "聊天", Toast.LENGTH_SHORT).show()
                },
                PopMenuIconView.MenuItem(R.mipmap.chat_ai_phone) {
                    Toast.makeText(context, "通话", Toast.LENGTH_SHORT).show()
                }
            ))

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

            ivChatSend.setOnClickListener {
                chatEditView.clearText()
            }
        }

        switchMode(mMode)
    }


    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
                    }
                }
            }
        }
    }


}