Imports System.IO

Public Class Form1

    Public DatotekaLogfile As String = My.Settings.DatLogfile
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If DatotekaLogfile = "" Then
            DatotekaLogfile = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "tps1100_log.txt")
            My.Settings.DatLogfile = DatotekaLogfile
            My.Settings.Save()
        End If
        My.Forms.Form1.Text = My.Application.Info.Title + " " + My.Application.Info.Version.Major.ToString + "." + My.Application.Info.Version.Minor.ToString + "." + My.Application.Info.Version.Build.ToString + "." + My.Application.Info.Version.Revision.ToString
        tsbDecimale.Text = My.Settings.DecimalnoLocilo
        tsbStolpci.SelectedIndex = My.Settings.Stolpci
    End Sub

    Public streamLines As Integer = 0
    Private Sub tsbOpenFile_Click(sender As Object, e As EventArgs) Handles tsbOpenFile.Click
        OpenFileDialog1.ReadOnlyChecked = False
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            RichTextBox1.Clear()
            ToolStripProgressBar1.Value = 0
            Dim streamReader As StreamReader = New StreamReader(OpenFileDialog1.FileName)
            Do Until streamReader.EndOfStream
                RichTextBox1.AppendText(streamReader.ReadLine + vbCrLf)
                streamLines = streamLines + 1
            Loop
            ToolStripProgressBar1.Maximum = streamLines
            streamReader.Close()
            streamReader.Dispose()

            My.Settings.Datoteka = OpenFileDialog1.SafeFileName
            My.Settings.ZadnjaMapa = OpenFileDialog1.FileName
            My.Settings.Stolpci = tsbStolpci.SelectedIndex
            tsbFileName.Text = My.Settings.Datoteka
            ToolStripStatusLabel1.Text = My.Settings.ZadnjaMapa
            tsbSaveAs.Text = "Shrani ..."
            tsbSaveAs.Enabled = False
            tsbConvert.Enabled = True
            tsbPointsOnly.Enabled = True
            ButtonUrediDat.Enabled = True
            RichTextBox1.BackColor = SystemColors.Info
            LabelDatoteka.Image = My.Resources.icons8_ok_50
            My.Settings.Save()
        Else
            tsbFileName.Text = "Odpri GSI datoteko..."
            RichTextBox1.Clear()
            tsbSaveAs.Text = "Shrani ..."
            tsbSaveAs.Enabled = False
            tsbConvert.Enabled = False
            tsbPointsOnly.Enabled = False
        End If
        OpenFileDialog1.Dispose()
    End Sub

    Private Sub tsbConvert_Click(sender As Object, e As EventArgs) Handles tsbConvert.Click
        ToolStripStatusLabel1.Text = "Procesiranje..."
        RichTextBox1.Clear()
        RichTextBox1.BackColor = SystemColors.Info
        RichTextBox1.AppendText("TPS 1100 Zapisnik" + vbCrLf + "++++++++++++++++++++++++++++++++" + vbCrLf)
        ToolStripProgressBar1.Maximum = streamLines
        ' prepare StreamReader
        Dim streamReader As StreamReader = New StreamReader(OpenFileDialog1.FileName)
        Dim i As Integer = 0
        Dim PodatkiStojisce As Boolean = False
        Dim dataStolpci As String = ""
        If tsbStolpci.SelectedIndex = 0 Then
            dataStolpci = " "
        ElseIf tsbStolpci.SelectedIndex = 1 Then
            dataStolpci = vbTab
        End If
        My.Settings.Stolpci = tsbStolpci.SelectedIndex
        My.Settings.Save()
        ' loop untill end of stream
        Do Until streamReader.EndOfStream
            ' Read and Split each line to separate it into fields
            Dim line() As String = streamReader.ReadLine().Split(" ")
            ' Remove * sign from index line
            line(0) = Replace(line(0), "*", "")
            ToolStripProgressBar1.Value = i
            ' Check if it's a new station setup
            If Microsoft.VisualBasic.Left(line(1), 2) = "25" Then
                RichTextBox1.AppendText(vbCrLf + "Station:" + vbCrLf + "****************" + vbCrLf)
                RichTextBox1.AppendText("PtID" + dataStolpci + "HzCor" + dataStolpci + "StnE" + dataStolpci + "StnN" + dataStolpci + "StnH" + dataStolpci + "ReflHt" + dataStolpci + "InstHt" + vbCrLf)
            End If

            If Microsoft.VisualBasic.Left(line(1), 2) = "84" Then
                RichTextBox1.AppendText(vbCrLf + "Station:" + vbCrLf + "****************" + vbCrLf)
                RichTextBox1.AppendText("PtID" + dataStolpci + "StnE" + dataStolpci + "StnN" + dataStolpci + "StnH" + dataStolpci + "ReflHt" + dataStolpci + "InstHt" + vbCrLf)
            End If
            ' Seaparate word index and data
            For Each readLine As String In line
                If readLine = "" Then
                    ' do nothing
                Else
                    ' wordindex is always 6 characters long and is read fron left to right
                    Dim wordIndex As String
                    wordIndex = Microsoft.VisualBasic.Left(readLine, 2)

                    ' data is always 17 characters long and is read from right to left and has a +/- sign in first place
                    Dim data As String
                    Dim dataTmp As String
                    Dim numData As Double
                    data = Microsoft.VisualBasic.Right(readLine, 17)

                    If wordIndex = "11" Then
                        data = Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                    ElseIf wordIndex = "19" Then    ' Date/Time
                        data = dataStolpci + "DateTime= " + Microsoft.VisualBasic.Right(data, 8)
                    ElseIf wordIndex = "21" Then    ' Horizontal angle
                        numData = data / 100000
                        data = dataStolpci + Format(numData, "0.0000")
                    ElseIf wordIndex = "22" Then    ' Vertical angle
                        numData = data / 100000
                        data = dataStolpci + Format(numData, "0.0000")
                    ElseIf wordIndex = "25" Then    ' Hz. Orientation correction
                        numData = data / 100000
                        data = dataStolpci + Format(numData, "0.0000")
                    ElseIf wordIndex = "31" Then    ' Slope distance
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "32" Then    ' Horizontal distance
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "33" Then    ' Height difference
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "34" Then    ' Stationing
                        numData = data / 1000
                        data = dataStolpci + "Station= " + Format(numData, "0.000")
                    ElseIf wordIndex = "35" Then    ' Info za offset???
                        numData = data / 1000
                        data = dataStolpci + "Offset= " + Format(numData, "0.000")
                    ElseIf wordIndex = "41" Then    ' Code
                        'numData = data
                        'data = "Code= " + numData.ToString
                        If My.Settings.KodeUporabnik = "Da" Then
                            If data = "+0000000000000010" Then
                                data = vbCrLf + "Setup: Quick Set" + vbCrLf + "============================="
                                PodatkiStojisce = True
                            End If
                            If data = "+0000000000000020" Then
                                data = vbCrLf + "Setup: Set Hz" + vbCrLf + "=============================" + vbCrLf + "PtID" + dataStolpci + "HZ" + dataStolpci + "V" + dataStolpci + "SD" + dataStolpci + "HD" + dataStolpci + "ReflHt" + dataStolpci + "PrCon" + dataStolpci + "HtDif" + dataStolpci + "PtCode"
                                PodatkiStojisce = True
                            End If
                            If data = "+0000000000000030" Then
                                data = vbCrLf + "Setup: Resection" + vbCrLf + "=============================" + vbCrLf + "PtID" + dataStolpci + "HZ" + dataStolpci + "V" + dataStolpci + "SD" + dataStolpci + "HD" + dataStolpci + "ReflHt" + dataStolpci + "PrCon" + dataStolpci + "HtDif" + dataStolpci + "PtCode"
                                PodatkiStojisce = True
                            End If
                            If data = "+0000000000000040" Then
                                data = vbCrLf + "Setup: Free Station" + vbCrLf + "=============================" + vbCrLf + "PtID" + dataStolpci + "HZ" + dataStolpci + "V" + dataStolpci + "SD" + dataStolpci + "HD" + dataStolpci + "ReflHt" + dataStolpci + "PrCon" + dataStolpci + "HtDif" + dataStolpci + "PtCode"
                                PodatkiStojisce = True
                            End If
                            If data = "+0000000000000050" Then
                                data = vbCrLf + "Setup: Orientation & Ht. transfer" + vbCrLf + "=============================" + vbCrLf + "PtID" + dataStolpci + "HZ" + dataStolpci + "V" + dataStolpci + "SD" + dataStolpci + "HD" + dataStolpci + "ReflHt" + dataStolpci + "PrCon" + dataStolpci + "HtDif" + dataStolpci + "PtCode"
                                PodatkiStojisce = True
                            End If
                            If data = "+0000000000000090" Then
                                data = vbCrLf + "Measured:" + vbCrLf + "----------------------" + vbCrLf + "PtID" + dataStolpci + "HZ" + dataStolpci + "V" + dataStolpci + "SD" + dataStolpci + "HD" + dataStolpci + "ReflHt" + dataStolpci + "PrCon" + dataStolpci + "HtDif" + dataStolpci + "E" + dataStolpci + "N" + dataStolpci + "H" + dataStolpci + "PtCode"
                                PodatkiStojisce = False
                            End If
                            If data = "+0000000000000099" Then
                                data = vbCrLf + "Imported/User-Entered:" + vbCrLf + "----------------------" + vbCrLf + "PtID" + dataStolpci + "E" + dataStolpci + "N" + dataStolpci + "H"
                                PodatkiStojisce = False
                            End If
                            If data = "+00000000STA_OFFS" Then
                                data = "Offsets: " + vbCrLf
                                PodatkiStojisce = False
                            End If
                        End If
                    ElseIf wordIndex = "42" Then    ' Code: Info 1
                        data = dataStolpci + "Info1= " + Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                    ElseIf wordIndex = "43" Then    ' Code: Info 2
                        data = dataStolpci + "Info2= " + Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                    ElseIf wordIndex = "44" Then    ' Code: Info 3
                        data = dataStolpci + "Info3= " + Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                    ElseIf wordIndex = "45" Then    ' Code: Info 4
                        data = dataStolpci + "Info4= " + Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                    ElseIf wordIndex = "47" Then    ' Code: Info 6
                        data = dataStolpci + "Info6= " + Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                    ElseIf wordIndex = "58" Then    ' Prism constant
                        numData = data / 10000
                        data = dataStolpci + numData.ToString
                    ElseIf wordIndex = "71" Then    ' Point Code
                        dataTmp = Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                        If dataTmp = "" Then
                            data = ""
                        Else
                            data = dataStolpci + dataTmp
                        End If
                    ElseIf wordIndex = "81" And PodatkiStojisce = False Then    ' Target Easting
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "82" And PodatkiStojisce = False Then    ' Target Northing
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "83" And PodatkiStojisce = False Then    ' Target Height
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "84" Then    ' Station Easting
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "85" Then    ' Station Northing
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "86" Then    ' Station Height
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "87" Then    ' Reflector height
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "88" Then    ' Instriment height
                        numData = data / 1000
                        data = dataStolpci + Format(numData, "0.000")
                    ElseIf wordIndex = "18" Then    ' DATE YY.ss
                        numData = data / 1000000
                        data = dataStolpci + "Year.Sec= " + Format(numData, "0.00")
                    Else
                        numData = 0
                        data = ""
                    End If
                    If tsbDecimale.Text = "Pika" Then
                        If wordIndex IsNot "11" Then
                            data = data.ToString.Replace(",", ".")
                        End If

                    End If
                    RichTextBox1.AppendText(data)
                End If
            Next
            RichTextBox1.AppendText(vbCrLf)
            i = i + 1
        Loop
        ToolStripStatusLabel1.Text = "Prebrano: " + i.ToString + "/" + ToolStripProgressBar1.Maximum.ToString + " vrstic"
        SaveFileDialog1.Filter = "Zapisnik meritve|*.mer|Text file|*.txt|All files|*.*"
        streamReader.Close()
        tsbSaveAs.Enabled = True
        tsbSaveAs.Text = "Shrani Zapisnik"
    End Sub

    Private Sub tsbPointsOnly_Click(sender As Object, e As EventArgs) Handles tsbPointsOnly.Click
        Dim dataStolpci As String = ""
        If tsbStolpci.SelectedIndex = 0 Then
            dataStolpci = " "
        ElseIf tsbStolpci.SelectedIndex = 1 Then
            dataStolpci = vbTab
        End If
        My.Settings.Stolpci = tsbStolpci.SelectedIndex
        My.Settings.Save()
        RichTextBox1.Clear()
        RichTextBox1.BackColor = SystemColors.Info
        Dim measuredPoints As Integer = 1
        Dim newStation As Integer = 0
        Dim streamReader As StreamReader = New StreamReader(OpenFileDialog1.FileName)
        Dim i As Integer = 0
        Dim lin As Integer = 0
        Do Until streamReader.EndOfStream
            ' Read and Split each line to separate it into fields
            Dim line() As String = streamReader.ReadLine().Split(" ")
            ' Remove * sign from index line
            line(0) = Replace(line(0), "*", "")
            ToolStripProgressBar1.Value = i
            If Microsoft.VisualBasic.Left(line(1), 2) = "25" Then
                measuredPoints = 1
            End If
            ' Seaparate word index and data
            For Each readLine As String In line

                If readLine = "" Then
                    ' do nothing
                Else
                    ' wordindex is always 6 characters long and is read fron left to right
                    Dim wordIndex As String
                    wordIndex = Microsoft.VisualBasic.Left(readLine, 2)
                    ' data is always 17 characters long and is read from right to left and has a +/- sign in first place
                    Dim data As String
                    Dim numData As Double

                    data = Microsoft.VisualBasic.Right(readLine, 17)
                    If wordIndex = "41" Then
                        If data = "+00000000STA_OFFS" Then
                            data = "90"
                        End If
                        numData = data
                        If numData = 90 Then
                            measuredPoints = 1
                        ElseIf numData < 90 Then
                            measuredPoints = 0
                        Else
                            measuredPoints = 2
                        End If
                    End If

                    If wordIndex = "11" And measuredPoints >= 1 Then
                        data = Microsoft.VisualBasic.Right(data, 16).TrimStart("0"c)
                        RichTextBox1.AppendText(data + dataStolpci)
                        i = i + 1
                    ElseIf wordIndex = "81" And measuredPoints >= 1 Then    ' Target Easting
                        numData = data / 1000
                        data = Format(numData, "0.000")
                        If tsbDecimale.Text = "Pika" Then
                            data = data.ToString.Replace(",", ".")
                        End If
                        RichTextBox1.AppendText(data + dataStolpci)
                    ElseIf wordIndex = "82" And measuredPoints >= 1 Then    ' Target Northing
                        numData = data / 1000
                        data = Format(numData, "0.000")
                        If tsbDecimale.Text = "Pika" Then
                            data = data.ToString.Replace(",", ".")
                        End If
                        RichTextBox1.AppendText(data + dataStolpci)
                    ElseIf wordIndex = "83" And measuredPoints >= 1 Then    ' Target Height
                        numData = data / 1000
                        data = Format(numData, "0.000")
                        If tsbDecimale.Text = "Pika" Then
                            data = data.ToString.Replace(",", ".")
                        End If
                        If measuredPoints = 2 Then
                            RichTextBox1.AppendText(data + dataStolpci + "REF" + vbCrLf)
                        Else
                            RichTextBox1.AppendText(data + dataStolpci + "MEAS" + vbCrLf)
                        End If
                    ElseIf wordIndex = "84" And measuredPoints = 1 Then    ' Station Easting
                        numData = data / 1000
                        data = Format(numData, "0.000")
                        If tsbDecimale.Text = "Pika" Then
                            data = data.ToString.Replace(",", ".")
                        End If
                        RichTextBox1.AppendText(data + dataStolpci)
                    ElseIf wordIndex = "85" And measuredPoints = 1 Then    ' Station Northing
                        numData = data / 1000
                        data = Format(numData, "0.000")
                        If tsbDecimale.Text = "Pika" Then
                            data = data.ToString.Replace(",", ".")
                        End If
                        RichTextBox1.AppendText(data + dataStolpci)
                    ElseIf wordIndex = "86" And measuredPoints = 1 Then    ' Station Height
                        numData = data / 1000
                        data = Format(numData, "0.000")
                        If tsbDecimale.Text = "Pika" Then
                            data = data.ToString.Replace(",", ".")
                        End If
                        RichTextBox1.AppendText(data + dataStolpci + "STA" + vbCrLf)
                    Else
                        numData = 0
                        data = ""
                    End If
                End If
            Next
            lin = lin + 1
            ToolStripProgressBar1.Value = lin
        Loop
        ToolStripStatusLabel1.Text = "Prebrano točk: " + i.ToString
        SaveFileDialog1.Filter = "KOO datoteka|*.koo|Text file (Tab)|*.txt|All files|*.*"
        streamReader.Close()
        tsbSaveAs.Enabled = True
        tsbSaveAs.Text = "Shrani Točke"
    End Sub

    Private Sub tsbSaveAs_Click(sender As Object, e As EventArgs) Handles tsbSaveAs.Click
        SaveFileDialog1.FileName = Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName)
        If SaveFileDialog1.ShowDialog = DialogResult.OK Then
            RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
            ToolStripStatusLabel1.Text = "Shranjeno: " + SaveFileDialog1.FileName.ToString
            RichTextBox1.BackColor = SystemColors.ControlLightLight
            My.Computer.FileSystem.WriteAllText(DatotekaLogfile, System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " | " + OpenFileDialog1.FileName.ToString + " -> " + SaveFileDialog1.FileName.ToString + vbCrLf, True)
        End If
    End Sub

    Private Sub tsbHelp_Click(sender As Object, e As EventArgs) Handles tsbHelp.Click
        ' MessageBox.Show("Za konvertiranje GSI datoteke najprej izberi datoteko, ki jo želiš konvertirati z ukazom 'Open File'. Nato izberi metodo konverzije (Poročilo, Merjene točke, Vse točke). Po končani knverziji shrani novo datoteko z ukazom 'Save As...'","Kratka navodila")
        SplashScreen1.ShowDialog()

    End Sub

    Private Sub ToolStripComboBox2_Click(sender As Object, e As EventArgs) Handles MyBase.FormClosing
        My.Settings.DecimalnoLocilo = tsbDecimale.Text
        My.Settings.Save()
    End Sub

    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ButtonUrediDat.Click
        Form2.ShowDialog()
    End Sub

    Private Sub ToolStripButtonNast_Click(sender As Object, e As EventArgs) Handles ToolStripButtonNast.Click
        FormNastavitve.ShowDialog()
    End Sub
End Class
