package com.daodaoshi.chick_mood

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.util.Log
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.widget.Button
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import android.os.Vibrator
import android.os.VibrationEffect
import com.chick_mood.data.model.Emotion
import java.text.DecimalFormat

/**
 * 添加心情页面
 * 通过摇晃手机来记录心情强度的核心功能页面
 */
class AddActivity : AppCompatActivity(), SensorEventListener {

    // UI组件
    private lateinit var btnBack: ImageView
    private lateinit var tvTitle: TextView
    private lateinit var tvMoodText: TextView
    private lateinit var tvMoodValue: TextView
    private lateinit var progressBar: ProgressBar
    private lateinit var ivChick: ImageView
    private lateinit var btnEnd: Button
    private lateinit var viewOverlay: View
    private lateinit var tvOverlayText: TextView

    // 传感器相关
    private lateinit var sensorManager: SensorManager
    private var accelerometer: Sensor? = null
    private var isSensorRegistered = false

    // 震动反馈
    private lateinit var vibrator: Vibrator

    // 心情数据
    private var selectedEmotion: Emotion? = null
    private var currentMoodValue = 0
    private val maxMoodValue = 100

    // 遮罩引导系统
    private val handler = Handler(Looper.getMainLooper())
    private var overlayState = OVERLAY_STATE_INITIAL

    // 摇晃检测相关
    private var lastShakeTime = 0L
    private var shakeThreshold = 15f // 摇晃强度阈值
    private val moodValueFormatter = DecimalFormat("#")

    companion object {
        private const val TAG = "AddActivity"
        private const val EXTRA_EMOTION_TYPE = "emotion_type"

        // 遮罩状态
        private const val OVERLAY_STATE_INITIAL = 0
        private const val OVERLAY_STATE_READY = 1
        private const val OVERLAY_STATE_HIDDEN = 2

        // 遮罩显示时间（毫秒）
        private const val OVERLAY_INITIAL_DURATION = 2000L
        private const val OVERLAY_READY_DURATION = 2000L

        // 心情值累计参数
        private const val SHAKE_COOLDOWN = 100L // 摇晃冷却时间（毫秒）
        private const val MOOD_INCREMENT_BASE = 2 // 基础心情值增量

        /**
         * 启动AddActivity
         * @param context 上下文
         * @param emotion 选择的心情类型
         */
        fun start(context: Context, emotion: Emotion) {
            val intent = Intent(context, AddActivity::class.java).apply {
                putExtra(EXTRA_EMOTION_TYPE, emotion.name)
            }
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add)

        // 获取传递的心情类型
        selectedEmotion = intent.getStringExtra(EXTRA_EMOTION_TYPE)?.let {
            Emotion.valueOf(it)
        } ?: Emotion.HAPPY

        initViews()
        initSensor()
        startOverlaySequence()
    }

    /**
     * 初始化视图组件
     */
    private fun initViews() {
        btnBack = findViewById(R.id.btn_back)
        tvTitle = findViewById(R.id.tv_title)
        tvMoodText = findViewById(R.id.tv_mood_text)
        tvMoodValue = findViewById(R.id.tv_mood_value)
        progressBar = findViewById(R.id.progress_bar)
        ivChick = findViewById(R.id.iv_chick)
        btnEnd = findViewById(R.id.btn_end)
        viewOverlay = findViewById(R.id.view_overlay)
        tvOverlayText = findViewById(R.id.tv_overlay_text)

        // 设置标题
        tvTitle.text = "添加心情"

        // 根据心情类型设置小鸡图标
        selectedEmotion?.let { emotion ->
            val chickIcon = getEmotionIconRes(emotion)
            ivChick.setImageResource(chickIcon)
        }

        // 初始化UI状态
        resetUI()

        // 设置点击监听器
        btnBack.setOnClickListener {
            finish()
        }

        btnEnd.setOnClickListener {
            onEndButtonClicked()
        }

        // 遮罩点击事件（可选：允许用户手动关闭遮罩）
        viewOverlay.setOnClickListener {
            // 可以选择是否允许用户手动关闭遮罩
            // hideOverlay()
        }
    }

    /**
     * 初始化传感器和震动器
     */
    private fun initSensor() {
        sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)

        // 初始化震动器
        vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator

