//
//  SettingsView.swift
//  SiiLibSample
//

import SwiftUI

struct SettingsView: View {
    @Binding var isShowSettingsView: Bool

    @State var sendTimeout: String = ""
    @State var receiveTimeout: String = ""
    @State var socketKeepingTime: String = ""
    @State var codePage: String = ""
    @State var internationalCharacter: String = ""
    @State var settingItem: String = ""
    @State var settingValue: String = ""
    @State var isShowSettingAlert: Bool = false

    let SECTION_TITLE_TABLE: [String] = [
        SETTINGS_SEND_TIMEOUT_SECTION,
        SETTINGS_RECEIVE_TIMEOUT_SECTION,
        SETTINGS_SOCKET_KEEP_TIME_SECTION,
        SETTINGS_CODEPAGE_SECTION,
        SETTINGS_INTERNATIONAL_CHARACTER_SECTION,
    ]

    let CODEPAGE_TABLE: [String] = [
        SETTINGS_CODEPAGE_437,
        SETTINGS_CODEPAGE_KATAKANA,
        SETTINGS_CODEPAGE_850,
        SETTINGS_CODEPAGE_860,
        SETTINGS_CODEPAGE_863,
        SETTINGS_CODEPAGE_865,
        SETTINGS_CODEPAGE_857,
        SETTINGS_CODEPAGE_737,
        SETTINGS_CODEPAGE_1252,
        SETTINGS_CODEPAGE_866,
        SETTINGS_CODEPAGE_852,
        SETTINGS_CODEPAGE_858,
        SETTINGS_CODEPAGE_855,
        SETTINGS_CODEPAGE_864,
        SETTINGS_CODEPAGE_1250,
        SETTINGS_CODEPAGE_1251,
        SETTINGS_CODEPAGE_1253,
        SETTINGS_CODEPAGE_1254,
    ]

    let INTERNATIONAL_CHARACTER_TABLE: [String] = [
        SETTINGS_INTERNATIONAL_CHARACTER_USA,
        SETTINGS_INTERNATIONAL_CHARACTER_FRANCE,
        SETTINGS_INTERNATIONAL_CHARACTER_GERMANY,
        SETTINGS_INTERNATIONAL_CHARACTER_ENGLAND,
        SETTINGS_INTERNATIONAL_CHARACTER_DENMARK_1,
        SETTINGS_INTERNATIONAL_CHARACTER_SWEDEN,
        SETTINGS_INTERNATIONAL_CHARACTER_ITALY,
        SETTINGS_INTERNATIONAL_CHARACTER_SPAIN,
        SETTINGS_INTERNATIONAL_CHARACTER_JAPAN,
        SETTINGS_INTERNATIONAL_CHARACTER_NORWAY,
        SETTINGS_INTERNATIONAL_CHARACTER_DENMARK_2,
        SETTINGS_INTERNATIONAL_CHARACTER_SPAIN_2,
        SETTINGS_INTERNATIONAL_CHARACTER_LATIN_AMERICA,
        SETTINGS_INTERNATIONAL_CHARACTER_ARABIA,
    ]

