' SummaryPage2_SecurityStmt_KeyDetails
Option Explicit On
Option Strict On

' #################################################################
' Security Statement Key Details

' Version: 2.06 (10/05/2024)

' Usage: Security Statements form
' #################################################################
' Version        Date  User     Job       Description
' -------  ----------  -------  --------  -------------------------
'    2.05  23/11/2023  ISS      J030836   Added the ability to show "Related via Hashtags" block
'    2.06  10/05/2024  ISS      J033263   Added Alert Message
' #################################################################

'#Const "ShowItemReference",Boolean,False,"Show Reference column in Security Items table."
'#const "ShowRelatedViaHashtagsBlock",Boolean,True,"Show Related via Hashtags block."

' Constants
Private ShowItemReference As Boolean = False
Private ShowRelatedViaHashtagsBlock As Boolean = True

' Objects
Private mSecurityStmt As finSecurityStmt

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

  ' Assume Success
  Main = True

  ' Initialise
  If TypeOf source Is String Then
    mSecurityStmt = finBL.CreateSecurityStmt()
    mSecurityStmt.Load(CStr(source))

    ' Check not filtered out
    If finBL.CurrentUserInformation.IsSecurityStmtFilteredOut(mSecurityStmt.SecurityStmtId) Then
      Throw New ApplicationException(finBL.Error.Message)
    End If
  Else
    mSecurityStmt = DirectCast(source, finSecurityStmt)
  End If

  ' Get Constants
  With ScriptInfo.Constants
    ShowItemReference = .GetBoolean("ShowItemReference", ShowItemReference)
    ShowRelatedViaHashtagsBlock = .GetBoolean("ShowRelatedViaHashtagsBlock", ShowRelatedViaHashtagsBlock)
  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 HeadingStyle As isefinSummaryPageBlockHeadingStyle
  Dim Ok As Boolean
  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim SecurityStmtItem As finSecurityStmtItemBase
  Dim ShowMotorWebMonitorActions As Boolean
  Dim SummaryTable As ISSummaryTable
  Dim Warnings As finWarnings

  ' Assume Success
  Ok = True

  ' Initialise
  sb = finBL.CreateHtmlSummaryPage2StringBuilder()

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

  If mSecurityStmt.IsNew AndAlso mSecurityStmt.AccountApp Is Nothing Then
    ' No Security Statement loaded
    sb.SectionColumnsAddQuickBlock("Summary", "{{info|Please enter a code to view a Security Statement record.}}")
  Else
    ' Security Statement
    ' Begin Section
    sb.SectionColumnsBegin()

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

    ' Summary
    sb.BlockBegin("Summary", "", If(Len(mSecurityStmt.AlertMessage) = 0, isefinSummaryPageBlockHeadingStyle.Main, isefinSummaryPageBlockHeadingStyle.Warning), False)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_Summary(ScriptInfo, mSecurityStmt, Me.ScriptInfo.FormKey <> "SecurityStmts", ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()

    ' PPSR
    If finBL.Settings.AddOns.SecurityRegisterPPSR AndAlso Not mSecurityStmt.IsNew AndAlso mSecurityStmt.SecurityItems.Count <> 0 AndAlso mSecurityStmt.RegisterStatus <> iseSecurityStmtRegisterStatus.Historic Then
      HeadingStyle = isefinSummaryPageBlockHeadingStyle.Normal
      Select Case finBL.CountryCode
        Case "AU"
          For Each SecurityStmtItem In mSecurityStmt.SecurityItems
            With SecurityStmtItem
              If .RegisterAction <> iseSecurityStmtRegisterAction.None  Then
                HeadingStyle = isefinSummaryPageBlockHeadingStyle.WarningLow
                Exit For
              End If
            End With
          Next
        Case Else
          If mSecurityStmt.RegisterAction <> iseSecurityStmtRegisterAction.None Then
            HeadingStyle = isefinSummaryPageBlockHeadingStyle.WarningLow
          End If
      End Select

      sb.BlockBegin("PPSR", "", HeadingStyle)
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_PPSRSummary(ScriptInfo, mSecurityStmt, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()
    End If

    ' Security Monitor
    Select Case ucase(ScriptInfo.FormKey)
      Case "MOTORWEBMONITOR"
        ' Don't display as we are in the monitor wizard
        ShowMotorWebMonitorActions = False
      Case Else
        ShowMotorWebMonitorActions = ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded
    End Select
    If finBL.Settings.AddOns.SecurityEnquiry_MotorWeb AndAlso finBL.Settings.AddOns.SecurityRegisterPPSR Then
      If mSecurityStmt.IsMonitored OrElse mSecurityStmt.HasMotorVehicles Then
        sb.BlockBegin("MotorWeb Security Monitor")
        sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_MotorWebSecurityMonitor(ScriptInfo, mSecurityStmt, ShowMotorWebMonitorActions))
        sb.BlockEnd()
      End If
    End If

    ' Security Items
    If finBL.Settings.SecurityStmtInsuranceBrokerTerminology Then
      sb.BlockBegin("Policies")
    Else
      sb.BlockBegin("Security Items")
    End If
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_SecurityItems(ScriptInfo, mSecurityStmt, ShowItemReference, False, False, False, False, True, 20, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()

    ' Related via Hashtags
    If ShowRelatedViaHashtagsBlock AndAlso finBL.Settings.QuickSearchUseVersion2 AndAlso finBL.Settings.QuickSearch2SupportHashtags Then
      SummaryTable = finBL.Utilities.GetSummaryTableOfHashTagReferences(isefinSearchSourceType.SecurityStmt, mSecurityStmt.Pk, mSecurityStmt.SecurityStmtId, mSecurityStmt.Notes, "Related via Hashtags")

      If SummaryTable IsNot Nothing Then
        sb.BlockBegin("Related via Hashtags")
        sb.SummaryTableAppend(SummaryTable)
        sb.BlockEnd()
      End If
    End If
    
    ' End Column 1
    sb.ColumnEnd()

    ' Begin Column 2
    sb.ColumnBegin("40%", "25em", "50em")

    ' Warnings
    Warnings = mSecurityStmt.GetWarnings()
    sb.BlockBegin("Warnings", "", If(Warnings.Count = 0, isefinSummaryPageBlockHeadingStyle.Normal, isefinSummaryPageBlockHeadingStyle.Warning))
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.WarningsSummary(ScriptInfo, Warnings))
    sb.BlockEnd()

    ' Accounts
    If mSecurityStmt.Accounts.Count <> 0 Then
      sb.BlockBegin("Accounts")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_Accounts(ScriptInfo, mSecurityStmt, 20, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()
    End If

    ' Clients
    If mSecurityStmt.Clients.Count <> 0 Then
      sb.BlockBegin("Clients")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_Clients(ScriptInfo, mSecurityStmt, 20, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()
    End If

    ' End Column 2
    sb.ColumnEnd()

    ' End Section
    sb.SectionColumnsEnd()
  End If

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Ok

End Function