//
//  SiiPrinterModel.swift
//  SIIMpaPrinterSample
//

import Foundation
import SwiftUI

class PrinterViewModel : ObservableObject {
    @AppStorage(KEY_TYPE_ADDRESS) var address = VALUE_TYPE_ADDRESS
    @AppStorage(KEY_TYPE_PORTTYPE) var portType = VALUE_TYPE_PORTTYPE
    @AppStorage(KEY_TYPE_PRINTERMODEL) var modelName = VALUE_TYPE_PRINTERMODEL
    
    @AppStorage(KEY_TYPE_SOCKET_KEEP_TIME) var socketKeepingTime = "300"
    @AppStorage(KEY_TYPE_WRITE_TIMEOUT) var writeTimeout = "1000"
    @AppStorage(KEY_TYPE_RESPONSE_TIMEOUT) var responseTimeout = "1000"
    @AppStorage(KEY_TYPE_CODEPAGE) var codePage = SETTINGS_CODEPAGE_SECTION
    @AppStorage(KEY_TYPE_INTERNATIONAL_CHARACTER) var internationalCharacter = SETTINGS_INTERNATIONAL_CHARACTER_SECTION
    @AppStorage(KEY_TYPE_LOG_FILE_SIZE) var logFileSize = SETTINGS_LOG_FILE_SIZE_50M
    
    @AppStorage(KEY_TYPE_TEXT) var textStr = "ABC0123456789"
    @AppStorage(KEY_TYPE_FONT) var textFont = TEXT_FONT_A
    @AppStorage(KEY_TYPE_SCALE) var textScale = TEXT_SCALE_1
    @AppStorage(KEY_TYPE_UNDERLINE) var textUnderline = TEXT_UNDERLINE_NONE
    @AppStorage(KEY_TYPE_BOLD) var textBold = TEXT_BOLD_OFF
    @AppStorage(KEY_TYPE_REVERSE) var textReverse = TEXT_REVERSE_OFF
    @AppStorage(KEY_TYPE_ROTATION) var textRotation = TEXT_ROTATION_NONE
    
    @AppStorage(KEY_TYPE_BARCODE_TYPE) var barcodeType = BARCODE_TYPE_UPC_A
    @AppStorage(KEY_TYPE_BARCODE_DATA) var barcodeData = "012345678901"
    @AppStorage(KEY_TYPE_BARCODE_MODULE_WIDTH) var barcodeModuleWidth = BARCODE_MODULE_WIDTH_3
    @AppStorage(KEY_TYPE_BARCODE_HEIGHT) var barcodeHeight = "162"
    @AppStorage(KEY_TYPE_BARCODE_HRI_SETTING) var barcodeHri = BARCODE_HRI_SETTING_NONE
    @AppStorage(KEY_TYPE_BARCODE_NW_RATIO) var barcodeNwRatio = BARCODE_NW_RATIO_1TO2_5
    
    @AppStorage(KEY_TYPE_2D_BARCODE_TYPE) var twoDCodeType = TWO_DIMENSION_BARCODE_TYPE_QR_CODE
    @AppStorage(KEY_TYPE_2D_BARCODE_MODE) var twoDCodeMode = TWO_DIMENSION_BARCODE_QR_CODE_MODEL_2
    @AppStorage(KEY_TYPE_2D_BARCODE_MODULE_SIZE) var twoDCodeModuleSize = TWO_DIMENSION_BARCODE_MODULE_SIZE_DEFAULT
    @AppStorage(KEY_TYPE_2D_BARCODE_ERROR_CORRECT) var twoDCodeErrorCorrect = TWO_DIMENSION_BARCODE_QR_ERROR_CORRECT_L
    @AppStorage(KEY_TYPE_2D_BARCODE_DATA) var twoDCodeData = "ABC0123456789"
    @AppStorage(KEY_TYPE_2D_BARCODE_MODULE_HEIGHT) var twoDCodeModuleHeight = "0"
    @AppStorage(KEY_TYPE_2D_BARCODE_COLUMN) var twoDCodeColumn = "0"
    @AppStorage(KEY_TYPE_2D_BARCODE_ROW) var twoDCodeRow = "0"
    
