//
//  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(wrappedValue:"", PreferencesKey.TypeString.Id.rawValue) static var Id: String
    @AppStorage(wrappedValue:"", PreferencesKey.TypeString.Password.rawValue) static var Password: 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.TypeURL.ECDISBasic.rawValue) static var ECDISBasicUrl: URL?
    @AppStorage(PreferencesKey.TypeURL.ECDISFunction.rawValue) static var ECDISFunctionUrl: URL?
    @AppStorage(PreferencesKey.TypeURL.RADARBasic.rawValue) static var RADARBasicUrl: URL?
    @AppStorage(PreferencesKey.TypeURL.RADARFunction.rawValue) static var RADARFunctionUrl: URL?
    @AppStorage(PreferencesKey.TypeURL.AIS.rawValue) static var AISUrl: URL?
    @AppStorage(PreferencesKey.TypeURL.InmalsatC.rawValue) static var InmalsatCUrl: URL?
    @AppStorage(PreferencesKey.TypeURL.InternationalVHF.rawValue) static var InternationalVHFUrl: URL?
    @AppStorage(PreferencesKey.TypeURL.VDR.rawValue) static var VDRURL: URL?
}


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