//
//  Settings.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 26.03.2021.
//

import UIKit
import Localize_Swift

@propertyWrapper
struct UserDefaultsValue<T> {
    var wrappedValue: T {
        get {
            let value = UserDefaults.standard.value(forKey: key) as? T
            return value ?? defaultValue
        }
        set {
            UserDefaults.standard.set(newValue, forKey: key)
            UserDefaults.standard.synchronize()
        }
    }
    
    private let key: String
    private let defaultValue: T

    init(wrappedValue defaultValue:T, key:String) {
        self.defaultValue = defaultValue
        self.key = key
    }
}

class Settings {
    static let shared = Settings()
    private init() {}
    
    @UserDefaultsValue(key: "app_theme")
    var appTheme = AppTheme.system
    
    @UserDefaultsValue(key: "app_theme_auto")
    var automaticSwitchTheme = false
    
    @UserDefaultsValue(key: "temperature_type")
    var temperatureType = TemperatureType.celsius
    
    @UserDefaultsValue(key: "wind_speed_type")
    var windSpeedType = WindSpeedType.milesPerHour
    
    @UserDefaultsValue(key: "pressure_type")
    var pressureType = PressureType.millibars
    
    @UserDefaultsValue(key: "distance_type")
    var distanceType = DistanceType.miles
    
    var locale:Locale {
        return Locale(identifier: Localize.currentLanguage())
    }
}
