import SwiftUI

struct AddAlarmView: View {
    @Binding var alarms: [Alarm]
    let systemSounds: [String]
    @Binding var newAlarmTime: Date
    @Binding var selectedSoundName: String
    @Binding var showAddAlarm: Bool
    
    var body: some View {
        NavigationView {
            VStack(spacing: 20) {
                DatePicker("Time", selection: $newAlarmTime, displayedComponents: .hourAndMinute)
                    .datePickerStyle(.wheel)
                Picker("Sound", selection: $selectedSoundName) {
                    ForEach(systemSounds, id: \.self) { sound in
                        Text(sound.capitalized).tag(sound)
                    }
                }
                .pickerStyle(.menu)
                HStack {
                    Button("Cancel") {
                        showAddAlarm = false
                    }
                    .padding()
                    Spacer()
                    Button("Add Alarm") {
                        let newAlarm = Alarm(id: UUID(), time: newAlarmTime, isEnabled: true, soundName: selectedSoundName)
                        alarms.append(newAlarm)
                        // Update notifications handled by AlarmView's onChange
                        showAddAlarm = false
                    }
                    .padding()
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(8)
                }
            }
            .padding()
            .navigationTitle("New Alarm")
            .navigationBarTitleDisplayMode(.inline)
        }
    }
}

#Preview {
    AddAlarmView(alarms: .constant([]), systemSounds: ["default", "bell", "chimes"], newAlarmTime: .constant(Date()), selectedSoundName: .constant("default"), showAddAlarm: .constant(true))
}

