//
//  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.Id = resjson.id
            Preferences.Password = resjson.pass

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

        isShowing = false
    }
}
