package com.chick_mood.ui.emotion

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.GridView
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.daodaoshi.chick_mood.R
import com.daodaoshi.chick_mood.databinding.DialogEmotionSelectorBinding
import com.chick_mood.data.model.Emotion

/**
 * 情绪选择对话框
 *
 * 提供六种基础情绪的选择界面
 * 使用BottomSheet样式的对话框
 * 支持情绪预览和选择回调
 *
 * @author Claude
 * @date 2025-10-23
 */
class EmotionSelectorDialog private constructor(
    private val context: Context,
    private val onEmotionSelected: (Emotion) -> Unit
) {

    private var binding: DialogEmotionSelectorBinding
    private var dialog: AlertDialog
    private var selectedEmotion: Emotion? = null

    init {
        binding = DialogEmotionSelectorBinding.inflate(LayoutInflater.from(context))

        dialog = AlertDialog.Builder(context)
            .setView(binding.root)
            .create()

        setupUI()
    }

    /**
     * 设置UI组件
     */
    private fun setupUI() {
        // 设置情绪网格
        val emotionAdapter = EmotionAdapter(context, getEmotionItems())
        binding.emotionGridContainer.adapter = emotionAdapter

        // 设置网格点击事件
        binding.emotionGridContainer.setOnItemClickListener { _, _, position, _ ->
            val emotionItems = getEmotionItems()
            selectedEmotion = emotionItems[position].emotion

            // 更新选中状态
            emotionAdapter.setSelectedPosition(position)

            // 显示确认按钮
            binding.btnConfirm.visibility = View.VISIBLE
        }

        // 设置按钮点击事件
        binding.btnCancel.setOnClickListener {
            dismiss()
        }

        binding.btnConfirm.setOnClickListener {
            selectedEmotion?.let { emotion ->
                onEmotionSelected(emotion)
                dismiss()
            }
        }
    }

    /**
     * 获取情绪数据项
     */
    private fun getEmotionItems(): List<EmotionItem> {
        return listOf(
            EmotionItem(Emotion.HAPPY, "开心", R.drawable.ic_emotion_happy, R.color.emotion_happy),
            EmotionItem(Emotion.SAD, "悲伤", R.drawable.ic_emotion_sad, R.color.emotion_sad),
            EmotionItem(Emotion.ANGRY, "生气", R.drawable.ic_emotion_angry, R.color.emotion_angry),
            EmotionItem(Emotion.WORRIED, "烦恼", R.drawable.ic_emotion_worried, R.color.emotion_worried),
            EmotionItem(Emotion.LONELY, "孤单", R.drawable.ic_emotion_lonely, R.color.emotion_lonely),
            EmotionItem(Emotion.SCARED, "害怕", R.drawable.ic_emotion_scared, R.color.emotion_scared)
        )
    }

    /**
     * 显示对话框
     */
    fun show() {
        dialog.show()
    }

    /**
     * 隐藏对话框
     */
    fun dismiss() {
        dialog.dismiss()
    }

    /**
     * 情绪数据项
     */
    private data class EmotionItem(
        val emotion: Emotion,
        val name: String,
        val iconRes: Int,
        val colorRes: Int
    )

    /**
     * 情绪网格适配器
     */
    private inner class EmotionAdapter(
        private val context: Context,
        private val emotionItems: List<EmotionItem>
    ) : BaseAdapter() {

        private var selectedPosition = -1

        fun setSelectedPosition(position: Int) {
            selectedPosition = position
            notifyDataSetChanged()
        }

        override fun getCount(): Int = emotionItems.size

        override fun getItem(position: Int): Any = emotionItems[position]

        override fun getItemId(position: Int): Long = position.toLong()

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            val view = convertView ?: LayoutInflater.from(context)
                .inflate(R.layout.item_emotion_grid, parent, false)

            val item = emotionItems[position]

            val iconView = view.findViewById<ImageView>(R.id.iv_emotion_icon)
            val nameView = view.findViewById<TextView>(R.id.tv_emotion_name)
            val container = view.findViewById<ViewGroup>(R.id.emotion_container)

            // 设置图标和文字
            iconView.setImageResource(item.iconRes)
            nameView.text = item.name

            // 设置选中状态
            if (position == selectedPosition) {
                container.setBackgroundResource(R.drawable.bg_emotion_selected)
                nameView.setTextColor(context.getColor(R.color.white))
            } else {
                container.setBackgroundColor(context.getColor(android.R.color.transparent))
                nameView.setTextColor(context.getColor(R.color.text_primary))
            }

            return view
        }
    }

    companion object {
        /**
         * 创建情绪选择对话框实例
         *
         * @param context 上下文
         * @param onEmotionSelected 情绪选择回调
         * @return EmotionSelectorDialog实例
         */
        fun create(
            context: Context,
            onEmotionSelected: (Emotion) -> Unit
        ): EmotionSelectorDialog {
            return EmotionSelectorDialog(context, onEmotionSelected)
        }
    }
}