//
//  AddAlarmView.swift
//  TheNoiseClock
//
//  Created by Matt Bruce on 9/7/25.
//

import SwiftUI
import AudioPlaybackKit

/// View for creating new alarms with iOS-native style interface
struct AddAlarmView: View {
    
    // MARK: - Properties
    let viewModel: AlarmViewModel
    @Binding var isPresented: Bool
    
    @State private var newAlarmTime = Calendar.current.date(bySettingHour: 6, minute: 0, second: 0, of: Date()) ?? Date()
    @State private var selectedSoundName = "digital-alarm.caf"
    @State private var alarmLabel = "Alarm"
    @State private var notificationMessage = "Your alarm is ringing"
    @State private var snoozeDuration = 9 // minutes
    @State private var isVibrationEnabled = true
    @State private var isLightFlashEnabled = false
    @State private var volume: Float = 1.0
    
    var body: some View {
        NavigationView {
            VStack(spacing: 0) {
                // Time picker section at top
                TimePickerSection(selectedTime: $newAlarmTime)
                TimeUntilAlarmSection(alarmTime: newAlarmTime)
                
                // List for settings below
                List {
                    // Label Section
                    NavigationLink(destination: LabelEditView(label: $alarmLabel)) {
                        HStack {
                            Image(systemName: "textformat")
                                .foregroundColor(AppAccent.primary)
                                .frame(width: 24)
                            Text("Label")
                            Spacer()
                            Text(alarmLabel)
                                .foregroundColor(.secondary)
                        }
                    }
                    
                    // Notification Message Section
                    NavigationLink(destination: NotificationMessageEditView(message: $notificationMessage)) {
                        HStack {
                            Image(systemName: "message")
                                .foregroundColor(AppAccent.primary)
                                .frame(width: 24)
                            Text("Message")
                            Spacer()
                            Text(notificationMessage)
                                .foregroundColor(.secondary)
                                .lineLimit(1)
                        }
                    }
                    
                    // Sound Section
                    NavigationLink(destination: SoundSelectionView(selectedSound: $selectedSoundName)) {
                        HStack {
                            Image(systemName: "music.note")
                                .foregroundColor(AppAccent.primary)
                                .frame(width: 24)
                            Text("Sound")
                            Spacer()
                            Text(getSoundDisplayName(selectedSoundName))
                                .foregroundColor(.secondary)
                        }
                    }
                    
                    // Snooze Section
                    NavigationLink(destination: SnoozeSelectionView(snoozeDuration: $snoozeDuration)) {
                        HStack {
                            Image(systemName: "clock.arrow.circlepath")
                                .foregroundColor(AppAccent.primary)
                                .frame(width: 24)
                            Text("Snooze")
                            Spacer()
                            Text("for \(snoozeDuration) min")
                                .foregroundColor(.secondary)
                        }
                    }
                }
                .listStyle(.insetGrouped)
            }
            .navigationTitle("Alarm")
            .navigationBarTitleDisplayMode(.inline)
            .navigationBarBackButtonHidden(true)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Button("Cancel") {
                        isPresented = false
                    }
                    .foregroundColor(AppAccent.primary)
                }
                
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("Save") {
                        Task {
                            let newAlarm = viewModel.createNewAlarm(
                                time: newAlarmTime,
                                soundName: selectedSoundName,
                                label: alarmLabel,
                                notificationMessage: notificationMessage,
                                snoozeDuration: snoozeDuration,
                                isVibrationEnabled: isVibrationEnabled,
                                isLightFlashEnabled: isLightFlashEnabled,
                                volume: volume
                            )
                            await viewModel.addAlarm(newAlarm)
                            isPresented = false
                        }
                    }
                    .foregroundColor(AppAccent.primary)
                    .fontWeight(.semibold)
                }
            }
        }
    }
    
    // MARK: - Helper Methods
    private func getSoundDisplayName(_ fileName: String) -> String {
        return AlarmSoundService.shared.getSoundDisplayName(fileName)
    }
}
