package com.example.timert

import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class AddWorkout : AppCompatActivity() {
    private var isSave = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_add_workout)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val buttonSaveAll: Button = findViewById(R.id.btn_save_all)
        buttonSaveAll.setOnClickListener {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            isSave = true

        }
        val button_on_main_all: Button = findViewById(R.id.btn_on_main_all)
        button_on_main_all.setOnClickListener {
            if (isSave == false) {
                AlertDialog.Builder(this)
                    .setTitle("Изменения не сохранены")
                    .setMessage("Продолжить без сохранения?")
                    .setPositiveButton("Да") { _, _ ->

                        val intent = Intent(this, MainActivity::class.java)
                        startActivity(intent)
                    }
                    .setNegativeButton("Нет") { dialog, _ ->
                        dialog.dismiss()
                    }
                    .show()
            } else {
                val intent = Intent(this, MainActivity::class.java)
                startActivity(intent)
            }
        }
        val button_add: Button = findViewById(R.id.add_workoutadd)
        button_add.setOnClickListener {
            val intent = Intent(this, CreatingTraining::class.java)
            startActivity(intent)
        }
        val timeInputRestSetAll: EditText = findViewById(R.id.rest_sets_all)
        val btnPlusTimeAll: ImageView = findViewById(R.id.add_res_set2)
        val btnMinusTimeAll: ImageView = findViewById(R.id.del_res_set2)

        val setsInputAll: EditText = findViewById(R.id.numbers_of_sets_all)
        val btnPlusSetsAll: ImageView = findViewById(R.id.add_set3)
        val btnMinusSetsAll: ImageView = findViewById(R.id.del_set3)

        val timeInputRest: EditText = findViewById(R.id.rest_workout_all)
        val btnPlusTimeWor: ImageView = findViewById(R.id.add_res)
        val btnMinusTimeWor: ImageView = findViewById(R.id.del_res)

        val timeWork: EditText = findViewById(R.id.time_work_all)
        val Plus: ImageView = findViewById(R.id.add_set)
        val Minus: ImageView = findViewById(R.id.del_set)

        setupPlusMinus(timeInputRestSetAll, btnPlusTimeAll, btnMinusTimeAll, minValue = 5, maxValue = 600)
        setupPlusMinus(setsInputAll, btnPlusSetsAll, btnMinusSetsAll, minValue = 1, maxValue = 20)
        setupPlusMinus(timeInputRest, btnPlusTimeWor, btnMinusTimeWor, minValue = 5, maxValue = 600)
        setupPlusMinus(timeWork, Plus, Minus, minValue = 5, maxValue = 600)


    }
    fun setupPlusMinus(
        editText: EditText,
        plus: ImageView,
        minus: ImageView,
        minValue: Int = 5,
        maxValue: Int = 600
    ) {
        plus.setOnClickListener {
            val current = editText.text.toString().toIntOrNull() ?: minValue
            val newValue = (current + 1).coerceAtMost(maxValue)
            editText.setText(newValue.toString())
        }

        minus.setOnClickListener {
            val current = editText.text.toString().toIntOrNull() ?: minValue
            val newValue = (current - 1).coerceAtLeast(minValue)
            editText.setText(newValue.toString())
        }
    }
}