    @Published var printer: SiiPrinter = SiiPrinter()
    @Published var alertStr: String = ""
    @Published var isShowAlert: Bool = false
    @Published var isShowIndicator: Bool = false
    @Published var isShowPrinterListViewIndicator: Bool = false
    
    @Published var valueTable: ValueTable = ValueTable(title: "", valueTable: [])
    
    init() {
        self.printer = SiiPrinter(address: self.address, modelName: self.modelName, portType: self.portType)
    }
    
    func savePrinterInfo(printer: SiiPrinter) {
        self.address = printer.address
        self.portType = printer.portType
        self.modelName = printer.modelName
    }
    
    func setPrinter(printer: SiiPrinter) {
        self.printer = printer
    }
    
    func getPrinter() -> SiiPrinter {
        return self.printer
    }
    
    func setAlert(_ str: String) {
        DispatchQueue.main.async {
            self.alertStr = str
            self.isShowAlert = (str != "")
        }
    }
    
    func showIndicator(_ show: Bool) {
        DispatchQueue.main.async {
            self.isShowIndicator = show
        }
    }
    
    func showPrinterListViewIndicator(_ show: Bool) {
        DispatchQueue.main.async {
            self.isShowPrinterListViewIndicator = show
        }
    }
    
    func setValueTable(title: String, valueTable: [String]) {
        DispatchQueue.main.async {
            self.valueTable = ValueTable(title: title, valueTable: valueTable)
        }
    }
    
    func checkValueValid() {
        DispatchQueue.main.async {
            if Int(self.socketKeepingTime) ?? 0 < 60 {
                self.socketKeepingTime = "60"
            } else if Int(self.socketKeepingTime)! > 300 {
                self.socketKeepingTime = "300"
            }
            
            if Int(self.writeTimeout) ?? 0 < 1000 {
                self.writeTimeout = "1000"
            } else if Int(self.writeTimeout)! > 90000 {
                self.writeTimeout = "90000"
            }
            
            if Int(self.responseTimeout) ?? 0 < 1000 {
                self.responseTimeout = "1000"
            } else if Int(self.responseTimeout)! > 90000 {
                self.responseTimeout = "90000"
            }
            
            if Int(self.barcodeHeight) ?? 0 < 1 {
                self.barcodeHeight = "1"
            } else if Int(self.barcodeHeight) ?? 0 > 255 {
                self.barcodeHeight = "255"
            }
            
            if self.twoDCodeType == TWO_DIMENSION_BARCODE_TYPE_PDF417 {
                if Int(self.twoDCodeModuleHeight) ?? 0 < 2 {
                    self.twoDCodeModuleHeight = "2"
                } else if Int(self.twoDCodeModuleHeight) ?? 0 > 127 {
                    self.twoDCodeModuleHeight = "127"
                }
                
                if self.twoDCodeColumn == "" {
                    self.twoDCodeColumn = "0"
                } else if Int(self.twoDCodeColumn) ?? 0 > 30 {
                    self.twoDCodeColumn = "30"
                }
                
                if self.twoDCodeRow == "" {
                    self.twoDCodeRow = "0"
                } else if Int(self.twoDCodeRow) ?? 0 > 0 && Int(self.twoDCodeRow) ?? 0 < 3 {
                    self.twoDCodeRow = "3"
                } else if Int(self.twoDCodeRow) ?? 0 > 90 {
                    self.twoDCodeRow = "90"
                }
            } else if self.twoDCodeType == TWO_DIMENSION_BARCODE_GS1_OMNI_DIRECTIONAL {
                if Int(self.twoDCodeModuleHeight) ?? 0 < 33 {
                    self.twoDCodeModuleHeight = "33"
                } else if Int(self.twoDCodeModuleHeight) ?? 0 > 255 {
                    self.twoDCodeModuleHeight = "255"
                }
            }
        }
    }
    
    func setSettingValues(writeTimeout: String, responseTimeout: String, codePage: String, internationalCharacter: String) {
        DispatchQueue.main.async {
            self.writeTimeout = writeTimeout
            self.responseTimeout = responseTimeout
            self.codePage = codePage
            self.internationalCharacter = internationalCharacter
        }
    }
    
