Imports System.Collections.Specialized.BitVector32
Imports System.IO
Imports System.Windows.Forms.VisualStyles.VisualStyleElement

Public Class Form2
    
    Public DatotekaSpemembe As Boolean = False
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Activated
        Form1.Hide()
        RichTextBoxEdit.Clear()

        ' prepare StreamReader
        Dim streamReader As StreamReader = New StreamReader(My.Settings.ZadnjaMapa + "\" + My.Settings.Datoteka)
        Dim i As Integer = 0

        ' loop untill end of stream
        Do Until streamReader.EndOfStream
            ' Beri vrstico po vrstico
            Dim line As String = streamReader.ReadLine()
            RichTextBoxEdit.AppendText(line + vbCrLf)
            i = i + 1
        Loop
        ToolStripStatusLabel1.Text = "Prebrano: " + i.ToString + " | Datoteka: " + My.Settings.ZadnjaMapa + "\" + My.Settings.Datoteka
        DatotekaSpemembe = False
        BtnShraniF2.Enabled = False
        ComBoxKoda.SelectedIndex = 0
        streamReader.Close()
        streamReader.Dispose()
    End Sub

    Private Sub Form2_Close(sender As Object, e As EventArgs) Handles MyBase.Closing
        ' If DatotekaSpemembe = True Then
        '     MessageBox.Show("Shranim spremembe?", "Podatki spremenjeni", MessageBoxButtons.YesNo)
        '     If MessageBoxButtons.YesNo = vbYes Then
        '         RichTextBoxEdit.SaveFile(My.Settings.ZadnjaMapa, RichTextBoxStreamType.PlainText)
        '         BtnShraniF2.Enabled = False
        '         DatotekaSpemembe = False
        '     End If
        ' End If
        Form1.RichTextBox1.Clear()
        Form1.streamLines = 0
        Dim streamReader As StreamReader = New StreamReader(Form1.OpenFileDialog1.FileName)
        Do Until streamReader.EndOfStream
            Form1.RichTextBox1.AppendText(streamReader.ReadLine + vbCrLf)
            Form1.streamLines = Form1.streamLines + 1
        Loop
        Form1.ToolStripProgressBar1.Maximum = Form1.streamLines
        streamReader.Close()
        streamReader.Dispose()

        Form1.Show()
    End Sub

    Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxEdit.TextChanged
        ToolStripStatusLabel1.Text = "Spremenjeni podatki... datoteka je bila urejana."
        BtnShraniF2.Enabled = True
        DatotekaSpemembe = True
    End Sub

    Private Sub BtnShraniF2_Click(sender As Object, e As EventArgs) Handles BtnShraniF2.Click
        ' SaveFileDialog2.FileName = My.Settings.ZadnjaMapa
        ' If SaveFileDialog2.ShowDialog = DialogResult.OK Then
        RichTextBoxEdit.SaveFile(My.Settings.ZadnjaMapa + "\" + My.Settings.Datoteka, RichTextBoxStreamType.PlainText)
        ToolStripStatusLabel1.Text = "Shranjeno: " + SaveFileDialog2.FileName.ToString
        BtnShraniF2.Enabled = False
        DatotekaSpemembe = False
        ' End If
    End Sub


    Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
        ' 0 ---
        ' 1 Setup:  Quick Set
        ' 2 Setup: Set Azimuth
        ' 3 Setup:  Resection
        ' 4 Setup:  Free Station
        ' 5 Setup:  Ori. & Ht. Transfer
        ' 6 Measure: Orientation
        ' 7 Measure: Points
        ' 8 Data:   User-Entered
        ' 9 Info:   Station
        ' 10 Info:   Temp. & Press.
        Dim dataTekst As String = ""
        If ComBoxKoda.SelectedIndex <= 0 Then

        ElseIf ComBoxKoda.SelectedIndex = 1 Then    ' Koda Setup: Quick Set
            dataTekst = "*410000+" + My.Settings.KodaQuickSet.PadLeft(16, "0") + " "
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        ElseIf ComBoxKoda.SelectedIndex = 2 Then    ' Koda Setup: Set Azimut
            dataTekst = "*410000+" + My.Settings.KodaSetAzi.PadLeft(16, "0") + " "
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        ElseIf ComBoxKoda.SelectedIndex = 3 Then    ' Koda Setup: Resection
            dataTekst = "*410000+" + My.Settings.KodaResection.PadLeft(16, "0") + " "
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        ElseIf ComBoxKoda.SelectedIndex = 4 Then    ' Koda Setup: Free Station
            dataTekst = "*410000+" + My.Settings.KodaFreeStation.PadLeft(16, "0") + " "
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        ElseIf ComBoxKoda.SelectedIndex = 5 Then    ' Koda Setup: Ori. & Ht. Transfer
            dataTekst = "*410000+" + My.Settings.KodaOriHtTrans.PadLeft(16, "0") + " "
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        ElseIf ComBoxKoda.SelectedIndex = 6 Then    ' Koda Orientacija
            dataTekst = "*410000+" + My.Settings.KodaMeasOri.PadLeft(16, "0") + " "
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        ElseIf ComBoxKoda.SelectedIndex = 7 Then    ' Koda Meritve
            dataTekst = "*410000+" + My.Settings.KodaMeas.PadLeft(16, "0") + " "
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        ElseIf ComBoxKoda.SelectedIndex = 8 Then    ' Koda User-Entered/Import
            dataTekst = "*410000+" + My.Settings.KodaUserImport.PadLeft(16, "0") + " "
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        ElseIf ComBoxKoda.SelectedIndex = 9 Then    ' Koda Stojišče
            Dim kodaStoj As String = ToolStripTextBox1.Text.PadLeft(16, "0")
            Dim kodaStojVis As Double = ToolStripTextBox2.Text.Replace(",", ".") * 1000
            dataTekst = "*410000+" + My.Settings.KodaStojisce.PadLeft(16, "0") + " 71....+" + kodaStoj.PadLeft(16, "0") + " 72....+" + Format(kodaStojVis, "0000000000000000").ToString + " "
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        ElseIf ComBoxKoda.SelectedIndex = 10 Then    ' Koda Podatki Temp./Pritisk
            Dim kodaVremeTemp As Double = ToolStripTextBox3.Text.Replace(",", ".")
            Dim kodaVremePrit As Double = ToolStripTextBox4.Text.Replace(",", ".") ' 1 mbar/hPa = 0.750062 Torr
            dataTekst = "*410000+" + My.Settings.KodaStojisceInfo.PadLeft(16, "0") + " 71....+" + Format(kodaVremeTemp, "0000000000000000").ToString + " 72....+" + Format(kodaVremePrit, "0000000000000000").ToString + " "
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        ElseIf ComBoxKoda.SelectedIndex = 11 Then    ' Koda Orientacija
            Dim kodaOri As String = ToolStripTextBox1.Text.PadLeft(16, "0")
            Dim kodaOriHz As Double = ToolStripTextBox5.Text.Replace(",", ".") * 100000
            dataTekst = "*410000+" + My.Settings.KodaStojisceOri.PadLeft(16, "0") + " 71....+" + kodaOri.PadLeft(16, "0") + " 72....+" + Format(kodaOriHz, "0000000000000000").ToString + " "
            BtnShraniF2.Enabled = True
            DatotekaSpemembe = True
        End If
        RichTextBoxEdit.Focus()
        RichTextBoxEdit.SelectionStart = RichTextBoxEdit.GetFirstCharIndexOfCurrentLine
        SendKeys.Send("+{HOME}")
        RichTextBoxEdit.SelectedText = dataTekst + vbCrLf
    End Sub

    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        RichTextBoxEdit.Undo()
    End Sub

    Private Sub ToolStripButton3_Click(sender As Object, e As EventArgs) Handles ToolStripButton3.Click
        Me.Close()
    End Sub

    Private Sub ComBoxKoda_Click(sender As Object, e As EventArgs) Handles ComBoxKoda.SelectedIndexChanged
        If ComBoxKoda.SelectedIndex <= 0 Then
            ToolStripButton2.Enabled = False
            ToolStripLabel2.Visible = False
            ToolStripLabel3.Visible = False
            ToolStripLabel4.Visible = False
            ToolStripLabel5.Visible = False
            ToolStripLabel6.Visible = False
            ToolStripTextBox1.Visible = False
            ToolStripTextBox2.Visible = False
            ToolStripTextBox3.Visible = False
            ToolStripTextBox4.Visible = False
            ToolStripTextBox5.Visible = False
        ElseIf ComBoxKoda.SelectedIndex = 9 Then
            ToolStripButton2.Enabled = True
            ToolStripLabel2.Visible = True
            ToolStripTextBox1.Visible = True
            ToolStripLabel3.Visible = True
            ToolStripTextBox2.Visible = True
            ToolStripLabel4.Visible = False
            ToolStripTextBox3.Visible = False
            ToolStripLabel5.Visible = False
            ToolStripTextBox4.Visible = False
            ToolStripLabel6.Visible = False
            ToolStripTextBox5.Visible = False
        ElseIf ComBoxKoda.SelectedIndex = 10 Then
            ToolStripButton2.Enabled = True
            ToolStripLabel2.Visible = False
            ToolStripTextBox1.Visible = False
            ToolStripLabel3.Visible = False
            ToolStripTextBox2.Visible = False
            ToolStripLabel4.Visible = True
            ToolStripTextBox3.Visible = True
            ToolStripLabel5.Visible = True
            ToolStripTextBox4.Visible = True
            ToolStripLabel6.Visible = False
            ToolStripTextBox5.Visible = False
        ElseIf ComBoxKoda.SelectedIndex = 11 Then
            ToolStripButton2.Enabled = True
            ToolStripLabel2.Visible = True
            ToolStripTextBox1.Visible = True
            ToolStripLabel3.Visible = False
            ToolStripTextBox2.Visible = False
            ToolStripLabel4.Visible = False
            ToolStripTextBox3.Visible = False
            ToolStripLabel5.Visible = False
            ToolStripTextBox4.Visible = False
            ToolStripLabel6.Visible = True
            ToolStripTextBox5.Visible = True
        Else
            ToolStripButton2.Enabled = True
            ToolStripLabel2.Visible = False
            ToolStripLabel3.Visible = False
            ToolStripLabel4.Visible = False
            ToolStripLabel5.Visible = False
            ToolStripLabel6.Visible = False
            ToolStripTextBox1.Visible = False
            ToolStripTextBox2.Visible = False
            ToolStripTextBox3.Visible = False
            ToolStripTextBox4.Visible = False
            ToolStripTextBox5.Visible = False
        End If
    End Sub
End Class