//
//  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(PreferencesKey.TypeInt.TermsAgreeDate.rawValue) static var termsAgreeDate: Date?
    static var termsAgreeDate_Int64 : Int64?{
        get{
            if let date = termsAgreeDate{
                DateTextLib.Date2UnixTime(date: date)
            }else{
                nil
            }
        }
        set{
            termsAgreeDate = DateTextLib.UnixTime2Date(newValue ?? 0)
        }
    }
    @AppStorage(PreferencesKey.TypeInt.PrivacyPolicyAgreeDate.rawValue) static var privacyPolicyAgreeDate: Date?
    static var privacyPolicyAgreeDate_Int64 : Int64?{
        get{
            if let date = privacyPolicyAgreeDate{
                DateTextLib.Date2UnixTime(date: date)
            }else{
                nil
            }
        }
        set{
            privacyPolicyAgreeDate = DateTextLib.UnixTime2Date(newValue ?? 0)
        }
    }
    
    @AppStorage(wrappedValue:"", PreferencesKey.TypeString.Id.rawValue) static var Id: String
    @AppStorage(wrappedValue:"", PreferencesKey.TypeString.Password.rawValue) static var Password: String
    @AppStorage(wrappedValue:"", PreferencesKey.TypeString.DeviceId.rawValue) static var DeviceId: String
    @AppStorage(wrappedValue:"", PreferencesKey.TypeString.UserName.rawValue) static var UserName: String

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

    @AppStorage(PreferencesKey.TypeString.ECDISBasic.rawValue) static var ECDISBasicManualVer: String?
    @AppStorage(PreferencesKey.TypeString.ECDISFunction.rawValue) static var ECDISFunctionManualVer: String?
    @AppStorage(PreferencesKey.TypeString.RADARBasic.rawValue) static var RADARBasicManualVer: String?
    @AppStorage(PreferencesKey.TypeString.RADARFunction.rawValue) static var RADARFunctionManualVer: String?
    @AppStorage(PreferencesKey.TypeString.AIS.rawValue) static var AISManualVer: String?
    @AppStorage(PreferencesKey.TypeString.InmalsatC.rawValue) static var InmalsatCManualVer: String?
    @AppStorage(PreferencesKey.TypeString.InternationalVHF.rawValue) static var InternationalVHFManualVer: String?
    @AppStorage(PreferencesKey.TypeString.VDR.rawValue) static var VDRManualVer: String?
}


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