//
//  Preferences.swift
//  forShip
//
//  Created by Mamoru Sugita on 2023/10/16.
//

import Foundation
import SwiftUI

class Preferences{
    @AppStorage(PreferencesKey.TypeInt.LastLoginDate.rawValue) private static var lastLoginDate: Date?
    static var lastLoginDate_Int64 : Int64?{
        get{
            if let date = lastLoginDate{
                DateTextLib.Date2UnixTime(date: date)
            }else{
                nil
            }
        }
        set{
            lastLoginDate = DateTextLib.UnixTime2Date(newValue ?? 0)
        }
    }
    
    @AppStorage(wrappedValue:"", PreferencesKey.TypeString.ShipId.rawValue) static var ShipId: String
    @AppStorage(wrappedValue:"", PreferencesKey.TypeString.ShipPassword.rawValue) static var ShipPassword: String
    @AppStorage(wrappedValue:"", PreferencesKey.TypeString.UserName.rawValue) static var UserName: String

    @AppStorage(wrappedValue:0, PreferencesKey.TypeInt.LocationType.rawValue) static var LocationType: Int
    //    func getPreferences(key)

    @AppStorage(PreferencesKey.TypeURL.ECDIS.rawValue) static var ECDISUrl: URL?
}


//Date型をAppStorageに保存できるように拡張
extension Date: RawRepresentable {
    public var rawValue: String {
        self.timeIntervalSinceReferenceDate.description
    }
    
    public init?(rawValue: String) {
        self = Date(timeIntervalSinceReferenceDate: Double(rawValue) ?? 0.0)
    }
}
