//
//  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: QrLogin = try decoder.decode(QrLogin.self, from: jsonData)

            Preferences.ShipId = resjson.id
            Preferences.ShipPassword = resjson.pass

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

        isShowing = false
    }
}