        if (accelerometer == null) {
            Toast.makeText(this, "无传感器", Toast.LENGTH_SHORT).show()
        }
    }

    /**
     * 开始遮罩引导序列
     */
    private fun startOverlaySequence() {
        overlayState = OVERLAY_STATE_INITIAL
        viewOverlay.visibility = View.VISIBLE
        tvOverlayText.visibility = View.VISIBLE
        tvOverlayText.text = "握紧手机"

        // 添加淡入动画
        val fadeIn = AlphaAnimation(0f, 1f)
        fadeIn.duration = 300
        viewOverlay.startAnimation(fadeIn)
        tvOverlayText.startAnimation(fadeIn)

        // 2秒后切换到准备状态
        handler.postDelayed({
            if (overlayState == OVERLAY_STATE_INITIAL) {
                overlayState = OVERLAY_STATE_READY
                tvOverlayText.text = "开始摇晃，记录心情"

                // 再过2秒后隐藏遮罩
                handler.postDelayed({
                    hideOverlay()
                }, OVERLAY_READY_DURATION)
            }
        }, OVERLAY_INITIAL_DURATION)
    }

    /**
     * 隐藏遮罩
     */
    private fun hideOverlay() {
        if (overlayState != OVERLAY_STATE_HIDDEN) {
            overlayState = OVERLAY_STATE_HIDDEN

            // 添加淡出动画
            val fadeOut = AlphaAnimation(1f, 0f)
            fadeOut.duration = 300
            fadeOut.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationStart(animation: Animation) {}
                override fun onAnimationEnd(animation: Animation) {
                    viewOverlay.visibility = View.GONE
                    tvOverlayText.visibility = View.GONE
                    // 开始监听摇晃
                    startShakeDetection()
                }
                override fun onAnimationRepeat(animation: Animation) {}
            })
            viewOverlay.startAnimation(fadeOut)
            tvOverlayText.startAnimation(fadeOut)
        }
    }

    /**
     * 开始摇晃检测
     */
    private fun startShakeDetection() {
        if (!isSensorRegistered && accelerometer != null) {
            sensorManager.registerListener(
                this,
                accelerometer,
                SensorManager.SENSOR_DELAY_NORMAL
            )
            isSensorRegistered = true
        }
    }

    /**
     * 停止摇晃检测
     */
    private fun stopShakeDetection() {
        if (isSensorRegistered) {
            sensorManager.unregisterListener(this)
            isSensorRegistered = false
        }
    }

    /**
     * 重置UI状态
     */
    private fun resetUI() {
        currentMoodValue = 0
        tvMoodText.text = "开始！"
        tvMoodValue.text = moodValueFormatter.format(currentMoodValue)
        progressBar.progress = 0
    }

    /**
     * 结束按钮点击处理
     */
    private fun onEndButtonClicked() {
        stopShakeDetection()

        // 创建心情记录数据
        val currentTime = System.currentTimeMillis()

        // 跳转到编辑页面
        EditActivity.startForAdd(
            context = this,
            emotion = selectedEmotion!!,
            moodValue = currentMoodValue,
            timestamp = currentTime
        )

        finish()
    }

    /**
     * 更新心情值和UI
     */
    private fun updateMoodValue(increment: Int) {
        currentMoodValue = (currentMoodValue + increment).coerceAtMost(maxMoodValue)

        // 更新UI
        tvMoodValue.text = moodValueFormatter.format(currentMoodValue)
        progressBar.progress = currentMoodValue

        // 更新心情文案
        updateMoodText()
    }

    /**
     * 更新心情文案
     */
    private fun updateMoodText() {
        val moodText = generateMoodText(currentMoodValue, selectedEmotion!!)
        tvMoodText.text = moodText
    }

    /**
     * 生成心情文案
     */
    private fun generateMoodText(value: Int, emotion: Emotion): String {
        val degree = when {
            value <= 20 -> "有些"
            value <= 40 -> "非常"
            value <= 60 -> "超级"
            value <= 80 -> "这也太"
            else -> "完全无法控制"
        }
        return "$degree ${emotion.displayName}"
    }

    /**
     * 获取情绪对应的图标资源
     */
    private fun getEmotionIconRes(emotion: Emotion): Int {
        return when (emotion) {
            Emotion.HAPPY -> R.drawable.ic_placeholder_chick_happy
            Emotion.ANGRY -> R.drawable.ic_placeholder_chick_angry
            Emotion.SAD -> R.drawable.ic_placeholder_chick_sad
            Emotion.WORRIED -> R.drawable.ic_placeholder_chick_worried
            Emotion.LONELY -> R.drawable.ic_placeholder_chick_lonely
            Emotion.SCARED -> R.drawable.ic_placeholder_chick_scared
        }
    }

    // SensorEventListener 回调方法
    override fun onSensorChanged(event: SensorEvent?) {
        event?.let {
            if (it.sensor.type == Sensor.TYPE_ACCELEROMETER) {
                val currentTime = System.currentTimeMillis()

                // 防止过于频繁的检测
                if (currentTime - lastShakeTime < SHAKE_COOLDOWN) {
                    return
                }

                val x = it.values[0]
                val y = it.values[1]
                val z = it.values[2]

                // 计算加速度的平方和
                val accelerationSquare = x * x + y * y + z * z

                // 计算总的加速度强度
                val acceleration = Math.sqrt(accelerationSquare.toDouble()).toFloat()

                // 检测是否达到摇晃阈值
                if (acceleration > shakeThreshold) {
                    lastShakeTime = currentTime

                    // 提供震动反馈
                    provideVibrationFeedback()

                    // 根据摇晃强度计算心情值增量
                    val increment = calculateMoodIncrement(acceleration)
                    updateMoodValue(increment)
                }
            }
        }
    }

    /**
     * 提供震动反馈
     */
    private fun provideVibrationFeedback() {
        try {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                // Android 8.0及以上使用VibrationEffect
                val vibrationEffect = VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE)
                vibrator.vibrate(vibrationEffect)
            } else {
                // 旧版本使用传统方法
                @Suppress("DEPRECATION")
                vibrator.vibrate(100)
            }
        } catch (e: Exception) {
            // 震动功能失败不影响核心功能
            Log.d(TAG, "震动反馈失败", e)
        }
    }

    /**
     * 根据摇晃强度计算心情值增量
     */
    private fun calculateMoodIncrement(acceleration: Float): Int {
        // 基础增量 + 根据摇晃强度的额外增量
        val baseIncrement = MOOD_INCREMENT_BASE
        val bonusIncrement = ((acceleration - shakeThreshold) / 10).toInt().coerceAtLeast(0)
        return baseIncrement + bonusIncrement
    }

    override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
        // 不需要处理
    }

    override fun onResume() {
        super.onResume()
        // 如果遮罩已经隐藏，重新开始传感器监听
        if (overlayState == OVERLAY_STATE_HIDDEN) {
            startShakeDetection()
        }
    }

    override fun onPause() {
        super.onPause()
        stopShakeDetection()
        handler.removeCallbacksAndMessages(null)
    }

    override fun onDestroy() {
        super.onDestroy()
        stopShakeDetection()
        handler.removeCallbacksAndMessages(null)
    }
}