//
//  CreateWorkoutSupersetView.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 12/19/24.
//
import SwiftUI

struct CreateWorkoutSupersetView: View {
    @Binding var selectedCreateWorkoutSuperSet: CreateWorkoutSuperSet?
    @Binding var showAddExercise: Bool
    @Binding var superset: CreateWorkoutSuperSet
    @ObservedObject var viewModel: WorkoutViewModel
    
    var body: some View {
        Section(content: {
            AddSupersetView(
                createWorkoutSuperSet: $superset,
                viewModel: viewModel,
                selectedCreateWorkoutSuperSet: $selectedCreateWorkoutSuperSet)
        }, header: {
            VStack {
                HStack {
                    TextField("Superset Title", text: $superset.title)
                        .font(.title2)
                }
                
                VStack {
                    HStack {
                        Text("Exercises: \(superset.exercises.count)")
                            .font(.subheadline)
                            .bold()
                        
                        Spacer()
                        
                        Button(action: {
                            selectedCreateWorkoutSuperSet = $superset.wrappedValue
                            showAddExercise.toggle()
                        }, label: {
                            Image(systemName: "dumbbell.fill")
                                .font(.title2)
                        })
                        
                        Divider()
                        
                        Button(action: {
                            viewModel.delete(superset: $superset.wrappedValue)
                            //viewModel.increaseRandomNumberForUpdating()
                            viewModel.objectWillChange.send()
                        }, label: {
                            Image(systemName: "trash")
                                .font(.title2)
                        })
                    }
                    
                    Divider()
                    
                    Stepper(label: {
                        HStack {
                            Text("Rounds: ")
                            
                            Text("\($superset.wrappedValue.numberOfRounds)")
                                .foregroundColor($superset.wrappedValue.numberOfRounds > 0 ? Color(uiColor: .label) : .red)
                                .bold()
                        }
                    }, onIncrement: {
                        $superset.wrappedValue.increaseNumberOfRounds()
                        //viewModel.increaseRandomNumberForUpdating()
                        viewModel.objectWillChange.send()
                    }, onDecrement: {
                        $superset.wrappedValue.decreaseNumberOfRounds()
                        //viewModel.increaseRandomNumberForUpdating()
                        viewModel.objectWillChange.send()
                    })
                }
            }
        })
    }
}
