//
//  ScannerViewModel.swift
//  forShip
//
//  Created by Mamoru Sugita on 2023/10/13.
//

import Foundation
import SwiftUI

class ScannerViewModel: ObservableObject {
    let scanInterval: Double = 1.0  /// QRコードを読み取る時間間隔
    @Published var lastQrCode: String = "QRコード"
    @Published var isShowing: Bool = false

    /// QRコード読み取り時に実行される。
    func onFoundQrCode(_ code: String) {
        do {
            self.lastQrCode = code
            let decoder = JSONDecoder()
            let jsonData = self.lastQrCode.data(using: .utf8)!
            let resjson: ReqLogin = try decoder.decode(ReqLogin.self, from: jsonData)

            Preferences.ShipId = resjson.Id
            Preferences.ShipPassword = resjson.Password
            Preferences.lastLoginDate_Int64 = DateTextLib.Date2UnixTime(date: Date())
        } catch {
            print(debug: "decodeエラー")
            Preferences.ShipId = ""
            Preferences.ShipPassword = ""
        }

        isShowing = false
    }
}
