//
//  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.Mmsi.rawValue) static var Mmsi: String
    @AppStorage(wrappedValue:"", PreferencesKey.TypeString.ShipName.rawValue) static var ShipName: String
    @AppStorage(wrappedValue:"", PreferencesKey.TypeString.GroupName.rawValue) static var GroupName: String
    @AppStorage(wrappedValue:"", PreferencesKey.TypeString.UserName.rawValue) static var UserName: String
//    func getPreferences(key)
}


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