package com.example.myapplication

import android.os.Bundle
import android.os.CountDownTimer
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class CookingModeActivity : AppCompatActivity() {

    private var currentStepIndex = 0
    private var timer: CountDownTimer? = null
    private var remainingTime: Long = 60_000 // Standardzeit pro Schritt: 1 Minute
    private var isPaused = true
    private lateinit var steps: List<String> // Liste der Schritte für das Rezept

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Theme basierend auf Einstellungen setzen
        val preferences = getSharedPreferences("appSettings", MODE_PRIVATE)
        val savedColorOption = preferences.getInt("colorOption", 1)
        setTheme(if (savedColorOption == 1) R.style.Theme_myapplication else R.style.Theme_MyApp_BlueTheme)

        setContentView(R.layout.activity_cooking_mode)

        // Rezept und Schritte aus Intent abrufen
        val recipe = intent.getParcelableExtra<Recipe>("RECIPE")
        steps = recipe?.steps ?: emptyList()

        // UI-Elemente initialisieren
        val currentStepText = findViewById<TextView>(R.id.currentStepText)
        val timerText = findViewById<TextView>(R.id.timerText)
        val startPauseButton = findViewById<Button>(R.id.startPauseButton)
        val nextStepButton = findViewById<Button>(R.id.nextStepButton)

        // Button-Text basierend auf dem Timer-Zustand setzen
        startPauseButton.text = if (isPaused) "Pause" else "Start"

        // UI initialisieren
        updateUI(currentStepText, timerText, startPauseButton, nextStepButton)

        // Start/Pause-Button Logik
        startPauseButton.setOnClickListener {
            if (isPaused) {
                startTimer(timerText)
                startPauseButton.text = "Pause"
                Toast.makeText(this, "Zeit läuft weiter", Toast.LENGTH_SHORT).show()
            } else {
                pauseTimer()
                startPauseButton.text = "Start"
                Toast.makeText(this, "Zeit angehalten", Toast.LENGTH_SHORT).show()
            }
        }

        // Nächster Schritt-Button Logik
        nextStepButton.setOnClickListener {
            pauseTimer()
            currentStepIndex++
            updateUI(currentStepText, timerText, startPauseButton, nextStepButton)
            startPauseButton.text = "Pause"
        }
    }

    private fun startTimer(timerText: TextView) {
        timer?.cancel() // Vorherigen Timer abbrechen
        timer = object : CountDownTimer(remainingTime, 1_000) {
            override fun onTick(millisUntilFinished: Long) {
                remainingTime = millisUntilFinished
                val minutes = millisUntilFinished / 60_000
                val seconds = (millisUntilFinished / 1_000) % 60
                timerText.text = String.format("%02d:%02d", minutes, seconds)
            }

            override fun onFinish() {
                currentStepIndex++
                updateUI(
                    findViewById(R.id.currentStepText),
                    findViewById(R.id.timerText),
                    findViewById(R.id.startPauseButton),
                    findViewById(R.id.nextStepButton)
                )
            }
        }.start()
        isPaused = false
    }

    private fun pauseTimer() {
        timer?.cancel()
        isPaused = true
    }

    private fun updateUI(
        currentStepText: TextView,
        timerText: TextView,
        startPauseButton: Button,
        nextStepButton: Button
    ) {
        if (currentStepIndex < steps.size) {
            currentStepText.text = steps[currentStepIndex]
            remainingTime = 60_000
            startTimer(timerText)
        } else {
            // Alle Schritte abgeschlossen
            currentStepText.text = "Alle Schritte abgeschlossen!"
            timer?.cancel()
            startPauseButton.isEnabled = false
            nextStepButton.isEnabled = false
            timerText.text = "00:00"
        }
    }
}