//
//  Countdown+Extension.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 25/01/2023.
//

import Foundation
import SwiftUI
import CoreData

enum TimerError: Error {
    case notificationAuthorizationMissing
}

extension AbstractSoundTimer {
    
    var sounds: Set<Sound> {
        if let soundList {
            return Set(soundList.enumItems())
        }
        return []
    }
    
//    var playlists: [Playlist] {
//        if let playlistList {
//            return playlistList.enumItems()
//        }
//        return []
//    }
    
    func setSounds(_ sounds: Set<Sound>) {
        self.soundList = sounds.stringRepresentation
    }
    
//    func setPlaylists(_ playlists: [Playlist]) {
//        self.playlistList = playlists.stringRepresentation
//    }
    
    var coolSound: Sound {
        var allSounds: [Sound] = []
        allSounds.append(contentsOf: self.sounds)
        return allSounds.randomElement() ?? Sound.allCases[0]
    }
    
    var soundName: String {
        return self.coolSound.soundName
    }
    
}

extension Stopwatch {
    
    var coolSound: Sound? {
        return Sound(rawValue: Int(self.sound)) ?? nil
    }

    static func fake(context: NSManagedObjectContext) -> Stopwatch {
        let stopwatch = Stopwatch(context: context)
        let activity = Activity(context: context)
        activity.name = "Run"
        stopwatch.activity = activity
        return stopwatch
    }

}

extension Record {
    
    public override func didChangeValue(forKey key: String) {
        super.didChangeValue(forKey: key)
        
        switch key {
        case "start", "end":
            self.computeDuration()
        default:
            break
        }
    }
    
    func computeDuration() {
        if let start, let end {
            self.duration = end.timeIntervalSince(start)
        }
    }
    
    var details: String {
        if let start, let end {
            return "\(start.formatted()) - \(end.formatted())"
        } else {
            return "no details"
        }
    }
        
}

extension Activity {
    
    fileprivate static var formatter: NumberFormatter = NumberFormatter()
    
    var recordCount: String {
        let count: Int = self.records?.count ?? 0
        return Activity.formatter.string(from: NSNumber(value: count)) ?? "--"
    }
    
}

extension CustomSound : Localized {
    
    var localizedString: String {
        return self.text ?? ""
    }
    
}


// MARK: - Storage convenience

fileprivate let separator = "|"
fileprivate let formatter: NumberFormatter = NumberFormatter()

extension String {
    
    func enumItems<T : RawRepresentable<Int>>() -> [T] {
        let ids: [String] = self.components(separatedBy: separator)
        let intIds: [Int] = ids.compactMap { formatter.number(from: $0)?.intValue }
        return intIds.compactMap { T(rawValue: $0) }
    }
    
}

extension Sequence where Element : RawRepresentable<Int> {
    
    var stringRepresentation: String {
        let ids = self.compactMap { formatter.string(from: NSNumber(value: $0.rawValue)) }
        return ids.joined(separator: separator)
    }
    
}
