//
//  TwoDCodeView.swift
//  SIIMpaPrinterSample
//

import SwiftUI

struct TwoDCodeView: View {
    @ObservedObject var printerViewModel: PrinterViewModel
    @FocusState var isKeyboardActive: Bool
    var body: some View {
        VStack {
            List {
                VStack(alignment: .leading) {
                    Text(TWO_DIMENSION_BARCODE_TYPE_SECTION)
                        .bold()
                    NavigationLink(printerViewModel.twoDCodeType, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$twoDCodeType)
                        .onAppear() {
                            printerViewModel.setValueTable(title: TWO_DIMENSION_BARCODE_TYPE_SECTION, valueTable: TWO_DIMENSION_BARCODE_TYPE_TABLE)
                        })
                }
                .onChange(of: printerViewModel.twoDCodeType) {type in
                    printerViewModel.set2DCodeDefaultValue()
                }
                
                VStack(alignment: .leading) {
                    Text(TWO_DIMENSION_BARCODE_DATA_SECTION)
                        .bold()
                    TextField(TWO_DIMENSION_BARCODE_DATA_SECTION, text: printerViewModel.$twoDCodeData)
                        .frame(maxWidth: .infinity)
                        .focused($isKeyboardActive)
                }
                
                if printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_TYPE_QR_CODE || printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_TYPE_PDF417 || printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_DATA_MATRIX || printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_MAXI_CODE || printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_MODE_SECTION {
                    VStack(alignment: .leading) {
                        Text(TWO_DIMENSION_BARCODE_MODE_SECTION)
                            .bold()
                        NavigationLink(printerViewModel.twoDCodeMode, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$twoDCodeMode)
                            .onAppear() {
                                var valueTable: [String] = []
                                if printerViewModel.twoDCodeType  == TWO_DIMENSION_BARCODE_TYPE_QR_CODE {
                                    valueTable = TWO_DIMENSION_BARCODE_QR_CODE_MODEL_TABLE
                                    
                                } else if printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_TYPE_PDF417 {
                                    valueTable = TWO_DIMENSION_BARCODE_PDF417_MODE_TABLE
                                } else if printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_DATA_MATRIX {
                                    valueTable = TWO_DIMENSION_BARCODE_DATA_MATRIX_MODE_TABLE
                                } else if printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_MAXI_CODE {
                                    valueTable = TWO_DIMENSION_BARCODE_MAXI_CODE_MODE_TABLE
                                }
                                else if printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_MODE_SECTION {
                                    valueTable = []
                                }
                                printerViewModel.setValueTable(title: TWO_DIMENSION_BARCODE_MODE_SECTION, valueTable: valueTable)
                            })
                    }
                }
                
                if printerViewModel.twoDCodeType != TWO_DIMENSION_BARCODE_MAXI_CODE {
                    VStack(alignment: .leading) {
                        Text(TWO_DIMENSION_BARCODE_MODULE_SIZE_SECTION)
                            .bold()
                        NavigationLink(printerViewModel.twoDCodeModuleSize, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$twoDCodeModuleSize)
                            .onAppear() {
                                var valueTable: [String] = []
                                if printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_TYPE_PDF417 {
                                    valueTable = TWO_DIMENSION_BARCODE_PDF417_MODULE_SIZE_TABLE
                                } else {
                                    valueTable = TWO_DIMENSION_BARCODE_MODULE_SIZE_TABLE
                                }
                                printerViewModel.setValueTable(title: TWO_DIMENSION_BARCODE_MODULE_SIZE_SECTION, valueTable: valueTable)
                            })
                    }
                }
                
                if printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_TYPE_PDF417  || printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_GS1_OMNI_DIRECTIONAL {
                    VStack(alignment: .leading) {
                        Text(TWO_DIMENSION_BARCODE_MODULE_HEIGHT_SECTION)
                            .bold()
                        TextField(TWO_DIMENSION_BARCODE_MODULE_HEIGHT_SECTION, text: printerViewModel.$twoDCodeModuleHeight)
                            .frame(maxWidth: .infinity)
                            .keyboardType(.numberPad)
                            .focused($isKeyboardActive)
                    }
                }
                
