Public Class InovarProfessorASPX
    Inherits Gizmox.WebGUI.Forms.Hosts.AspPageBase



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim url = Utils.GetBaseURL()

        Me.BASEURL = url

        'preciso disto para os caminhos dos js, csss, etc
        If Not url.EndsWith("/") Then
            url = url & "/"
        End If

        Me.FOLDERURL = url & "InovarProfessor/"

        UpdateLinks_Scripts_CSS()


    End Sub


    Private Sub UpdateLinks_Scripts_CSS()
        'para não estar sempre a fzer isto...
        If Not String.IsNullOrWhiteSpace(container_SCRIPTS.Text) Then
            Exit Sub
        End If

        Dim scripts = New List(Of System.IO.FileInfo)
        Dim styleSheets = New List(Of System.IO.FileInfo)

        Dim path = Server.MapPath(".").ToLower()

        If path.Contains("route") Then
            path = Split(path, "route")(0)
        End If

        path &= "\InovarProfessor"

        If Not System.IO.Directory.Exists(path) Then Exit Sub

        Dim directoryInfo = New System.IO.DirectoryInfo(path)

        'ficheiros que acabem em .bundle. mais qq coisa, para carregar os .bundle.js e .bundle.css
        ' Dim files = directoryInfo.GetFiles("*.bundle.", IO.SearchOption.TopDirectoryOnly)

        Dim files = directoryInfo.GetFiles("*", IO.SearchOption.TopDirectoryOnly)

        'do mais recente para o mais antigo para apenas ficar com os mais recentes se existirem ficheiros do mesmo tipo (main.123.js e main.456.js)
        For Each f In files.OrderByDescending(Function(x) x.CreationTimeUtc)

            Dim fileInfo = f
            Dim fileName = fileInfo.Name

            If fileName.EndsWith(".js") Then

                AddFileIfNotExists(scripts, fileInfo)

            ElseIf fileName.EndsWith(".css") Then

                AddFileIfNotExists(styleSheets, fileInfo)
            End If
        Next

        'Dim scriptPattern = "<script type=""text/javascript"" src=""{0}""></script>"

        'Dim scriptsStr = String.Join(Environment.NewLine, scripts.Select(Function(x) String.Format(scriptPattern, FOLDERURL & x.Name)))
        'container_SCRIPTS.Text = scriptsStr
        container_SCRIPTS.Text = BuildScriptsString(scripts)

        Dim cssPattern = "<link href=""{0}"" rel=""stylesheet"" />"

        Dim cssStr = String.Join(Environment.NewLine, styleSheets.Select(Function(x) String.Format(cssPattern, FOLDERURL & x.Name)))
        container_CSS.Text = cssStr

    End Sub

    Private Function BuildScriptsString(files As List(Of System.IO.FileInfo)) As String

        Dim scriptPattern = "<script type=""text/javascript"" src=""{0}""></script>"

        Dim inline = "", polyfills = "", main = "", scripts = ""

        For Each f In files

            If f.Name.StartsWith("inline") Then
                inline = scriptPattern.FormatWith(FOLDERURL & f.Name)
            ElseIf f.Name.StartsWith("polyfills") Then
                polyfills = scriptPattern.FormatWith(FOLDERURL & f.Name)
            ElseIf f.Name.StartsWith("scripts") Then
                scripts = scriptPattern.FormatWith(FOLDERURL & f.Name)
            ElseIf f.Name.StartsWith("main") Then
                main = scriptPattern.FormatWith(FOLDERURL & f.Name)
            End If

        Next

        Return String.Join(Environment.NewLine, {inline, polyfills, scripts, main})
    End Function


    Private Function GetDistinctNewerFiles(files As List(Of System.IO.FileInfo)) As List(Of System.IO.FileInfo)

        Dim result As New List(Of System.IO.FileInfo)

        For Each f In files.OrderByDescending(Function(x) x.CreationTimeUtc)

            Dim fileInfo = f
            Dim fileName = fileInfo.Name

            If fileName.EndsWith(".js") OrElse fileName.EndsWith(".css") Then

                AddFileIfNotExists(result, fileInfo)
            End If
        Next

        Return result
    End Function



    Private Sub AddFileIfNotExists(ByRef source As List(Of System.IO.FileInfo), file As System.IO.FileInfo)
        ' para tentar ignorar ficheiros do mesmo tipo que não tenham sido apagados
        If Not source.Any(Function(x) x.Name.Contains(file.Name.Substring(0, 4))) Then
            source.Add(file)
        End If
    End Sub

    Private Sub AddIfNotExists(ByRef source As List(Of String), fileName As String)
        ' para tentar ignorar ficheiros do mesmo tipo que não tenham sido apagados
        If Not source.Any(Function(x) x.Contains(fileName.Substring(0, 4))) Then
            source.Add(fileName)
        End If
    End Sub

    Private Function CheckIfFileExistsByStartName(source As List(Of String), fileName As String)
        ' para tentar ignorar ficheiros do mesmo tipo que não tenham sido apagados
        Return source.Any(Function(x) x.Contains(fileName.Substring(0, 4)))
    End Function


    Protected Property BASEURL As String = ""
    Protected Property FOLDERURL As String = ""

End Class