package com.example.livingai.ui.page

import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageButton
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.example.livingai.R
import com.example.livingai.camera.CameraActivity
import java.io.File

class FullScreenImageActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_full_screen_image)
        
        val imagePath = intent.getStringExtra("IMAGE_PATH")
        val allowRetake = intent.getBooleanExtra("ALLOW_RETAKE", false)
        val cowName = intent.getStringExtra("COW_NAME")
        val orientation = intent.getStringExtra("ORIENTATION")
        val silhouetteId = intent.getIntExtra("SILHOUETTE_ID", 0)

        if (imagePath != null) {
            val file = File(imagePath)
            if (file.exists()) {
                val bitmap = BitmapFactory.decodeFile(file.absolutePath)
                findViewById<ImageView>(R.id.fullScreenImageView).setImageBitmap(bitmap)
            }
        }
        
        val btnRetake = findViewById<Button>(R.id.btnRetake)
        if (allowRetake && imagePath != null) {
            btnRetake.visibility = View.VISIBLE
            btnRetake.text = getString(R.string.btn_retake)
            
            btnRetake.setOnClickListener {
                // Launch camera to retake. Pass the current image path so CameraActivity can overwrite/delete it on success.
                val intent = Intent(this, CameraActivity::class.java)
                intent.putExtra("SILHOUETTE_ID", silhouetteId)
                intent.putExtra("COW_NAME", cowName)
                intent.putExtra("ORIENTATION", orientation)
                intent.putExtra("RETAKE_IMAGE_PATH", imagePath) // Pass image path to replace
                startActivity(intent)
                finish()
            }
        } else {
            btnRetake.visibility = View.GONE
        }

        val btnBack = findViewById<ImageButton>(R.id.btnBack)
        btnBack.contentDescription = getString(R.string.content_desc_back)
        btnBack.setOnClickListener {
            finish()
        }
        
        findViewById<ImageView>(R.id.fullScreenImageView).contentDescription = getString(R.string.content_desc_full_screen_image)
    }
}