package com.divya.animaltap.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.divya.animaltap.R
import com.divya.animaltap.util.SoundPlayer
import com.google.android.material.button.MaterialButton

class AnimalDetailActivity : AppCompatActivity() {

    private lateinit var soundPlayer: SoundPlayer
    private var nameSoundRes: Int = 0
    private var animalSoundRes: Int = 0

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

        soundPlayer = SoundPlayer(this)

        val nameRes = intent.getIntExtra(EXTRA_NAME_RES, 0)
        val imageRes = intent.getIntExtra(EXTRA_IMAGE_RES, 0)
        val colorRes = intent.getIntExtra(EXTRA_COLOR_RES, R.color.white)
        nameSoundRes = intent.getIntExtra(EXTRA_NAME_SOUND_RES, 0)
        animalSoundRes = intent.getIntExtra(EXTRA_ANIMAL_SOUND_RES, 0)

        val rootLayout = findViewById<ConstraintLayout>(R.id.detail_root)
        val imgAnimal = findViewById<ImageView>(R.id.img_detail_animal)
        val btnPlay = findViewById<MaterialButton>(R.id.btn_play_sound)
        val btnBack = findViewById<android.widget.ImageButton>(R.id.btn_back)

        // Setup UI
        // rootLayout.setBackgroundColor(ContextCompat.getColor(this, colorRes)) // Override to match mockup sky blue
        rootLayout.setBackgroundColor(android.graphics.Color.parseColor("#B3E5FC"))
        if (imageRes != 0) imgAnimal.setImageResource(imageRes)
        if (nameRes != 0) {
            val name = getString(nameRes)
            btnPlay.text = name
            
            // Dynamic Bubble Text
            val bubbleLeft = findViewById<android.widget.TextView>(R.id.bubble_left)
            bubbleLeft.text = "I'm a $name!"
        }

        // Click Listener logic
        btnPlay.setOnClickListener {
            val nameRes = intent.getIntExtra(EXTRA_NAME_RES, 0)
            if (nameRes != 0) {
                soundPlayer.speakAnimalName(nameRes)
                
                // Fun bounce animation
                btnPlay.animate().scaleX(1.1f).scaleY(1.1f).setDuration(100).withEndAction {
                    btnPlay.animate().scaleX(1f).scaleY(1f).setDuration(100).start()
                }.start()
            }
        }
        
        imgAnimal.setOnClickListener {
            if (animalSoundRes != 0) {
                // Play sound
                soundPlayer.playAnimalSoundEffect(animalSoundRes)
                
                // Animate shake
                imgAnimal.animate().rotation(10f).setDuration(50).withEndAction {
                    imgAnimal.animate().rotation(-10f).setDuration(50).withEndAction {
                         imgAnimal.animate().rotation(0f).setDuration(50).start()
                    }.start()
                }.start()
                
                // Navigate to next animal after a 3 second delay
                imgAnimal.postDelayed({
                    navigateToNextAnimal()
                }, 3000)
            }
        }

        btnBack.setOnClickListener {
            finish()
        }
    }

    private fun navigateToNextAnimal() {
        val currentId = intent.getIntExtra(EXTRA_ID, -1)
        val nextAnimal = com.divya.animaltap.data.AnimalRepository.getNextAnimal(currentId)
        
        val intent = newIntent(
            this,
            nextAnimal.id,
            nextAnimal.nameRes,
            nextAnimal.imageRes,
            nextAnimal.colorRes,
            nextAnimal.nameSoundRes,
            nextAnimal.animalSoundRes
        )
        startActivity(intent)
        // Custom slide animation: Enter from Right, Exit to Left
        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
        // Actually, users usually expect "Next" to come from right.
        // Let's use system default or create simple ones. 
        // For simplicity, let's try a standard transition if defaults aren't perfect.
        // Or actually, simple fade is often nicer? Let's stick to standard Activity transition but force it.
        // To make it distinct: Enter from Right (next comes in), Exit to Left (current goes out).
        // Since we don't have custom anim XMLs handy, we can try using system resources if available, 
        // but typically slide_in_left is for "back". We want slide_in_right.
        // If system R.anim doesn't have it publicly, we might just do finish(). 
        // Let's just finish() the current one to clear stack? 
        // User asked for "Animation". 
        finish() 
    }

    // Removed old playSound method as logic is now split
    
    override fun onDestroy() {
        super.onDestroy()
        soundPlayer.stop()
    }

    companion object {
        private const val EXTRA_ID = "extra_id"
        private const val EXTRA_NAME_RES = "extra_name_res"
        private const val EXTRA_IMAGE_RES = "extra_image_res"
        private const val EXTRA_COLOR_RES = "extra_color_res"
        private const val EXTRA_NAME_SOUND_RES = "extra_name_sound_res"
        private const val EXTRA_ANIMAL_SOUND_RES = "extra_animal_sound_res"

        fun newIntent(context: Context, id: Int, nameRes: Int, imageRes: Int, colorRes: Int, nameSoundRes: Int, animalSoundRes: Int): Intent {
            return Intent(context, AnimalDetailActivity::class.java).apply {
                putExtra(EXTRA_ID, id)
                putExtra(EXTRA_NAME_RES, nameRes)
                putExtra(EXTRA_IMAGE_RES, imageRes)
                putExtra(EXTRA_COLOR_RES, colorRes)
                putExtra(EXTRA_NAME_SOUND_RES, nameSoundRes)
                putExtra(EXTRA_ANIMAL_SOUND_RES, animalSoundRes)
            }
        }
    }
}