    var body: some View {
        ZStack {
            NavigationView {
                VStack {
                    List {
                        ForEach(SECTION_TITLE_TABLE, id: \.self) { title in
                            Section(content: {
                                HStack {
                                    if title == SETTINGS_SEND_TIMEOUT_SECTION {
                                        Text(sendTimeout)
                                    } else if title == SETTINGS_RECEIVE_TIMEOUT_SECTION {
                                        Text(receiveTimeout)
                                    } else if title == SETTINGS_SOCKET_KEEP_TIME_SECTION {
                                        Text(socketKeepingTime)
                                    } else if title == SETTINGS_CODEPAGE_SECTION {
                                        Text(codePage)
                                    } else if title == SETTINGS_INTERNATIONAL_CHARACTER_SECTION {
                                        Text(internationalCharacter)
                                    }
                                    Spacer()
                                }
                                .contentShape(Rectangle())
                                .onTapGesture {
                                    settingItem = title
                                    isShowSettingAlert = true
                                }
                            },header: {
                                Text(title)
                            })
                        }
                    }
                    .listStyle(.plain)
                    .alert(
                        settingItem,
                        isPresented: $isShowSettingAlert,
                        actions: {
                            if settingItem == SETTINGS_SEND_TIMEOUT_SECTION ||
                                settingItem == SETTINGS_RECEIVE_TIMEOUT_SECTION ||
                                settingItem == SETTINGS_SOCKET_KEEP_TIME_SECTION {
                                TextField(
                                    "Input timeout value", text: $settingValue
                                )
                                .keyboardType(.numberPad)
                                Button("OK") {
                                    // Set input value to SDK and get return value for display.
                                    if settingItem == SETTINGS_SEND_TIMEOUT_SECTION {
                                        sendTimeout = String(
                                            SampleFunctions.set_sendTimeoutSample(
                                                time: Int32(settingValue) ?? 0
                                            ))
                                    } else if settingItem == SETTINGS_RECEIVE_TIMEOUT_SECTION {
                                        receiveTimeout = String(
                                            SampleFunctions.set_receiveTimeoutSample(
                                                time: Int32(settingValue) ?? 0
                                            ))
                                    } else if settingItem == SETTINGS_SOCKET_KEEP_TIME_SECTION {
                                        socketKeepingTime = String(
                                            SampleFunctions.set_socketKeepingTimeSample(
                                                time: Int32(settingValue) ?? 0
                                            ))
                                    }
                                    settingValue = ""
                                }
                                Button(role: .cancel,
                                       action: {
                                    settingValue = ""
                                }, label: {
                                    Text("Cancel")
                                })
                            } else if settingItem == SETTINGS_CODEPAGE_SECTION {
                                ForEach(CODEPAGE_TABLE, id: \.self) { item in
                                    // Set selected value to SDK and get return value for display.
                                    Button(item) {
                                        codePage = convertCodePageIntegerToString(
                                            SampleFunctions.set_codePageSample(
                                                codePage: convertCodePageStringToInteger(item)
                                            ))
                                    }
                                }
                                Button(role: .cancel,
                                       action: {},
                                       label: {
                                    Text("Cancel")
                                })
                            } else if settingItem == SETTINGS_INTERNATIONAL_CHARACTER_SECTION {
                                ForEach(INTERNATIONAL_CHARACTER_TABLE, id: \.self) { item in
                                    // Set selected value to SDK and get return value for display.
                                    Button(item) {
                                        internationalCharacter =
                                        convertInternationalCharacterIntegerToString(
                                            SampleFunctions.set_internationalCharacterSample(
                                                internationalCharacter: convertInternationalCharacterStringToInteger(item)
                                            ))
                                    }
                                }
                                Button(role: .cancel,
                                       action: {},
                                       label: {
                                    Text("Cancel")
                                })
                            }
                        })
                }
                .toolbar(content: {
                    ToolbarItem(
                        placement: .topBarLeading,
                        content: {
                            Button("close") {
                                isShowSettingsView = false
                            }
                        })
                })
                .onAppear {
                    getValue()
                }
            }
        }
    }

    func getValue() {
        sendTimeout = String(SampleFunctions.get_sentTimeoutSample())
        receiveTimeout = String(SampleFunctions.get_receiveTimeoutSample())
        socketKeepingTime = String(
            SampleFunctions.get_socketKeepingTimeSample())
        codePage = convertCodePageIntegerToString(
            SampleFunctions.get_codePageSample())
        internationalCharacter = convertInternationalCharacterIntegerToString(
            SampleFunctions.get_internationalCharacterSample())
    }
}

func convertInternationalCharacterIntegerToString(_ internationalCharacterCode: Int32) -> String {
    if internationalCharacterCode == SII_PM_COUNTRY_USA {
        return SETTINGS_INTERNATIONAL_CHARACTER_USA
    } else if internationalCharacterCode == SII_PM_COUNTRY_FRANCE {
        return SETTINGS_INTERNATIONAL_CHARACTER_FRANCE
    } else if internationalCharacterCode == SII_PM_COUNTRY_GERMANY {
        return SETTINGS_INTERNATIONAL_CHARACTER_GERMANY
    } else if internationalCharacterCode == SII_PM_COUNTRY_ENGLAND {
        return SETTINGS_INTERNATIONAL_CHARACTER_ENGLAND
    } else if internationalCharacterCode == SII_PM_COUNTRY_DENMARK_1 {
        return SETTINGS_INTERNATIONAL_CHARACTER_DENMARK_1
    } else if internationalCharacterCode == SII_PM_COUNTRY_SWEDEN {
        return SETTINGS_INTERNATIONAL_CHARACTER_SWEDEN
    } else if internationalCharacterCode == SII_PM_COUNTRY_ITALY {
        return SETTINGS_INTERNATIONAL_CHARACTER_ITALY
    } else if internationalCharacterCode == SII_PM_COUNTRY_SPAIN {
        return SETTINGS_INTERNATIONAL_CHARACTER_SPAIN
    } else if internationalCharacterCode == SII_PM_COUNTRY_JAPAN {
        return SETTINGS_INTERNATIONAL_CHARACTER_JAPAN
    } else if internationalCharacterCode == SII_PM_COUNTRY_NORWAY {
        return SETTINGS_INTERNATIONAL_CHARACTER_NORWAY
    } else if internationalCharacterCode == SII_PM_COUNTRY_DENMARK_2 {
        return SETTINGS_INTERNATIONAL_CHARACTER_DENMARK_2
    } else if internationalCharacterCode == SII_PM_COUNTRY_SPAIN_2 {
        return SETTINGS_INTERNATIONAL_CHARACTER_SPAIN_2
    } else if internationalCharacterCode == SII_PM_COUNTRY_LATIN_AMERICA {
        return SETTINGS_INTERNATIONAL_CHARACTER_LATIN_AMERICA
    } else if internationalCharacterCode == SII_PM_COUNTRY_ARABIA {
        return SETTINGS_INTERNATIONAL_CHARACTER_ARABIA
    } else {
        return "internationalCharacterCode is invalid"
    }
}

