//
//  SecondView.swift
//  SIIMpaPrinterSample
//

import SwiftUI

struct SecondView: View {
    @ObservedObject var printerViewModel: PrinterViewModel
    @FocusState var isKeyboardActive: Bool
    @State var viewDidLoad: Bool = false
    var body: some View {
        VStack {
            NavigationStack {
                List {
                    VStack(alignment: .leading) {
                        Text(SETTINGS_GET_VERSIOM_SECTION)
                            .bold()
                        Text(PrinterManager.getVersion())
                    }
                    
                    VStack(alignment: .leading) {
                        Text(SETTINGS_SOCKET_KEEP_TIME_SECTION)
                            .bold()
                        TextField(SETTINGS_SOCKET_KEEP_TIME_SECTION, text: printerViewModel.$socketKeepingTime)
                            .frame(maxWidth: .infinity)
                            .keyboardType(.asciiCapableNumberPad)
                            .focused($isKeyboardActive)
                    }
                    
                    VStack(alignment: .leading) {
                        Text(SETTINGS_WRITE_TIMEOUT_SECTION)
                            .bold()
                        TextField(SETTINGS_WRITE_TIMEOUT_SECTION, text: printerViewModel.$writeTimeout)
                            .frame(maxWidth: .infinity)
                            .keyboardType(.asciiCapableNumberPad)
                            .focused($isKeyboardActive)
                    }
                    
                    VStack(alignment: .leading) {
                        Text(SETTINGS_RESPONSE_TIMEOUT_SECTION)
                            .bold()
                        TextField(SETTINGS_RESPONSE_TIMEOUT_SECTION, text: printerViewModel.$responseTimeout)
                            .frame(maxWidth: .infinity)
                            .keyboardType(.asciiCapableNumberPad)
                            .focused($isKeyboardActive)
                    }
                    
                    VStack(alignment: .leading) {
                        Text(SETTINGS_CODEPAGE_SECTION)
                            .bold()
                        NavigationLink(printerViewModel.codePage, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$codePage)
                            .onAppear() {
                                printerViewModel.setValueTable(title: SETTINGS_CODEPAGE_SECTION, valueTable: CODEPAGE_TABLE)
                            }
                            .onDisappear() {
                                // register selected value to printer
                                PrinterManager.setCodePage(value: printerViewModel.codePage)
                            }
                        )
                    }
                    
                    VStack(alignment: .leading) {
                        Text(SETTINGS_INTERNATIONAL_CHARACTER_SECTION)
                            .bold()
                        NavigationLink(printerViewModel.internationalCharacter, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$internationalCharacter)
                            .onAppear() {
                                printerViewModel.setValueTable(title: SETTINGS_INTERNATIONAL_CHARACTER_SECTION, valueTable: INTERNATIONAL_CHARACTER_TABLE)
                            }
                            .onDisappear(){
                                // register selected value to printer
                                PrinterManager.setInternationalCharacter(value: printerViewModel.internationalCharacter)
                            })
                    }
                    
                    VStack(alignment: .leading) {
                        Text(SETTINGS_LOG_FILE_SIZE_SECTION)
                            .bold()
                        NavigationLink(printerViewModel.logFileSize, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$logFileSize)
                            .onAppear() {
                                printerViewModel.setValueTable(title: SETTINGS_LOG_FILE_SIZE_SECTION, valueTable: LOG_FILE_SIZE_TABLE)
                            }
                            .onDisappear(){
                                // register selected value to printer
                                PrinterManager.setLogFileSize(value: printerViewModel.logFileSize)
                            })
                    }
                }
                .listStyle(.plain)
                .toolbar {
                    ToolbarItemGroup(placement: .keyboard, content: {
                        Spacer()
                        Button("close") {
                            isKeyboardActive = false
                        }
                    })
                }
                .onChange(of: isKeyboardActive, perform: {isActive in
                    if !isActive {
                        printerViewModel.checkValueValid()
                    }
                })
                .navigationTitle("Settings")
            }
            .navigationViewStyle(.stack)
        }
        // When change to SecondView(Settings view), get settings values from printer
        .onAppear() {
            PrinterManager.getDispatchQueue().async {
                let writeTimeout = PrinterManager.getWriteTimeout()
                let responseTimeout = PrinterManager.getResponseTimeout()
                let codePage = PrinterManager.getCodePage()
                let internationalCharacter = PrinterManager.getInternationalCharacter()
                printerViewModel.setSettingValues(writeTimeout: writeTimeout, responseTimeout: responseTimeout, codePage: codePage, internationalCharacter: internationalCharacter)
            }
        }
        // When changed to other view(FirstView), settings will be registered to printer
        .onDisappear() {
            PrinterManager.getDispatchQueue().async {
                PrinterManager.setWriteTimeout(value: printerViewModel.writeTimeout)
                PrinterManager.setResponseTimeout(value: printerViewModel.responseTimeout)
            }
        }
    }
}

