Imports System.IO

Public Class FormNastavitve

    Private Sub FormNastavitve_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Izvedi pri nalaganju okna z nastavitvami
        If My.Settings.Stolpci = 0 Then
            RadioButton1.Checked = True
        ElseIf My.Settings.Stolpci = 1 Then
            RadioButton2.Checked = True
        End If

        If My.Settings.DecimalnoLocilo = "Pika" Then
            RadioButton3.Checked = True
        ElseIf My.Settings.DecimalnoLocilo = "Vejica" Then
            RadioButton4.Checked = True
        End If

        TextBoxStoj.Text = My.Settings.KodaStojisce.ToString
        TextBoxStojInfo.Text = My.Settings.KodaStojisceInfo.ToString
        TextBoxOriHz.Text = My.Settings.KodaStojisceOri.ToString
        TextBoxQuickSet.Text = My.Settings.KodaQuickSet.ToString
        TextBoxSetAzi.Text = My.Settings.KodaSetAzi.ToString
        TextBoxRes.Text = My.Settings.KodaResection.ToString
        TextBoxFree.Text = My.Settings.KodaFreeStation.ToString
        TextBoxOriHt.Text = My.Settings.KodaOriHtTrans.ToString
        TextBoxMeasOri.Text = My.Settings.KodaMeasOri.ToString
        TextBoxMeas.Text = My.Settings.KodaMeas.ToString
        TextBoxImport.Text = My.Settings.KodaUserImport.ToString
        TextBoxLog.Text = My.Settings.DatLogfile.ToString

        LabelFontInfo.Text = My.Settings.Pisava + " / " + My.Settings.PisavaVelikost.ToString

    End Sub
    Private Sub FormNastavitve_Closing(sender As Object, e As EventArgs) Handles MyBase.Closing
        ' Izvedi preden zapreš okno z nastavitvami
        If RadioButton1.Checked = True Then
            My.Settings.Stolpci = 0
        End If

        If RadioButton2.Checked = True Then
            My.Settings.Stolpci = 1
        End If

        If RadioButton3.Checked = True Then
            My.Settings.DecimalnoLocilo = "Pika"
        End If

        If RadioButton4.Checked = True Then
            My.Settings.DecimalnoLocilo = "Vejica"
        End If

        My.Settings.KodaStojisce = TextBoxStoj.Text
        My.Settings.KodaStojisceInfo = TextBoxStojInfo.Text
        My.Settings.KodaStojisceOri = TextBoxOriHz.Text
        My.Settings.KodaQuickSet = TextBoxQuickSet.Text
        My.Settings.KodaSetAzi = TextBoxSetAzi.Text
        My.Settings.KodaResection = TextBoxRes.Text
        My.Settings.KodaFreeStation = TextBoxFree.Text
        My.Settings.KodaOriHtTrans = TextBoxOriHt.Text
        My.Settings.KodaMeasOri = TextBoxMeasOri.Text
        My.Settings.KodaMeas = TextBoxMeas.Text
        My.Settings.KodaUserImport = TextBoxImport.Text
        My.Settings.DatLogfile = TextBoxLog.Text

        My.Settings.Save()

        ShraniNastDat()

        Form1.DatotekaLogfile = My.Settings.DatLogfile
        Form1.RichTextBox1.Font = New Font(My.Settings.Pisava, My.Settings.PisavaVelikost)
    End Sub

    Public Sub ShraniNastDat()
        ' Izvoz kodiranja v TXT datoteko
        Dim dataNast As String = ""

        dataNast = dataNast + "DecimalnoLocilo = " + My.Settings.DecimalnoLocilo + vbCrLf
        dataNast = dataNast + "StolpciTab = " + My.Settings.Stolpci.ToString + vbCrLf
        dataNast = dataNast + "KodaStojisce = " + My.Settings.KodaStojisce + vbCrLf
        dataNast = dataNast + "KodaStojisceInfo = " + My.Settings.KodaStojisceInfo + vbCrLf
        dataNast = dataNast + "KodaStojisceOri = " + My.Settings.KodaStojisceOri + vbCrLf
        dataNast = dataNast + "KodaQuickSet = " + My.Settings.KodaQuickSet + vbCrLf
        dataNast = dataNast + "KodaSetAzi = " + My.Settings.KodaSetAzi + vbCrLf
        dataNast = dataNast + "KodaResection = " + My.Settings.KodaResection + vbCrLf
        dataNast = dataNast + "KodaFreeStation = " + My.Settings.KodaFreeStation + vbCrLf
        dataNast = dataNast + "KodaOriHtTrans = " + My.Settings.KodaOriHtTrans + vbCrLf
        dataNast = dataNast + "KodaMeasOri = " + My.Settings.KodaMeasOri + vbCrLf
        dataNast = dataNast + "KodaMeas = " + My.Settings.KodaMeas + vbCrLf
        dataNast = dataNast + "KodaUserImport = " + My.Settings.KodaUserImport + vbCrLf

        My.Computer.FileSystem.WriteAllText("tps1100_kode.txt", dataNast, False)

    End Sub

    Private Sub ButtonOdpriLog_Click(sender As Object, e As EventArgs) Handles ButtonOdpriLog.Click
        ' Pokaži pot do datoteke z zapisnikom procesiranja
        If SaveFileDialogLog.ShowDialog = DialogResult.OK Then
            TextBoxLog.Text = SaveFileDialogLog.FileName.ToString
            My.Settings.DatLogfile = SaveFileDialogLog.FileName.ToString
        End If
    End Sub

    Private Sub ButtonFont_Click(sender As Object, e As EventArgs) Handles ButtonFont.Click
        ' Izbira pisave za prikaz v glavnem oknu
        Dim fontdia1 = FontDialog1.Font.Name = My.Settings.Pisava
        Dim fontdia2 = FontDialog1.Font.Size = My.Settings.PisavaVelikost

        If FontDialog1.ShowDialog = DialogResult.OK Then
            My.Settings.Pisava = FontDialog1.Font.Name
            My.Settings.PisavaVelikost = FontDialog1.Font.Size
            LabelFontInfo.Text = My.Settings.Pisava + " / " + My.Settings.PisavaVelikost.ToString
        End If
    End Sub

    Private Sub IzvoziKodeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles IzvoziKodeToolStripMenuItem.Click
        ' Izvoz kodiranja v TXT datoteko
        Dim dataNast As String = ""

        dataNast = dataNast + "DecimalnoLocilo = " + My.Settings.DecimalnoLocilo + vbCrLf
        dataNast = dataNast + "StolpciTab = " + My.Settings.Stolpci.ToString + vbCrLf
        dataNast = dataNast + "KodaStojisce = " + My.Settings.KodaStojisce + vbCrLf
        dataNast = dataNast + "KodaStojisceInfo = " + My.Settings.KodaStojisceInfo + vbCrLf
        dataNast = dataNast + "KodaStojisceOri = " + My.Settings.KodaStojisceOri + vbCrLf
        dataNast = dataNast + "KodaQuickSet = " + My.Settings.KodaQuickSet + vbCrLf
        dataNast = dataNast + "KodaSetAzi = " + My.Settings.KodaSetAzi + vbCrLf
        dataNast = dataNast + "KodaResection = " + My.Settings.KodaResection + vbCrLf
        dataNast = dataNast + "KodaFreeStation = " + My.Settings.KodaFreeStation + vbCrLf
        dataNast = dataNast + "KodaOriHtTrans = " + My.Settings.KodaOriHtTrans + vbCrLf
        dataNast = dataNast + "KodaMeasOri = " + My.Settings.KodaMeasOri + vbCrLf
        dataNast = dataNast + "KodaMeas = " + My.Settings.KodaMeas + vbCrLf
        dataNast = dataNast + "KodaUserImport = " + My.Settings.KodaUserImport + vbCrLf

        If SaveFileDialogNast.ShowDialog = DialogResult.OK Then
            My.Computer.FileSystem.WriteAllText(SaveFileDialogNast.FileName, dataNast, False)
        End If
    End Sub

    Private Sub UvoziKodeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UvoziKodeToolStripMenuItem.Click
        ' Uvoz nastavitev iz datototeke
        If OpenFileDialogNast.ShowDialog = DialogResult.OK Then
            Dim streamReader As StreamReader = New StreamReader(OpenFileDialogNast.FileName)
            Do Until streamReader.EndOfStream
                ' Read and Split each line to separate it into fields
                Dim line() As String = streamReader.ReadLine().Split("=")
                Dim kodaIme As String
                Dim kodaVal As String
                kodaIme = Replace(line(0), " ", "")
                kodaVal = Replace(line(1), " ", "")
                If kodaIme = "KodaStojisce" Then
                    My.Settings.KodaStojisce = kodaVal
                    TextBoxStoj.Text = kodaVal.ToString
                End If
                If kodaIme = "KodaStojisceInfo" Then
                    My.Settings.KodaStojisceInfo = kodaVal
                    TextBoxStojInfo.Text = kodaVal.ToString
                End If
                If kodaIme = "KodaStojisceOri" Then
                    My.Settings.KodaStojisceOri = kodaVal
                    TextBoxOriHz.Text = kodaVal.ToString
                End If
                If kodaIme = "KodaQuickSet" Then
                    My.Settings.KodaQuickSet = kodaVal
                    TextBoxQuickSet.Text = kodaVal.ToString
                End If
                If kodaIme = "KodaSetAzi" Then
                    My.Settings.KodaSetAzi = kodaVal
                    TextBoxSetAzi.Text = kodaVal.ToString
                End If
                If kodaIme = "KodaResection" Then
                    My.Settings.KodaResection = kodaVal
                    TextBoxRes.Text = kodaVal.ToString
                End If
                If kodaIme = "KodaFreeStation" Then
                    My.Settings.KodaFreeStation = kodaVal
                    TextBoxFree.Text = kodaVal.ToString
                End If
                If kodaIme = "KodaOriHtTrans" Then
                    My.Settings.KodaOriHtTrans = kodaVal
                    TextBoxOriHt.Text = kodaVal.ToString
                End If
                If kodaIme = "KodaMeasOri" Then
                    My.Settings.KodaMeasOri = kodaVal
                    TextBoxMeasOri.Text = kodaVal.ToString
                End If
                If kodaIme = "KodaMeas" Then
                    My.Settings.KodaMeas = kodaVal
                    TextBoxMeas.Text = kodaVal.ToString
                End If
                If kodaIme = "KodaUserImport" Then
                    My.Settings.KodaUserImport = kodaVal
                    TextBoxImport.Text = kodaVal.ToString
                End If
            Loop

            streamReader.Close()
            streamReader.Dispose()
            My.Settings.Save()

        End If
    End Sub

    Private Sub ButtonShraniZapri_Click(sender As Object, e As EventArgs) Handles ButtonShraniZapri.Click
        Me.Close()

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

    End Sub

    Private Sub PonastaviKodeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PonastaviKodeToolStripMenuItem.Click
        ' Ponastavi Kode na privzete vrednosti
        My.Settings.KodaStojisce = "1"
        My.Settings.KodaStojisceInfo = "2"
        My.Settings.KodaStojisceOri = "3"
        My.Settings.KodaQuickSet = "10"
        My.Settings.KodaSetAzi = "20"
        My.Settings.KodaResection = "30"
        My.Settings.KodaFreeStation = "40"
        My.Settings.KodaOriHtTrans = "50"
        My.Settings.KodaMeasOri = "80"
        My.Settings.KodaMeas = "90"
        My.Settings.KodaUserImport = "99"

        My.Settings.Save()

        TextBoxStoj.Text = My.Settings.KodaStojisce.ToString
        TextBoxStojInfo.Text = My.Settings.KodaStojisceInfo.ToString
        TextBoxOriHz.Text = My.Settings.KodaStojisceOri.ToString
        TextBoxQuickSet.Text = My.Settings.KodaQuickSet.ToString
        TextBoxSetAzi.Text = My.Settings.KodaSetAzi.ToString
        TextBoxRes.Text = My.Settings.KodaResection.ToString
        TextBoxFree.Text = My.Settings.KodaFreeStation.ToString
        TextBoxOriHt.Text = My.Settings.KodaOriHtTrans.ToString
        TextBoxMeasOri.Text = My.Settings.KodaMeasOri.ToString
        TextBoxMeas.Text = My.Settings.KodaMeas.ToString
        TextBoxImport.Text = My.Settings.KodaUserImport.ToString
    End Sub
End Class