func convertInternationalCharacterStringToInteger(_ internationalCharacterString: String) -> Int32 {
    var internationalCharacterCode: Int32 = -1
    if internationalCharacterString == SETTINGS_INTERNATIONAL_CHARACTER_USA {
        internationalCharacterCode = SII_PM_COUNTRY_USA
    } else if internationalCharacterString
        == SETTINGS_INTERNATIONAL_CHARACTER_FRANCE
    {
        internationalCharacterCode = SII_PM_COUNTRY_FRANCE
    } else if internationalCharacterString
        == SETTINGS_INTERNATIONAL_CHARACTER_GERMANY
    {
        internationalCharacterCode = SII_PM_COUNTRY_GERMANY
    } else if internationalCharacterString
        == SETTINGS_INTERNATIONAL_CHARACTER_ENGLAND
    {
        internationalCharacterCode = SII_PM_COUNTRY_ENGLAND
    } else if internationalCharacterString
        == SETTINGS_INTERNATIONAL_CHARACTER_DENMARK_1
    {
        internationalCharacterCode = SII_PM_COUNTRY_DENMARK_1
    } else if internationalCharacterString
        == SETTINGS_INTERNATIONAL_CHARACTER_SWEDEN
    {
        internationalCharacterCode = SII_PM_COUNTRY_SWEDEN
    } else if internationalCharacterString
        == SETTINGS_INTERNATIONAL_CHARACTER_ITALY
    {
        internationalCharacterCode = SII_PM_COUNTRY_ITALY
    } else if internationalCharacterString
        == SETTINGS_INTERNATIONAL_CHARACTER_SPAIN
    {
        internationalCharacterCode = SII_PM_COUNTRY_SPAIN
    } else if internationalCharacterString
        == SETTINGS_INTERNATIONAL_CHARACTER_JAPAN
    {
        internationalCharacterCode = SII_PM_COUNTRY_JAPAN
    } else if internationalCharacterString
        == SETTINGS_INTERNATIONAL_CHARACTER_NORWAY
    {
        internationalCharacterCode = SII_PM_COUNTRY_NORWAY
    } else if internationalCharacterString
        == SETTINGS_INTERNATIONAL_CHARACTER_DENMARK_2
    {
        internationalCharacterCode = SII_PM_COUNTRY_DENMARK_2
    } else if internationalCharacterString
        == SETTINGS_INTERNATIONAL_CHARACTER_SPAIN_2
    {
        internationalCharacterCode = SII_PM_COUNTRY_SPAIN_2
    } else if internationalCharacterString
        == SETTINGS_INTERNATIONAL_CHARACTER_LATIN_AMERICA
    {
        internationalCharacterCode = SII_PM_COUNTRY_LATIN_AMERICA
    } else if internationalCharacterString
        == SETTINGS_INTERNATIONAL_CHARACTER_ARABIA
    {
        internationalCharacterCode = SII_PM_COUNTRY_ARABIA
    }
    return internationalCharacterCode
}

