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

import UIKit
import Localize_Swift

@propertyWrapper
struct UserDefaultsUnitValue<T> {
    var wrappedValue: T {
        get {
            guard
                let data = UserDefaults.standard.data(forKey: key),
                let decoded  = NSKeyedUnarchiver.unarchiveObject(with: data) as? T
            else {
                return defaultValue
            }
            
            return decoded
        }
        set {
            
            let data = NSKeyedArchiver.archivedData(withRootObject: newValue)
            UserDefaults.standard.set(data, forKey: key)
            UserDefaults.standard.synchronize()
            
            Settings.shared.delegate.invoke { (delegate) in
                delegate.settingsDidChange()
            }
        }
    }
    
    private let key: String
    private let defaultValue: T

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

@propertyWrapper
struct UserDefaultsBasicValue<T> {
    var wrappedValue: T {
        get {
            let value = UserDefaults.standard.value(forKey: key) as? T
            return value ?? defaultValue
        }
        
        set {
            UserDefaults.standard.setValue(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
    }
}

protocol SettingsDelegate:class {
    func settingsDidChange()
}

class Settings {
    static let shared = Settings()
    let delegate = MulticastDelegate<SettingsDelegate>()
    private init() {}
    
    @UserDefaultsBasicValue(key: "app_theme")
    private var _appTheme = AppTheme.system.rawValue
    
    public var appTheme:AppTheme {
        get {
            return AppTheme(rawValue: _appTheme) ?? .system
        }
        set {
            _appTheme = newValue.rawValue
            
            if #available(iOS 13, *) {
                switch newValue {
                case .light:
                    UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = .light
                case .dark:
                    UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = .dark
                case .system:
                    UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = .unspecified
                }
            }
            
            Settings.shared.delegate.invoke { (delegate) in
                delegate.settingsDidChange()
            }
        }
    }
    
    @UserDefaultsUnitValue(key: "temperature_type")
    public var temperatureType = UnitTemperature.celsius
    
    @UserDefaultsUnitValue(key: "wind_speed_type")
    public var windSpeedType = UnitSpeed.milesPerHour
    
    @UserDefaultsUnitValue(key: "pressure_type")
    public var pressureType = UnitPressure.millibars
    
    @UserDefaultsUnitValue(key: "distance_type")
    public var distanceType = UnitLength.miles
    
    public var locale:Locale {
        return Locale(identifier: Localize.currentLanguage())
    }
}