    func setBarcodeDefaultValue() {
        self.barcodeModuleWidth = BARCODE_MODULE_WIDTH_3
        self.barcodeHeight = "162"
        self.barcodeHri = BARCODE_HRI_SETTING_NONE
        self.barcodeNwRatio = BARCODE_NW_RATIO_1TO2_5
        switch self.barcodeType {
        case BARCODE_TYPE_UPC_A:
            self.barcodeData = "012345678901"
        case BARCODE_TYPE_UPC_E:
            self.barcodeData = "01200000123"
        case BARCODE_TYPE_JAN13:
            self.barcodeData = "012345678912"
        case BARCODE_TYPE_JAN8:
            self.barcodeData = "0123456"
        case BARCODE_TYPE_CODE39:
            self.barcodeData = "A"
        case BARCODE_TYPE_ITF:
            self.barcodeData = "01"
        case BARCODE_TYPE_CODABAR:
            self.barcodeData = "B+C"
        case BARCODE_TYPE_CODE128:
            self.barcodeData = "{A0123456789"
        case BARCODE_TYPE_CODE93:
            self.barcodeData = "0123456789"
        case BARCODE_TYPE_JAN13_ADDON2:
            self.barcodeData = "49012345678940"
        case BARCODE_TYPE_JAN13_ADDON5:
            self.barcodeData = "49012345678940123"
        case BARCODE_TYPE_GS1_OMNI_DIRECTIONAL:
            self.barcodeData = "4901234567894"
        case BARCODE_TYPE_GS1_TRUNCATED:
            self.barcodeData = "4901234567894"
        case BARCODE_TYPE_GS1_LIMITED:
            self.barcodeData = "0123456789012"
        case BARCODE_TYPE_GS1_EXPANDED:
            self.barcodeData = "490{123456789"
        default:
            break
        }
    }
    
    func set2DCodeDefaultValue() {
        self.twoDCodeMode = TWO_DIMENSION_BARCODE_MODE_NONE
        self.twoDCodeModuleSize = TWO_DIMENSION_BARCODE_MODULE_SIZE_DEFAULT
        self.twoDCodeModuleHeight = "0"
        self.twoDCodeColumn = "0"
        self.twoDCodeRow = "0"
        self.twoDCodeErrorCorrect = TWO_DIMENSION_BARCODE_ERROR_CORRECT_NONE
        switch self.twoDCodeType {
        case TWO_DIMENSION_BARCODE_TYPE_QR_CODE:
            self.twoDCodeData = "ABC0123456789"
            self.twoDCodeMode = TWO_DIMENSION_BARCODE_QR_CODE_MODEL_1
            self.twoDCodeErrorCorrect = TWO_DIMENSION_BARCODE_QR_ERROR_CORRECT_L
        case TWO_DIMENSION_BARCODE_TYPE_PDF417:
            self.twoDCodeData = "ABC0123456789"
            self.twoDCodeMode = TWO_DIMENSION_BARCODE_PDF417_MODE_STANDARD
            self.twoDCodeModuleHeight = "10"
            self.twoDCodeErrorCorrect = TWO_DIMENSION_BARCODE_PDF417_ERROR_CORRECT_0
        case TWO_DIMENSION_BARCODE_DATA_MATRIX:
            self.twoDCodeData = "ABC0123456789"
            self.twoDCodeMode = TWO_DIMENSION_BARCODE_DATA_MATRIX_MODE_AUTO
        case TWO_DIMENSION_BARCODE_MAXI_CODE:
            self.twoDCodeData = "122333444455555ABC0123456789DEF"
            self.twoDCodeMode = TWO_DIMENSION_BARCODE_MAXI_CODE_MODE_2
        case TWO_DIMENSION_BARCODE_GS1_STACKED:
            self.twoDCodeData = "1230123456789"
        case TWO_DIMENSION_BARCODE_GS1_OMNI_DIRECTIONAL:
            self.twoDCodeData = "1230123456789"
            self.twoDCodeModuleHeight = "33"
        case TWO_DIMENSION_BARCODE_GS1_EXPANDED_STACKED:
            self.twoDCodeData = "01173500538500163202000150"
            self.twoDCodeColumn = "2"
        default:
            break;
        }
    }
}
