' SummaryPage2_Logs_LogSummary
Option Explicit On
Option Strict On

' #################################################################
' Log Summary

' Version: 2.03 (26/04/2016)

' Usage: Below Logs grids and in the Task Manager
' #################################################################

' Constants

' Objects
Private mLog As finLogRO
Private mLogs As finLogs

' Other
Private mThreaded As Boolean

Public Function Main(source As Object,
                     eventId As String,
                     eventArgs As ISKeyValueList,
                     ByRef handled As Boolean,
                     ByRef returnValues As ISKeyValueList,
                     ByRef text As String) As Boolean

  Dim Index As Integer

  ' Assume Success
  Main = True

  ' Initialise
  mLogs = DirectCast(source, finLogs)
  Index = eventArgs.GetInteger("Index")
  If Index >= 0 AndAlso Index < mLogs.Count Then
    mLog = mLogs(Index)
  End If
  mThreaded = eventArgs.GetBoolean("Threaded")

  ' Get Constants
  With ScriptInfo.Constants
  End With

  ' Handle Events
  Select Case eventId
    Case ""
      ' Main event, i.e., return main page content
      Main = CreatePage(text)
  End Select

End Function

Private Function CreatePage(ByRef html As String) As Boolean

  Dim Collapsed As Boolean
  Dim HeadingStyle As isefinSummaryPageBlockHeadingStyle
  Dim Icon As String
  Dim Log As finLogRO
  Dim LogsToShow As List(Of finLogRO)
  Dim Ok As Boolean
  Dim ParentLogPk As Integer
  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim Title As String
  Dim TitleSuffix As String

  ' Assume Success
  Ok = True

  ' Initialise
  LogsToShow = New List(Of finLogRO)
  If mThreaded Then ParentLogPk = mLog.ParentLogPk
  If ParentLogPk = 0 Then ParentLogPk = mLog.Pk
  sb = finBL.CreateHtmlSummaryPage2StringBuilder()

  ' Add Parent Log First (should always be first but do it this way just in case)
  For Each Log In mLogs
    If Log.Pk = ParentLogPk AndAlso Log.LogClass = mLog.LogClass Then
      LogsToShow.Add(Log)
      Exit For
    End If
  Next

  ' Add Child Logs
  If mThreaded Then
    For Each Log In mLogs
      If Log.ParentLogPk = ParentLogPk AndAlso Log.LogClass = mLog.LogClass Then
        LogsToShow.Add(Log)
      End If
    Next
  End If

  ' Initialise HTML
  With sb
    .Initialise(ScriptInfo)
    .HtmlHeaderBegin("", isefinSummaryPageStyle.Blocks)
    .HtmlHeaderEnd()
  End With

  ' Begin Section
  sb.SectionColumnsBegin()

  ' Begin Column 1
  sb.ColumnBegin("100%")

  For Each Log In LogsToShow
    ' Main Log being summarised should be expanded and styles to look different
    If Log Is mLog Then
      Collapsed = False
      HeadingStyle = isefinSummaryPageBlockHeadingStyle.Main
    Else
      Collapsed = True
      HeadingStyle = isefinSummaryPageBlockHeadingStyle.Normal
    End If

    ' Icon
    If Log.LogSeverityPk = 0 Then
      Icon = "{{colourblock|transparent||14|14}} "
    Else
      If Log.IsIncomplete Then
        Icon = finBL.LogSeverities.ItemByPk(Log.LogSeverityPk).OpenColourBackground
      Else
        Icon = finBL.LogSeverities.ItemByPk(Log.LogSeverityPk).CompleteColourBackground
      End If
      If Len(Icon) = 0 Then Icon = "transparent"
      Icon = "{{colourblock|" & Icon & "|Log Severity: " & Log.LogSeverityId & "|14|14}} "
    End If

    ' Title
    Title = Log.Subject
    If finBL.Settings.TimeZoneUse OrElse (finBL.IsRunningFromWeb AndAlso finBL.Settings.SummaryPagesAlwaysShowTimeZoneInformationFromWeb) Then
      ' Log Date including Time Zone information
      TitleSuffix = String.Format(" - {0} {1}", finBL.FormatDateTimeLong(Log.LogTzDate, False, True), Log.LogTimeZoneDescription)
    Else
      ' Log Date
      TitleSuffix = String.Format(" ({0})", finBL.FormatDateTimeLong(Log.LogDate, False, True))
    End If

    ' Summary
    sb.BlockBegin(Icon & Title, TitleSuffix, HeadingStyle, True, Collapsed)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Log_Summary(ScriptInfo, Log, True, True, False, True))
    sb.BlockEnd()
  Next

  ' End Column 1
  sb.ColumnEnd()

  ' End Section
  sb.SectionColumnsEnd()

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Ok

End Function