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.ImageButton
import android.widget.ImageView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.timert.data.adapter.ExerciseAdapter
import com.example.timert.data.viewModel.ExerciseViewModel

class CreatingTraining : AppCompatActivity() {

    private lateinit var viewModel: ExerciseViewModel

    private var isSave = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_creating_training)




        viewModel = ViewModelProvider(
            this,
            ViewModelProvider.AndroidViewModelFactory.getInstance(application)
        )[ExerciseViewModel::class.java]


        val adapter = ExerciseAdapter { id ->
            viewModel.deleteExerciseById(id)
        }
       // adapter = ExerciseAdapter(this)
        val recyclerView = findViewById<RecyclerView>(R.id.exercise)
        recyclerView.adapter = adapter
        recyclerView.layoutManager = LinearLayoutManager(this)


        viewModel.allExercise.observe(this) { exercises ->
            exercises?.let {
                adapter.setExercises(it)
            }
        }
        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 buttonSave: Button = findViewById(R.id.btn_save)
        buttonSave.setOnClickListener {

            isSave=true
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)

        }
        val timeInputRest: EditText = findViewById(R.id.rest_sets)
        val btnPlusTime: ImageView = findViewById(R.id.add_res)
        val btnMinusTime: ImageView = findViewById(R.id.del_res)

        val setsInput: EditText = findViewById(R.id.numbers_of_sets)
        val btnPlusSets: ImageView = findViewById(R.id.add_set)
        val btnMinusSets: ImageView = findViewById(R.id.del_set)

        setupPlusMinus(timeInputRest, btnPlusTime, btnMinusTime, minValue = 5, maxValue = 600)
        setupPlusMinus(setsInput, btnPlusSets, btnMinusSets, minValue = 1, maxValue = 20)
        val button_on_main: Button = findViewById(R.id.btn_on_main)
        button_on_main.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_workout)
        button_add.setOnClickListener {
            val intent = Intent(this, AllTraining::class.java)
            startActivity(intent)
        }

    }
    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())
        }
    }
}