Imports System.IO
Imports System.IO.Compression
Imports System.Windows.Forms


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"
        Dim i As Integer = 0
        Dim iselected As Integer = 0
        ' 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)
                    If projIme = "Virtualna Kartica" Then
                        ListViewProj.Items.Item(i).ImageIndex = 2
                    Else
                        ListViewProj.Items.Item(i).ImageIndex = 1
                    End If

                    If projIme = My.Settings.Projekt Then
                        iselected = i
                    End If

                    i = i + 1
                End If

            Loop

            streamReader.Close()
            streamReader.Dispose()
            My.Settings.Save()
            ListViewProj.Items.Item(iselected).Selected = True
            ListViewProj.Focus()
        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

    Public Sub UvoziMapeProj()
        Dim appPath As String = My.Application.Info.DirectoryPath.ToString
        Dim mapaProjekti As String = "\Projekti"
        Dim pathProjekti As String = appPath + mapaProjekti
        Dim mapeSeznam = My.Computer.FileSystem.GetDirectories(pathProjekti)
        Dim mapa
        Dim i As Integer = 1

        ListViewProj.Clear()
        ListViewProj.Items.Add("Virtualna Kartica", 2).SubItems.Add("Virtualna Kartica\GSI")

        For Each mapa In mapeSeznam
            Dim projIme As String = Path.GetFullPath(mapa).Replace(pathProjekti + "\", "").ToString
            Dim projMapa As String = "Projekti\" + projIme

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

            i = i + 1
        Next

    End Sub

    Private Sub UvoziGsiJob()
        If OpenFileDialogUvoziJob.ShowDialog = DialogResult.OK Then
            Dim delovnaMapa = My.Application.Info.DirectoryPath.ToString + "\" + My.Settings.ProjektMapa.ToString + "\"
            Dim fileUvoz = OpenFileDialogUvoziJob.FileName
            Dim fileIme = Path.GetFileName(OpenFileDialogUvoziJob.FileName)
            Dim fileIzvoz = delovnaMapa + fileIme
            My.Computer.FileSystem.CopyFile(fileUvoz, fileIzvoz, True)
            OpenFileDialogUvoziJob.FileName = fileIzvoz

        End If

        OpenFileDialogUvoziJob.Dispose()
    End Sub

    Private Sub NastaviDelovniProj()
        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 NaloziJob()
        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
        Form1.datJob = TextBoxJobDat.Text
        Form1.NaloziGsiJob()
        Me.Close()
    End Sub

    Private Sub PreberiJobe()
        Dim izbran As ListView.SelectedListViewItemCollection = Me.ListViewProj.SelectedItems
        Dim item As ListViewItem
        Dim ime As String = ""
        Dim mapa As String = ""
        Dim ix As Integer = 0

        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
        projektIzbran = ime.ToString
        projektIzbranMapa = delovnaMapa
        Dim datotekeJobi = My.Computer.FileSystem.GetFiles(delovnaMapa, FileIO.SearchOption.SearchTopLevelOnly, "*.GSI")
        ListView2.Clear()
        LabelJobIme.Text = ""
        TextBoxJobDat.Text = ""
        For Each datJob In datotekeJobi
            ListView2.Items.Add(Path.GetFileName(datJob).ToString, 0).SubItems.Add(Path.GetFullPath(datJob))
            ix = ix + 1
        Next

        If ime = "Virtualna Kartica" Then
            ToolStripButton1.Enabled = False
            ToolStripButton2.Enabled = False
            ToolStripButton6.Enabled = False
            ToolStripButton7.Enabled = False
        Else
            ToolStripButton1.Enabled = True
            ToolStripButton2.Enabled = True
            ToolStripButton6.Enabled = False
            ToolStripButton7.Enabled = False
        End If
    End Sub

    Public Sub ArhivirajProjekt()
        ' Arhiviraj izbran jprojekt
        Dim arhivCas = System.DateTime.Now.ToString("yyyyMMdd-HHmm")
        Dim mapaZav = My.Application.Info.DirectoryPath.ToString + "\Zavarovanja\"
        Dim arhivZav = mapaZav + "Projekt_" + projektIzbran + "_" + arhivCas + ".zip"
        Dim msgArhivProj = MsgBox("Arhiviram projekt '" + projektIzbran + "'", vbYesNo + vbExclamation, "Arhiviranje Projekta")

        If msgArhivProj = DialogResult.Yes Then
            ZipFile.CreateFromDirectory(projektIzbranMapa, arhivZav, CompressionLevel.Optimal, False)
            MessageBox.Show("Kartica zavarovana v: " + vbCrLf + arhivZav.ToString)
        End If

    End Sub

    Public Sub OdstraniProjekt()
        ' Izbriši izbran projekt
        Dim msgDelProj = MsgBox("Odstranim projekt [" + projektIzbran + "]?", vbYesNo + vbCritical, "Brisanje Projekta")
        If msgDelProj = DialogResult.Yes Then
            My.Computer.FileSystem.DeleteDirectory(projektIzbranMapa, FileIO.DeleteDirectoryOption.DeleteAllContents, FileIO.RecycleOption.SendToRecycleBin)
            UvoziMapeProj()
            ProjektiShraniNast()
        End If
    End Sub

    Private Sub ListViewProj_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListViewProj.SelectedIndexChanged
        PreberiJobe()
    End Sub

    Private Sub ListViewProj_DoubleClick(sender As Object, e As EventArgs) Handles ListViewProj.DoubleClick
        NastaviDelovniProj()
        Me.Close()
    End Sub

    Private Sub ButtonDelovni_Click(sender As Object, e As EventArgs) Handles ButtonDelovni.Click, ToolStripButton5.Click
        NastaviDelovniProj()
    End Sub

    Private Sub ListView2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView2.SelectedIndexChanged
        Dim izbran As ListView.SelectedListViewItemCollection = Me.ListView2.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
        LabelJobIme.Text = ime.ToString
        TextBoxJobDat.Text = mapa.ToString
        If LabelJobIme.Text = "" Then
            ToolStripButton6.Enabled = False
            ToolStripButton7.Enabled = False
        Else
            ToolStripButton6.Enabled = True
            ToolStripButton7.Enabled = True
        End If

        'Dim delovnaJob = My.Application.Info.DirectoryPath.ToString + "\" + mapa.ToString
        'projektIzbran = ime.ToString
        'projektIzbranMapa = delovnaMapa
        'Dim datotekeJobi = My.Computer.FileSystem.GetFiles(delovnaMapa, FileIO.SearchOption.SearchTopLevelOnly, "*.GSI")
    End Sub

    Private Sub ListView2_DoubleClick(sender As Object, e As EventArgs) Handles ListView2.DoubleClick
        NaloziJob()

    End Sub

    Private Sub TSButtonUvozi_Click(sender As Object, e As EventArgs)
        UvoziGsiJob()

    End Sub

    Private Sub ToolStripButton7_Click(sender As Object, e As EventArgs) Handles ToolStripButton7.Click
        ' Arhiviraj izbran job
        Dim datIzb = TextBoxJobDat.Text
        Dim datArh = datIzb.Replace(LabelJobIme.Text, "_ARHIV\" + LabelJobIme.Text)

        Dim msgArh = MsgBox("Arhiviram job: " + LabelJobIme.Text, vbOKCancel + vbExclamation, "Arhiviranje Job-a...")
        If msgArh = DialogResult.OK Then
            If My.Computer.FileSystem.FileExists(datIzb) Then
                My.Computer.FileSystem.MoveFile(datIzb, datArh, True)
            End If
            ' Ponovno osveži vsebino seznama
            PreberiJobe()
        End If
    End Sub

    Private Sub ToolStripButton6_Click(sender As Object, e As EventArgs) Handles ToolStripButton6.Click
        ' Izbriši izbran job
        Dim datIzb = TextBoxJobDat.Text

        Dim msgArh = MsgBox("Izbrišem job: " + LabelJobIme.Text, vbOKCancel + vbCritical, "Brisanje Job-a...")
        If msgArh = DialogResult.OK Then
            If My.Computer.FileSystem.FileExists(datIzb) Then
                My.Computer.FileSystem.DeleteFile(datIzb, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin)
            End If
            ' Ponovno osveži vsebino seznama
            PreberiJobe()
        End If
    End Sub

    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        ' Arhiviraj izbran jprojekt
        ArhivirajProjekt()

    End Sub

    Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
        ' Izbriši izbran projekt
        OdstraniProjekt()
    End Sub

    Private Sub NovProjektToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NovProjektToolStripMenuItem.Click
        DialogNovProj.ShowDialog()
    End Sub

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

    Private Sub UvoziProjekteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UvoziProjekteToolStripMenuItem.Click
        UvoziMapeProj()
        Dim msgUvoziMape = MsgBox("Shranim seznam projektov?" + vbCrLf + "Datoteka: tps1100_projekti.txt", vbOKCancel + vbExclamation, "Shranim Projekte?")

        If msgUvoziMape = DialogResult.OK Then
            ProjektiShraniNast()
        End If

    End Sub

    Private Sub ArhivirajProjektToolStripMenuItem_Click(sender As Object, e As EventArgs)
        ArhivirajProjekt()
    End Sub

    Private Sub NastaviDelovniProjektToolStripMenuItem_Click(sender As Object, e As EventArgs)
        NastaviDelovniProj()
    End Sub

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

    Private Sub ToolStripButton4_Click(sender As Object, e As EventArgs) Handles ToolStripButton4.Click
        UvoziGsiJob()
    End Sub

    Private Sub ToolStripButton9_Click(sender As Object, e As EventArgs) Handles ToolStripButton9.Click
        NastaviDelovniProj()
    End Sub
End Class