                if printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_TYPE_PDF417  || printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_GS1_EXPANDED_STACKED {
                    VStack(alignment: .leading) {
                        Text(TWO_DIMENSION_BARCODE_COLUMN_SECTION)
                            .bold()
                        TextField(TWO_DIMENSION_BARCODE_COLUMN_SECTION, text: printerViewModel.$twoDCodeColumn)
                            .frame(maxWidth: .infinity)
                            .keyboardType(.numberPad)
                            .focused($isKeyboardActive)
                    }
                }
                
                if printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_TYPE_PDF417 {
                    VStack(alignment: .leading) {
                        Text(TWO_DIMENSION_BARCODE_ROW_SECTION)
                            .bold()
                        TextField(TWO_DIMENSION_BARCODE_ROW_SECTION, text: printerViewModel.$twoDCodeRow)
                            .frame(maxWidth: .infinity)
                            .keyboardType(.numberPad)
                            .focused($isKeyboardActive)
                    }
                }
                
                if printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_TYPE_PDF417 || printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_TYPE_QR_CODE {
                    VStack(alignment: .leading) {
                        Text(TWO_DIMENSION_BARCODE_ERROR_CORRECT_SECTION)
                            .bold()
                        NavigationLink(printerViewModel.twoDCodeErrorCorrect, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$twoDCodeErrorCorrect)
                            .onAppear() {
                                var valueTable: [String] = []
                                if printerViewModel.twoDCodeType  == TWO_DIMENSION_BARCODE_TYPE_QR_CODE {
                                    valueTable = TWO_DIMENSION_BARCODE_QR_ERROR_CORRECT_TABLE
                                } else if printerViewModel.twoDCodeType == TWO_DIMENSION_BARCODE_TYPE_PDF417 {
                                    valueTable = TWO_DIMENSION_BARCODE_PDF417_ERROR_CORRECT_TABLE
                                }
                                printerViewModel.setValueTable(title: TWO_DIMENSION_BARCODE_ERROR_CORRECT_SECTION, valueTable: valueTable)
                            })
                    }
                }
            }
            .listStyle(.plain)
            .toolbar {
                ToolbarItemGroup(placement: .keyboard, content: {
                    Spacer()
                    Button("close") {
                        isKeyboardActive = false
                        printerViewModel.checkValueValid()
                    }
                })
            }
            
            Spacer()
        }
        .navigationTitle("2DCode")
        .navigationBarItems(trailing:
                                HStack {
            Button("Print") {
                PrinterManager.getDispatchQueue().async {
                    printerViewModel.showIndicator(true)
                    
                    let type = ValueManager.convert2DBarcodeTypeStringToEnum(type: printerViewModel.twoDCodeType)
                    let text = printerViewModel.twoDCodeData
                    let mode = ValueManager.convert2DBarcodeModeStringToEnum(mode: printerViewModel.twoDCodeMode)
                    let moduleSize = ValueManager.convert2DBarcodeModuleSizeStringToEnum(moduleSize: printerViewModel.twoDCodeModuleSize)
                    let moduleHeight = Int(printerViewModel.twoDCodeModuleHeight) ?? 0
                    let column = Int(printerViewModel.twoDCodeColumn) ?? 0
                    let row = Int(printerViewModel.twoDCodeRow) ?? 0
                    let errorCorrect = ValueManager.convert2DBarcodeErrorCorrectStringToEnum(errorCorrect: printerViewModel.twoDCodeErrorCorrect)
                    
                    _ = PrinterManager.print2DCode(type: type, text: text, mode: mode, moduleSize: moduleSize, moduleHeight: moduleHeight, column: column, row: row, errorCorrect: errorCorrect)
                    
                    printerViewModel.setAlert(PrinterManager.getResultString())
                    printerViewModel.showIndicator(false)
                }
            }
        })
    }
}
