//
//  StatusViewModel.swift
//  SIIMpaPrinterSample
//

import Foundation
import SIIMpaPrinterManager
import SwiftUI

class StatusViewModel: NSObject, ObservableObject {
    var online = StatusElement(CALLBACK_ONLINE_SECTION)
    var voltage = StatusElement(CALLBACK_VOLTAGE_ERROR)
    var hardware = StatusElement(CALLBACK_HARDWARE_ERROR)
    var headTemperature = StatusElement(CALLBACK_HEAD_TEMPERATURE_ERROR)
    var outOfPaper = StatusElement(CALLBACK_OUT_OF_PAPER_ERROR)
    var paperJam = StatusElement(CALLBACK_PAPER_JAM_ERROR)
    var coverOpen = StatusElement(CALLBACK_COVER_OPEN_ERROR)
    var batteryError = StatusElement(CALLBACK_BATTERY_ERROR)
    var feedSwitch = StatusElement(CALLBACK_FEED_SWTICH_STATE)
    var paperFeed = StatusElement(CALLBACK_PAPER_FEED_STATE)
    var returnWaiting = StatusElement(CALLBACK_RETURN_WAITING_STATE)
    var flashMemory = StatusElement(CALLBACK_FLASH_MEMORY_REWRITING)
    var batteryState = StatusElement(CALLBACK_BATTERY_STATE)
    
    @Published var statusTable: [StatusElement] = []
    
    override init() {
        self.statusTable = [
            voltage,
            hardware,
            headTemperature,
            outOfPaper,
            paperJam,
            coverOpen,
            batteryError,
            feedSwitch,
            paperFeed,
            returnWaiting,
            flashMemory,
            batteryState
        ]
    }
    
    func reloadStatus() {
        DispatchQueue.main.async {
            self.statusTable = [
                self.voltage,
                self.hardware,
                self.headTemperature,
                self.outOfPaper,
                self.paperJam,
                self.coverOpen,
                self.batteryError,
                self.feedSwitch,
                self.paperFeed,
                self.returnWaiting,
                self.flashMemory,
                self.batteryState
            ]
        }
    }
    
    @objc func statusChanged(status: SIIMpaPrinterStatus) {
        if status.errOffline {
            self.online.setStatus(.offline)
            self.voltage.setStatus(.noStatus)
            self.hardware.setStatus(.noStatus)
            self.headTemperature.setStatus(.noStatus)
            self.outOfPaper.setStatus(.noStatus)
            self.paperJam.setStatus(.noStatus)
            self.coverOpen.setStatus(.noStatus)
            self.batteryError.setStatus(.noStatus)
            self.feedSwitch.setStatus(.noStatus)
            self.paperFeed.setStatus(.noStatus)
            self.returnWaiting.setStatus(.noStatus)
            self.flashMemory.setStatus(.noStatus)
            self.batteryState.setStatus(.noStatus)
        } else {
            self.online.setStatus(.online)
            
            if status.errVoltage {
                self.voltage.setStatus(.err)
            } else {
                self.voltage.setStatus(.no)
            }
            
            if status.errHardware {
                self.hardware.setStatus(.err)
            } else {
                self.hardware.setStatus(.no)
            }
            
            if status.errHeadTemperature {
                self.headTemperature.setStatus(.err)
            } else {
                self.headTemperature.setStatus(.no)
            }
            
            if status.errOutOfPaper {
                self.outOfPaper.setStatus(.err)
            } else {
                self.outOfPaper.setStatus(.no)
            }
            
            if status.errMarkPaperJam {
                self.paperJam.setStatus(.err)
            } else {
                self.paperJam.setStatus(.no)
            }
            
            if status.errCoverOpen {
                self.coverOpen.setStatus(.err)
            } else {
                self.coverOpen.setStatus(.no)
            }
            
            if status.errBattery {
                self.batteryError.setStatus(.err)
            } else {
                self.batteryError.setStatus(.no)
            }
            
            if status.stateFeedSwitch {
                self.feedSwitch.setStatus(.on)
            } else {
                self.feedSwitch.setStatus(.off)
            }
            
            if status.statePaperFeed {
                self.paperFeed.setStatus(.feeding)
            } else {
                self.paperFeed.setStatus(.stop)
            }
            
            if status.stateReturnWaiting {
                self.returnWaiting.setStatus(.err)
            } else {
                self.returnWaiting.setStatus(.no)
            }
            
            if status.stateFlashMemoryRewriting {
                self.flashMemory.setStatus(.writing)
            } else {
                self.flashMemory.setStatus(.no)
            }
            
            if status.stateBattery == 0 {
                self.batteryState.setStatus(.battery_none)
            } else if status.stateBattery == 1 {
                self.batteryState.setStatus(.battery_lev4)
            } else if status.stateBattery == 2 {
                self.batteryState.setStatus(.battery_lev3)
            } else if status.stateBattery == 3 {
                self.batteryState.setStatus(.battery_lev2)
            } else if status.stateBattery == 4 {
                self.batteryState.setStatus(.battery_lev1)
            } else {
                self.batteryState.setStatus(.err)
            }
        }
        self.reloadStatus()
    }
    
}
