//
//  CountdownScheduler.swift
//  Countdown
//
//  Created by Laurent Morvillier on 15/01/2023.
//

import Foundation
import UserNotifications

class CountdownScheduler {
    
    static let master = CountdownScheduler()
    
    func scheduleIfPossible(countdown: Countdown, handler: @escaping (Result<Date?, Error>) -> Void) {
        self.cancelCurrentNotifications(countdownId: countdown.stringId)
        self._scheduleCountdownNotification(countdown: countdown, handler: handler)
    }
    
    func cancelCurrentNotifications(countdownId: String) {
        UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [countdownId])
//        Conductor.maestro.cancelCountdown(id: countdownId)
    }
    
    fileprivate func _scheduleCountdownNotification(countdown: Countdown, handler: @escaping (Result<Date?, Error>) -> Void) {
        let content = UNMutableNotificationContent()
        content.title = NSLocalizedString("It's time!", comment: "")
        
        let duration = countdown.duration
        let body: String
        if let name = countdown.activity?.name {
            body = NSLocalizedString("Time's up for \(name)!", comment: "")
        } else {
            body = NSLocalizedString("Your \(duration.minuteSecond) countdown is over!", comment: "")
        }
        
        content.body = body
        content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: countdown.soundName))
        content.interruptionLevel = .critical

        self._createNotification(countdown: countdown, content: content, handler: handler)
        
//        if countdown.repeats {
//            for i in 1...50 {
//                let offset = Double(i) * (countdown.coolSound.duration + 1.0)
////                self._createNotification(countdown: countdown, offset: offset, content: content, handler: handler)
//            }
//        }
        
    }
    
    fileprivate func _createNotification(countdown: Countdown, offset: TimeInterval = 0.0, content: UNMutableNotificationContent, handler: @escaping (Result<Date?, Error>) -> Void) {
        
        let duration = countdown.duration
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: duration + offset, repeats: false)
        let request = UNNotificationRequest(identifier: countdown.objectID.uriRepresentation().absoluteString,
                                            content: content,
                                            trigger: trigger)
        UNUserNotificationCenter.current().add(request) { error in
            DispatchQueue.main.async {
                if let error {
                    handler(.failure(error))
                    print("Scheduling error = \(error)")
                } else {
                    
                    if offset == 0.0 {
                        if let triggerDate = trigger.nextTriggerDate() {
                            Conductor.maestro.startCountdown(triggerDate, countdown: countdown)
                            handler(.success(trigger.nextTriggerDate()))
                        } else {
                            let backupDate = Date().addingTimeInterval(duration)
                            Conductor.maestro.startCountdown(backupDate, countdown: countdown)
                        }
                    }
                }
            }
        }
        
    }
    
}