func convertCodePageIntegerToString(_ codePageCode: Int32) -> String {
    if codePageCode == SII_PM_CODE_PAGE_437 {
        return SETTINGS_CODEPAGE_437
    } else if codePageCode == SII_PM_CODE_PAGE_KATAKANA {
        return SETTINGS_CODEPAGE_KATAKANA
    } else if codePageCode == SII_PM_CODE_PAGE_850 {
        return SETTINGS_CODEPAGE_850
    } else if codePageCode == SII_PM_CODE_PAGE_860 {
        return SETTINGS_CODEPAGE_860
    } else if codePageCode == SII_PM_CODE_PAGE_863 {
        return SETTINGS_CODEPAGE_863
    } else if codePageCode == SII_PM_CODE_PAGE_865 {
        return SETTINGS_CODEPAGE_865
    } else if codePageCode == SII_PM_CODE_PAGE_857 {
        return SETTINGS_CODEPAGE_857
    } else if codePageCode == SII_PM_CODE_PAGE_737 {
        return SETTINGS_CODEPAGE_737
    } else if codePageCode == SII_PM_CODE_PAGE_1252 {
        return SETTINGS_CODEPAGE_1252
    } else if codePageCode == SII_PM_CODE_PAGE_866 {
        return SETTINGS_CODEPAGE_866
    } else if codePageCode == SII_PM_CODE_PAGE_852 {
        return SETTINGS_CODEPAGE_852
    } else if codePageCode == SII_PM_CODE_PAGE_858 {
        return SETTINGS_CODEPAGE_858
    } else if codePageCode == SII_PM_CODE_PAGE_855 {
        return SETTINGS_CODEPAGE_855
    } else if codePageCode == SII_PM_CODE_PAGE_864 {
        return SETTINGS_CODEPAGE_864
    } else if codePageCode == SII_PM_CODE_PAGE_1250 {
        return SETTINGS_CODEPAGE_1250
    } else if codePageCode == SII_PM_CODE_PAGE_1251 {
        return SETTINGS_CODEPAGE_1251
    } else if codePageCode == SII_PM_CODE_PAGE_1253 {
        return SETTINGS_CODEPAGE_1253
    } else if codePageCode == SII_PM_CODE_PAGE_1254 {
        return SETTINGS_CODEPAGE_1254
    } else {
        return "Codepage is invalid"
    }
}

func convertCodePageStringToInteger(_ codePageString: String) -> Int32 {
    var codePageCode: Int32 = -1
    if codePageString == SETTINGS_CODEPAGE_437 {
        codePageCode = SII_PM_CODE_PAGE_437
    } else if codePageString == SETTINGS_CODEPAGE_KATAKANA {
        codePageCode = SII_PM_CODE_PAGE_KATAKANA
    } else if codePageString == SETTINGS_CODEPAGE_850 {
        codePageCode = SII_PM_CODE_PAGE_850
    } else if codePageString == SETTINGS_CODEPAGE_860 {
        codePageCode = SII_PM_CODE_PAGE_860
    } else if codePageString == SETTINGS_CODEPAGE_863 {
        codePageCode = SII_PM_CODE_PAGE_863
    } else if codePageString == SETTINGS_CODEPAGE_865 {
        codePageCode = SII_PM_CODE_PAGE_865
    } else if codePageString == SETTINGS_CODEPAGE_857 {
        codePageCode = SII_PM_CODE_PAGE_857
    } else if codePageString == SETTINGS_CODEPAGE_737 {
        codePageCode = SII_PM_CODE_PAGE_737
    } else if codePageString == SETTINGS_CODEPAGE_1252 {
        codePageCode = SII_PM_CODE_PAGE_1252
    } else if codePageString == SETTINGS_CODEPAGE_866 {
        codePageCode = SII_PM_CODE_PAGE_866
    } else if codePageString == SETTINGS_CODEPAGE_852 {
        codePageCode = SII_PM_CODE_PAGE_852
    } else if codePageString == SETTINGS_CODEPAGE_858 {
        codePageCode = SII_PM_CODE_PAGE_858
    } else if codePageString == SETTINGS_CODEPAGE_855 {
        codePageCode = SII_PM_CODE_PAGE_855
    } else if codePageString == SETTINGS_CODEPAGE_864 {
        codePageCode = SII_PM_CODE_PAGE_864
    } else if codePageString == SETTINGS_CODEPAGE_1250 {
        codePageCode = SII_PM_CODE_PAGE_1250
    } else if codePageString == SETTINGS_CODEPAGE_1251 {
        codePageCode = SII_PM_CODE_PAGE_1251
    } else if codePageString == SETTINGS_CODEPAGE_1253 {
        codePageCode = SII_PM_CODE_PAGE_1253
    } else if codePageString == SETTINGS_CODEPAGE_1254 {
        codePageCode = SII_PM_CODE_PAGE_1254
    }
    return codePageCode
}
