//
//  Preferences.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 16/02/2023.
//

import Foundation

enum PreferenceKey: String {
    case countdowns
    case stopwatches
    case playConfirmationSound
    case showSilentModeAlert
    case soundDurations
    case lastSoundPlayed
    case tips
    case timerSiriTips
}

class Preferences {
    
    @UserDefault(PreferenceKey.countdowns.rawValue, defaultValue: [:]) static var savedCountdowns: [String : DateInterval]
    @UserDefault(PreferenceKey.soundDurations.rawValue, defaultValue: [:]) static var soundDurations: [Int : TimeInterval]
    @UserDefault(PreferenceKey.lastSoundPlayed.rawValue, defaultValue: [:]) static var lastSelectedSound: [String : Int]
    @UserDefault(PreferenceKey.tips.rawValue, defaultValue: nil) static var lastShownTip: Int?
    @UserDefault(PreferenceKey.timerSiriTips.rawValue, defaultValue: []) static var timerSiriTips: Set<String>
    @UserDefault(PreferenceKey.playConfirmationSound.rawValue, defaultValue: true) static var playConfirmationSound: Bool

    static var hideSilentModeAlerts: Bool {
        return UserDefaults.standard.bool(forKey: PreferenceKey.showSilentModeAlert.rawValue)
    }
    
    static func hideSilentModeAlerts(_ hide: Bool) {
        UserDefaults.standard.setValue(true, forKey: PreferenceKey.showSilentModeAlert.rawValue)
    }
    
    static var tipToShow: Tip? {
        if let tipRawValue = self.lastShownTip, let tip = Tip(rawValue: tipRawValue) {
            return Tip(rawValue: tip.rawValue + 1)
        }
        return Tip.allCases.first
    }
    
}
