package com.remax.visualnovel.ui.chat.ui


import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.core.graphics.toColorInt
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.LayoutChatMenuViewBinding
import com.remax.visualnovel.entity.response.ChatBackground
import com.remax.visualnovel.entity.response.ChatBubble
import com.remax.visualnovel.entity.response.ChatHistory
import com.remax.visualnovel.entity.response.ChatMode
import com.remax.visualnovel.entity.response.ChatSound
import com.remax.visualnovel.extension.showConfirmDialog
import com.remax.visualnovel.ui.chat.ui.expandableSelector.ExpandAiModelSelectView
import com.remax.visualnovel.ui.chat.ui.expandableSelector.ExpandBubbleSelectView
import com.remax.visualnovel.ui.chat.ui.expandableSelector.ExpandChatModeSelectView
import com.remax.visualnovel.ui.chat.ui.expandableSelector.ExpandSoundSelectView
import com.remax.visualnovel.ui.chat.ui.expandableSelector.SelectorItem
import com.remax.visualnovel.widget.imageviewer.utils.activity
import java.util.Date

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

    private var mBinding = LayoutChatMenuViewBinding.inflate(LayoutInflater.from(context), this, true)


    init {
        with(mBinding) {
            maxResponseNumView.setEventListener(object: MaxNumView.IEventListener {
                override fun onValueChanged(value: Int) {
                    // TODO -save new value
                }
            })
        }

        initAiModelSelectorView()
        initChatModeSelectorView()
        initSoundSelectorView()
        initBubbleSelectView()
        initBackgroundSelectView()
        initHistoryListView()
        initOtherClickEvent()
    }

    private fun initOtherClickEvent() {
        with (mBinding) {
            llDelete.setOnClickListener {
                activity?.showConfirmDialog (
                    dialogBg = R.drawable.chat_delete_bg,
                    R.string.delete_chat,
                    R.string.delete_chat_hint,
                    R.mipmap.setting_delete,
                    null,
                    R.string.no,
                    R.string.sure,
                    {
                        // TODO - do delete the chat
                    }
                )
            }
        }
    }

    private fun scroll2Position(targetSubView: View) {
        mBinding.scrollView.scroll2ChildView(targetSubView)
    }

    fun initAiModelSelectorView() {
        val items = listOf(
            SelectorItem(
                name = "Max-0618",
                description = "Previous-generation large model",
                pointsInfo = "0.3 points / 1K tokens",
                color = "#FF6B35".toColorInt(),
                isRecommended = true
            ),
            SelectorItem(
                name = "Turbo-0826-32K",
                description = "Previous-generation large model",
                pointsInfo = "0.3 points / 1K tokens",
                color = Color.parseColor("#4285F4")
            ),
            SelectorItem(
                name = "XL-0430",
                description = "Previous-generation large model",
                pointsInfo = "0.3 points / 1K tokens",
                color = Color.parseColor("#34A853")
            ),
            SelectorItem(
                name = "XL-0826-32K",
                description = "Previous-generation large model",
                pointsInfo = "0.3 points / 1K tokens",
                color = Color.parseColor("#FBBC05"),
                isSelected = true // 默认选中
            )
        )

        with(mBinding.aiModelSelector) {
            setTitleIcon(R.mipmap.setting_ai_model)
            setItems(items)
            selectItem(0)
            setOnEventListener(object : ExpandAiModelSelectView.IEventListener {
                override fun onItemSelected(
                    position: Int,
                    item: SelectorItem
                ) {
                    // TODO -
                }

                override fun onExpanded(isExpanded: Boolean) {
                    if (isExpanded)
                        scroll2Position(this@with)
                }
            })
        }
    }

    fun initChatModeSelectorView() {
        val items = listOf(
            ChatMode(
                name = "Mode-1",
                description = "Previous-generation large model",
            ),
            ChatMode(
                name = "Mode-2",
                description = "aaaaaaaaaaaaaaaaaaa",
                onlyVip = true
            ),
            ChatMode(
                name = "Mode-3",
                description = "ccccccccccccccccccccccccc",
            ),
            ChatMode(
                name = "Mode-4",
                description = "Pppppppppppppppppppppp",
                isSelected = true,
                onlyVip = true
            )
        )

        with(mBinding.chatModelSelector) {
            setTitleIcon(R.mipmap.setting_chat_mode_icon)
            setTitleText(R.string.chat_mode)
            setItems(items)
            selectItem(0)
            setOnEventListener(object : ExpandChatModeSelectView.IEventListener {
                override fun onItemSelected(
                    position: Int,
                    item: ChatMode
                ) {

                }

                override fun onExpanded(isExpanded: Boolean) {
                    if (isExpanded)
                        scroll2Position(this@with)
                }

            })
        }
    }


    fun initSoundSelectorView() {
        val items = listOf(
            ChatSound(
                id = 1L,
                name = "Sound-1",
                description = "This is description for sound-1",
                isMale = true,
                imgUrl = ""
            ),
            ChatSound(
                id = 2L,
                name = "Sound-2",
                description = "This is description for sound-2",
                isMale = false,
                imgUrl = ""
            ),

            ChatSound(
                id = 3L,
                name = "Sound-3",
                description = "This is description for sound-3",
                isMale = true,
                imgUrl = ""
            ),

            ChatSound(
                id = 4L,
                name = "Sound-4",
                description = "This is description for sound-4",
                isMale = false,
                imgUrl = ""
            ),

            ChatSound(
                id = 5L,
                name = "Sound-5",
                description = "This is description for sound-5",
                isMale = true,
                imgUrl = ""
            )
        )

        with(mBinding.soundActorSelector) {
            setItems(items)
            setEventListener(object : ExpandSoundSelectView.IEventListener {
                override fun onItemSelected(
                    position: Int,
                    item: ChatSound
                ) {

                }

                override fun onExpanded(isExpanded: Boolean) {
                    if (isExpanded)
                        scroll2Position(this@with)
                }
            })
        }
    }

    fun initBubbleSelectView() {
        val items = listOf(
            ChatBubble(
                id = 1L,
                name = "Bubble-1",
                imgUrl = ""
            ),
            ChatBubble(
                id = 2L,
                name = "Bubble-2",
                imgUrl = ""
            ),

            ChatBubble(
                id = 3L,
                name = "Bubble-3",
                imgUrl = ""
            ),

            ChatBubble(
                id = 4L,
                name = "Bubble-4",
                imgUrl = ""
            ),

            ChatBubble(
                id = 5L,
                name = "Bubble-5",
                imgUrl = ""
            )
        )

        with(mBinding.bubbleSelectView) {
            setTitleText(R.string.chat_bubble)
            setItems(items)
            setOnEventListener(object : ExpandBubbleSelectView.IEventListener {
                override fun onItemSelected(
                    position: Int,
                    item: ChatBubble
                ) {

                }

                override fun onExpanded(isExpanded: Boolean) {
                    if (isExpanded)
                        scroll2Position(this@with)
                }
            })
        }
    }

    fun initBackgroundSelectView() {
        val items = listOf(
            ChatBackground(
                backgroundId = 1,
                imgUrl = "https://cdhrss.chengdu.gov.cn/cdrsj/xhtml/images/2022_logo.png",
            ),
            ChatBackground(
                backgroundId = 1,
                imgUrl = "https://cdhrss.chengdu.gov.cn/cdrsj/xhtml/images/2022_logo.png",
            ),
            ChatBackground(
                backgroundId = 1,
                imgUrl = "https://cdhrss.chengdu.gov.cn/cdrsj/xhtml/images/2022_logo.png",
            ),
            ChatBackground(
                backgroundId = 1,
                imgUrl = "https://cdhrss.chengdu.gov.cn/cdrsj/xhtml/images/2022_logo.png",
            ),
            ChatBackground(
                backgroundId = 1,
                imgUrl = "https://cdhrss.chengdu.gov.cn/cdrsj/xhtml/images/2022_logo.png",
            ),
            ChatBackground(
                backgroundId = 1,
                imgUrl = "https://cdhrss.chengdu.gov.cn/cdrsj/xhtml/images/2022_logo.png",
            ),
            ChatBackground(
                backgroundId = 1,
                imgUrl = "https://cdhrss.chengdu.gov.cn/cdrsj/xhtml/images/2022_logo.png",
                deletable = true
            ),
            ChatBackground(
                isUploadLocalItem = true
            )
        )

        mBinding.backgroundSelectorView.setItems(items)
    }


    fun initHistoryListView() {
        val items = listOf(
            ChatHistory(
                imgUrl = "",
                time = Date().time - 3600*100*1,
                describle = "上次聊天记录 11111",
            ),
            ChatHistory(
                imgUrl = "",
                time = Date().time - 3600*100*2,
                describle = "上次聊天记录 2222",
            ),
            ChatHistory(
                imgUrl = "",
                time = Date().time - 3600*100*3,
                describle = "上次聊天记录 333",
            ),
            ChatHistory(
                imgUrl = "",
                time = Date().time - 3600*100*4,
                describle = "上次聊天记录 444",
            ),ChatHistory(
                imgUrl = "",
                time = Date().time - 3600*100*5,
                describle = "上次聊天记录 5555",
            )

        )

        mBinding.historySelectView.setItems(items)
    }



}
