//
//  BarcodeView.swift
//  SIIMpaPrinterSample
//

import SwiftUI

struct BarcodeView: View {
    @ObservedObject var printerViewModel: PrinterViewModel
    @FocusState var isKeyboardActive: Bool
    var body: some View {
        VStack {
            List {
                VStack(alignment: .leading) {
                    Text(BARCODE_TYPE_SECTION)
                        .bold()
                    NavigationLink(printerViewModel.barcodeType, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$barcodeType)
                        .onAppear() {
                            printerViewModel.setValueTable(title: BARCODE_TYPE_SECTION, valueTable: BARCODE_TYPE_TABLE)
                        })
                }
                .onChange(of: printerViewModel.barcodeType) {barcodeType in
                    printerViewModel.setBarcodeDefaultValue()
                }
                
                VStack(alignment: .leading) {
                    Text(BARCODE_DATA_SECTION)
                        .bold()
                    TextField(BARCODE_DATA_SECTION, text: printerViewModel.$barcodeData)
                        .frame(maxWidth: .infinity)
                        .focused($isKeyboardActive)
                }
                VStack(alignment: .leading) {
                    Text(BARCODE_MODULE_WIDTH_SECTION)
                        .bold()
                    NavigationLink(printerViewModel.barcodeModuleWidth, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$barcodeModuleWidth)
                        .onAppear() {
                            printerViewModel.setValueTable(title: BARCODE_MODULE_WIDTH_SECTION, valueTable: BARCODE_MODULE_WIDTH_TABLE)
                        })
                }
                
                VStack(alignment: .leading) {
                    Text(BARCODE_HEIGHT_SECTION)
                        .bold()
                    TextField(BARCODE_HEIGHT_SECTION, text: printerViewModel.$barcodeHeight)
                        .frame(maxWidth: .infinity)
                        .keyboardType(.numberPad)
                        .focused($isKeyboardActive)
                }
                
                VStack(alignment: .leading) {
                    Text(BARCODE_HRI_SETTING_SECTION)
                        .bold()
                    NavigationLink(printerViewModel.barcodeHri, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$barcodeHri)
                        .onAppear() {
                            printerViewModel.setValueTable(title: BARCODE_HRI_SETTING_SECTION, valueTable: BARCODE_HRI_SETTING_TABLE)
                        })
                }
                
                VStack(alignment: .leading) {
                    Text(BARCODE_NW_RATIO_SECTION)
                        .bold()
                    NavigationLink(printerViewModel.barcodeNwRatio, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$barcodeNwRatio)
                        .onAppear() {
                            printerViewModel.setValueTable(title: BARCODE_NW_RATIO_SECTION, valueTable: BARCODE_NW_RATIO_TABLE)
                        })
                }
            }
            .listStyle(.plain)
            .toolbar {
                ToolbarItemGroup(placement: .keyboard, content: {
                    Spacer()
                    Button("close") {
                        isKeyboardActive = false
                        printerViewModel.checkValueValid()
                    }
                })
            }
            Spacer()
        }
        .navigationTitle("Barcode")
        .navigationBarItems(trailing:
                                HStack {
            Button("Print") {
                PrinterManager.getDispatchQueue().async {
                    printerViewModel.showIndicator(true)
                    
                    let type = ValueManager.convertBarcodeTypeStringToEnum(type: printerViewModel.barcodeType)
                    let width = ValueManager.convertBarcodeModuleWidthStringToEnum(width: printerViewModel.barcodeModuleWidth)
                    let height = Int(printerViewModel.barcodeHeight)!
                    let hri = ValueManager.convertBarcodeHriSettingStringToEnum(hri: printerViewModel.barcodeHri)
                    let nwRatio = ValueManager.convertBarcodeNwRatioStringToEnum(nwRatio: printerViewModel.barcodeNwRatio)
                    
                    _ = PrinterManager.printBarcode(type: type, text: printerViewModel.barcodeData, width: width, height: height, hri: hri, nwRatio: nwRatio)
                    
                    printerViewModel.setAlert(PrinterManager.getResultString())
                    printerViewModel.showIndicator(false)
                }
            }
        })
    }
}
