//
//  TextFormattingView.swift
//  SIIMpaPrinterSample
//

import SwiftUI

struct TextFormattingView: View {
    @ObservedObject var printerViewModel: PrinterViewModel
    var body: some View {
        VStack {
            List {
                VStack(alignment: .leading) {
                    Text("Text")
                        .bold()
                    TextField("text", text: printerViewModel.$textStr)
                        .frame(maxWidth: .infinity)
                }
                
                VStack(alignment: .leading) {
                    Text("Font")
                        .bold()
                    NavigationLink(printerViewModel.textFont, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$textFont)
                        .onAppear() {
                            printerViewModel.setValueTable(title: TEXT_FONT_SECTION, valueTable: TEXT_FONT_TABLE)
                        })
                }
                
                VStack(alignment: .leading) {
                    Text("Scale")
                        .bold()
                    NavigationLink(printerViewModel.textScale, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$textScale)
                        .onAppear() {
                            printerViewModel.setValueTable(title: TEXT_SCALE_SECTION, valueTable: TEXT_SCALE_TABLE)
                        })
                }
                
                VStack(alignment: .leading) {
                    Text("Underline")
                        .bold()
                    NavigationLink(printerViewModel.textUnderline, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$textUnderline)
                        .onAppear() {
                            printerViewModel.setValueTable(title: TEXT_UNDERLINE_SECTION, valueTable: TEXT_UNDERLINE_TABLE)
                        })
                }
                
                VStack(alignment: .leading) {
                    Text("Bold")
                        .bold()
                    NavigationLink(printerViewModel.textBold, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$textBold)
                        .onAppear() {
                            printerViewModel.setValueTable(title: TEXT_BOLD_SECTION, valueTable: TEXT_BOLD_TABLE)
                        })
                }
                
                VStack(alignment: .leading) {
                    Text("Reverse")
                        .bold()
                    NavigationLink(printerViewModel.textReverse, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$textReverse)
                        .onAppear() {
                            printerViewModel.setValueTable(title: TEXT_REVERSE_SECTION, valueTable: TEXT_REVERSE_TABLE)
                        })
                }
                
                VStack(alignment: .leading) {
                    Text("Rotation")
                        .bold()
                    NavigationLink(printerViewModel.textRotation, destination: SetValueView(printerViewModel: printerViewModel, value: printerViewModel.$textRotation)
                        .onAppear() {
                            printerViewModel.setValueTable(title: TEXT_ROTATION_SECTION, valueTable: TEXT_ROTATION_TABLE)
                        })
                }
            }
            .listStyle(.plain)
            Spacer()
        }
        .navigationTitle("Text formatting")
        .navigationBarItems(trailing:
                                HStack {
            Button("Print") {
                PrinterManager.getDispatchQueue().async {
                    printerViewModel.showIndicator(true)
                    
                    let font = ValueManager.convertFontStringToEnum(font: printerViewModel.textFont)
                    let scale = ValueManager.convertScaleStringToEnum(scale: printerViewModel.textScale)
                    let underline = ValueManager.convertUnderlineStringToEnum(underline: printerViewModel.textUnderline)
                    let bold = ValueManager.convertBoldStringToEnum(bold: printerViewModel.textBold)
                    let reverse = ValueManager.convertReverseStringToEnum(reverse: printerViewModel.textReverse)
                    let rotation = ValueManager.convertRotationStringToEnum(rotation: printerViewModel.textRotation)
                    
                    _ = PrinterManager.printText(text: printerViewModel.textStr, font: font, scale: scale, underline: underline, bold: bold, reverse: reverse, rotate: rotation)
                    
                    printerViewModel.setAlert(PrinterManager.getResultString())
                    printerViewModel.showIndicator(false)
                }
            }
        })
    }
}
