Imports System.IO

Public Class FormGsiJobs

    Public projektAktiven
    Public projektAktivenMapa
    Public projektIzbran
    Public projektIzbranMapa
    Public jobIzbran
    Public jobDatoteka

    ' Funkcije izvedene pri nalaganju okna
    Private Sub FormGsiJobs_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        projektAktiven = My.Settings.Projekt.ToString
        projektAktivenMapa = My.Settings.ProjektMapa.ToString
        LabelDeloProj.Text = projektAktiven
        LabelDeloMapa.Text = projektAktivenMapa

        ProjektBeriNast()

    End Sub

    Public Sub ProjektBeriNast()
        Dim datProjektiNast = "tps1100_projekti.txt"
        ' Uvoz nastavitev iz datototeke
        If My.Computer.FileSystem.FileExists(datProjektiNast) Then
            Dim streamReader As StreamReader = New StreamReader(datProjektiNast)
            Do Until streamReader.EndOfStream
                ' Read and Split each line to separate it into fields
                Dim line() As String = streamReader.ReadLine().Split("=")

                If (line(0) IsNot "") Then
                    Dim projIme As String = line(0)
                    Dim projMapa As String = line(1)

                    ListViewProj.Items.Add(projIme).SubItems.Add(projMapa)

                End If

            Loop

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

        Else
            ' Izvoz kodiranja v TXT datoteko
            Dim dataNast As String = ""

            dataNast = dataNast + "Virtualna Kartica=\Virtualna Kartica\GSI" + vbCrLf
            dataNast = dataNast + "Test Projekt=\Projekti\Test Projekt" + vbCrLf

            My.Computer.FileSystem.WriteAllText(datProjektiNast, dataNast, False)
        End If
    End Sub

    Public Sub ProjektiShraniNast()
        Dim datProjektiNast = "tps1100_projekti.txt"
        ' Uvoz nastavitev iz datototeke
        If My.Computer.FileSystem.FileExists(datProjektiNast) Then
            Dim dataProj As ListView.ListViewItemCollection = Me.ListViewProj.Items
            Dim item As ListViewItem
            Dim ime As String = ""
            Dim mapa As String = ""
            Dim dataOut As String = ""
            For Each item In dataProj
                ime = item.SubItems(0).Text
                mapa = item.SubItems(1).Text
                dataOut = dataOut + ime + "=" + mapa + vbCrLf
            Next
            My.Computer.FileSystem.WriteAllText(datProjektiNast, dataOut, False)
        End If
    End Sub

    Private Sub ListViewProj_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListViewProj.SelectedIndexChanged
        Dim izbran As ListView.SelectedListViewItemCollection = Me.ListViewProj.SelectedItems
        Dim item As ListViewItem
        Dim ime As String = ""
        Dim mapa As String = ""
        For Each item In izbran
            ime = item.SubItems(0).Text
            mapa = item.SubItems(1).Text
        Next
        LabelIzbProj.Text = ime.ToString
        LabelIzbProjMapa.Text = mapa.ToString
        Dim delovnaMapa = My.Application.Info.DirectoryPath.ToString + "\" + mapa.ToString
        projektIzbranMapa = delovnaMapa
        Dim datotekeJobi = My.Computer.FileSystem.GetFiles(delovnaMapa, FileIO.SearchOption.SearchTopLevelOnly, "*.GSI")
        ListView2.Clear()
        For Each datJob In datotekeJobi
            ListView2.Items.Add(Path.GetFileName(datJob).ToString)
        Next
    End Sub

    Private Sub ButtonDelovni_Click(sender As Object, e As EventArgs) Handles ButtonDelovni.Click, ToolStripButton5.Click
        LabelDeloProj.Text = LabelIzbProj.Text
        LabelDeloMapa.Text = LabelIzbProjMapa.Text
        My.Settings.Projekt = LabelDeloProj.Text
        My.Settings.ProjektMapa = LabelDeloMapa.Text
        My.Settings.Save()
        If LabelDeloProj.Text = "Virtualna Kartica" Then
            Form1.ToolStripLabel2.Text = LabelDeloProj.Text
            Form1.ToolStripLabel2.Image = My.Resources.icons8_micro_sd_50
        Else
            Form1.ToolStripLabel2.Text = LabelDeloProj.Text
            Form1.ToolStripLabel2.Image = My.Resources.icons8_browse_folder_50_2
        End If
    End Sub

    Private Sub ToolStripButton4_Click(sender As Object, e As EventArgs) Handles ToolStripButton4.Click
        DialogNovProj.ShowDialog()

    End Sub

    Private Sub ToolStripButton3_Click(sender As Object, e As EventArgs) Handles ToolStripButton3.Click
        Process.Start(projektIzbranMapa)
    End Sub

    Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click

    End Sub

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

    End Sub
End Class