Imports System.IO
Imports System.Text

Public Class frmMqtt
    Private programForm As String = "DUALSONIC T/D MEASURING INSTRUMENT"

    ' TODO: 제공된 사용자 이름과 암호를 사용하여 사용자 지정 인증을 수행하는 코드를 삽입합니다
    ' (https://go.microsoft.com/fwlink/?LinkId=35339 참조).  
    ' 그러면 사용자 지정 보안 주체가 현재 스레드의 보안 주체에 다음과 같이 첨부될 수 있습니다. 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' 여기서 CustomPrincipal은 인증을 수행하는 데 사용되는 IPrincipal이 구현된 것입니다. 
    ' 나중에 My.User는 CustomPrincipal 개체에 캡슐화된 사용자 이름, 표시 이름 등의
    ' ID 정보를 반환합니다.

    Private Sub frmMqtt_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim versionNum As String = "2.3.0"
        Me.Text = "DUALSONIC T/D MEASURING INSTRUMENT_" & versionNum & "- MQTT Setting"
    End Sub

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        If txbMqttAd.Text = String.Empty Or txbMqttTp.Text = String.Empty Then
            MsgBox("브로커와 토픽을 제대로 입력하여 주십시오.", vbExclamation)
            GoTo failResult
        End If

        If saveMqtt() = False Then
            GoTo failResult
        End If

        If loadMqtt() = False Then
            GoTo failResult
        End If

        Me.Close()
        Exit Sub

failResult:
        txbMqttAd.Text = String.Empty
        txbMqttTp.Text = String.Empty
        frmTD_JIG.mqttBroker = String.Empty
        frmTD_JIG.mqttTopic = String.Empty
        txbMqttAd.Focus()
        Exit Sub
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        If loadMqtt() = False Then
            txbMqttAd.Text = String.Empty
            txbMqttTp.Text = String.Empty
            frmTD_JIG.mqttBroker = String.Empty
            frmTD_JIG.mqttTopic = String.Empty
        End If

        Me.Close()
    End Sub

    Private Function saveMqtt() As Boolean
        Try
            Dim filePath, filename, saveStr As String

            filePath = System.AppDomain.CurrentDomain.BaseDirectory() & "\Config"
            dirUse(filePath)
            filename = filePath & "\TD_Select_MQTT_Config.ini"

            saveStr = txbMqttAd.Text & "^" & txbMqttTp.Text

            If File.Exists(filename) Then
                File.Delete(filename)
            End If

            File.WriteAllText(filename, saveStr, Encoding.UTF8)

            Return True
        Catch ex As Exception
            MsgBox("등록 오류 ::" & ex.Message, vbCritical)
            Return False
        End Try

    End Function

    Public Function loadMqtt() As Boolean
        Try
            Dim filePath, fileName, readData As String

            filePath = System.AppDomain.CurrentDomain.BaseDirectory & "\Config"
            dirUse(filePath)
            fileName = filePath & "\TD_Select_MQTT_Config.ini"

            If File.Exists(fileName) Then
                readData = File.ReadAllText(fileName, System.Text.Encoding.UTF8)
                If readData <> Nothing Or readData <> "" Then
                    If UBound(Split(readData, "^")) = 1 Then
                        txbMqttAd.Text = Mid(readData, 1, InStr(readData, "^") - 1)
                        txbMqttTp.Text = Mid(readData, InStr(readData, "^") + 1, readData.Length)

                        frmTD_JIG.mqttBroker = txbMqttAd.Text
                        frmTD_JIG.mqttTopic = txbMqttTp.Text

                        Return True
                    ElseIf UBound(Split(readData, "^")) > 1 Then
                        MsgBox("잘못된 데이터가 존재합니다.", vbCritical)
                        Return False
                    Else
                        Return False
                    End If
                Else
                    Return False
                End If
            Else
                Return False
            End If
        Catch ex As Exception
            MsgBox("오류 발생 :: " + ex.Message & vbCritical)
            Return False
        End Try

    End Function

    Private Sub dirUse(filePath As String)
        If Dir(filePath, vbDirectory) = "" Then
            MkDir(filePath)
        End If
    End Sub

End Class
