' SummaryPage2_SecurityStmtSecurityItems_SecurityItemSummary
Option Explicit On
Option Strict On

' #################################################################
' Security Statement Item Summary
' 
' Version: 2.09 (24/05/2024)
' 
' Usage: Security Statements form, Security Items page
' #################################################################
' Version        Date  User     Job       Description
' -------  ----------  -------  --------  -------------------------
'    2.07  06/07/2021  ISS
'    2.08  31/01/2023  ISS      J028790   Added Monitoring Block
'    2.09  24/05/2024  ISS      J033492   Added Files Block
'          24/05/2024  ISS      J033546   Added Logs Block
' #################################################################

'#const "LogsBlockMaxDisplayRows",Integer,20,"The maximum number of Logs to display"
'#const "ShowFilesBlock",Boolean,True,"Show Security Item Files?"
'#const "ShowInsuranceBlock",Boolean,True,"Show Insurance details?"
'#const "ShowItemFullDescription",Boolean,True,"Show full description of Security Item?"
'#const "ShowLogsBlock",Boolean,True,"Show Security Item Logs?"
'#const "ShowOwnerLocationBlock",Boolean,True,"Show Owner and Location details?"
'#const "ShowMonitorBlock",Boolean,True,"Show Monitoring details?"
'#const "ShowPPSRBlock",Boolean,True,"Show PPSR details?"
'#const "ShowUserDefined",Boolean,False,"Show User Defined fields for the Security Statement Item"
'#const "ShowUserDefinedOnLeft",Boolean,False,"Indicates whether to show the user Define fields in the left column (the default is the righthand column)"
'#const "ShowValuationBlock",Boolean,True,"Show Valuation details?"
'#const "ShowValuationLandBlock",Boolean,True,"Always show Valuation details for Land Items irrespective of the ShowValuationBlock setting?"


' Constants
Private LogsBlockMaxDisplayRows As Integer = 20
Private ShowFilesBlock As Boolean = True
Private ShowInsuranceBlock As Boolean = True
Private ShowItemFullDescription As Boolean = True
Private ShowLogsBlock As Boolean = True
Private ShowMonitorBlock As Boolean = True
Private ShowOwnerLocationBlock As Boolean = True
Private ShowPPSRBlock As Boolean = True
Private ShowUserDefined As Boolean = True
Private ShowUserDefinedOnLeft As Boolean = False
Private ShowValuationBlock As Boolean = True
Private ShowValuationLandBlock As Boolean = True

' Objects
Private mSecurityStmt As finSecurityStmt
Private mSecurityStmtItem As finSecurityStmtItemBase

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
  mSecurityStmt = DirectCast(source, finSecurityStmt)
  Index = eventArgs.GetInteger("Index")
  If Index >= 0 AndAlso Index < mSecurityStmt.SecurityItems.Count Then
    mSecurityStmtItem = mSecurityStmt.SecurityItems(Index)
  End If

  ' Get Constants
  With ScriptInfo.Constants
    LogsBlockMaxDisplayRows = .GetInteger("LogsBlockMaxDisplayRows", LogsBlockMaxDisplayRows)
    ShowFilesBlock = .GetBoolean("ShowFilesBlock", ShowFilesBlock)
    ShowInsuranceBlock = .GetBoolean("ShowInsuranceBlock", ShowInsuranceBlock)
    ShowItemFullDescription = .GetBoolean("ShowItemFullDescription", ShowItemFullDescription)
    ShowLogsBlock = .GetBoolean("ShowLogsBlock", ShowLogsBlock)
    ShowMonitorBlock = .GetBoolean("ShowMonitorBlock", ShowMonitorBlock)
    ShowOwnerLocationBlock = .GetBoolean("ShowOwnerLocationBlock", ShowOwnerLocationBlock)
    ShowPPSRBlock = .GetBoolean("ShowPPSRBlock", ShowPPSRBlock)
    ShowUserDefined = .GetBoolean("ShowUserDefined", ShowUserDefined)
    ShowUserDefinedOnLeft = .GetBoolean("ShowUserDefinedOnLeft", ShowUserDefinedOnLeft)
    ShowValuationBlock = .GetBoolean("ShowValuationBlock", ShowValuationBlock)
    ShowValuationLandBlock = .GetBoolean("ShowValuationLandBlock", ShowValuationLandBlock)
  End With

  ' Overrides ShowUserDefined if there is no user defined data
  If ShowUserDefined Then
    ShowUserDefined = mSecurityStmtItem IsNot Nothing AndAlso mSecurityStmtItem.SecurityType IsNot Nothing AndAlso mSecurityStmtItem.SecurityType.UserDefinedParameterSetPk <> 0
  End If

  ' 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 IncludeValuation As Boolean
  Dim Ok As Boolean
  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim SummaryTables As ISSummaryTables
  Dim Warnings As finWarnings

  ' Assume Success
  Ok = True

  ' Initialise
  sb = finBL.CreateHtmlSummaryPage2StringBuilder()
  SummaryTables = finBL.CreateSummaryTables()

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

  If mSecurityStmtItem IsNot Nothing Then
    ' Begin Section
    sb.SectionColumnsBegin()

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

    ' Summary (Include Valuation if not showing the Valuation Block)
    If ShowValuationBlock Then
      IncludeValuation = False
    ElseIf ShowValuationLandBlock AndAlso mSecurityStmtItem.BaseType = isefinSecurityBaseType.Land Then
      IncludeValuation = False
    Else
      IncludeValuation = True
    End If
    sb.BlockBegin("Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_SecurityItemSummary(ScriptInfo, mSecurityStmtItem, IncludeValuation, ShowItemFullDescription, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
    sb.BlockEnd()
    
    ' Valuation
    If ShowValuationBlock OrElse (mSecurityStmtItem.BaseType = isefinSecurityBaseType.Land AndAlso ShowValuationLandBlock) Then
      If mSecurityStmtItem.BaseType = isefinSecurityBaseType.Insurance OrElse mSecurityStmtItem.BaseType = isefinSecurityBaseType.Investment Then
        ' Not Applicable for insurance
        ' Included in investment details page
      Else
        sb.BlockBegin("Valuation")
        sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_SecurityItemValuation(ScriptInfo, mSecurityStmtItem, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
        sb.BlockEnd()
      End If
    End If
    
    ' Files
    If ShowFilesBlock Then
      ' Get the Summary Tables
      SummaryTables = finBL.SummaryPageStandardBlocks.SecurityStmt_SecurityItemFiles(ScriptInfo, mSecurityStmtItem)
      
      ' Check that the Summary Table has data
      If SummaryTables.ItemByIndex(0).HasData Then
        sb.BlockBegin("Files")
        sb.SummaryTablesAppend(SummaryTables)
        sb.BlockEnd()
      End If
    End If

    ' Logs
    If ShowLogsBlock Then
      ' Get the Summary Tables
      SummaryTables = finBL.SummaryPageStandardBlocks.SecurityStmt_SecurityItemLogs(ScriptInfo, mSecurityStmtItem, True, LogsBlockMaxDisplayRows, Nothing)
      
      ' Check that the Summary Table has data
      If SummaryTables.ItemByIndex(0).HasData Then
        sb.BlockBegin("Logs")
        sb.SummaryTablesAppend(SummaryTables)
        sb.BlockEnd()
      End If
    End If

    ' User Defined (left-hand side)
    If ShowUserDefined AndAlso ShowUserDefinedOnLeft Then
      sb.BlockBegin("User Defined")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_SecurityItemUserDefined(ScriptInfo, mSecurityStmtItem, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()
    End If

    ' End Column 1
    sb.ColumnEnd()

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

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

    ' PPSR B2G
    If ShowPPSRBlock AndAlso finBL.CountryCode = "AU" AndAlso mSecurityStmtItem.IsSecuredItem Then
      If mSecurityStmtItem.RegisterAction = iseSecurityStmtRegisterAction.None Then
        HeadingStyle = isefinSummaryPageBlockHeadingStyle.Normal
      Else
        HeadingStyle = isefinSummaryPageBlockHeadingStyle.WarningLow
      End If

      sb.BlockBegin("PPSR", "", HeadingStyle)
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_PPSRItemSummaryAU(ScriptInfo, mSecurityStmtItem))
      sb.BlockEnd()
    End If
    
    ' Monitoring
    If ShowMonitorBlock AndAlso (mSecurityStmtItem.MonitorType <> isefinSecurityStmtMonitorType.None OrElse  mSecurityStmtItem.CanHoldMonitoring() OrElse mSecurityStmtItem.CanStartMonitoring(iseSecurityEnquiryService.InfoAgent) OrElse mSecurityStmtItem.CanStopMonitoring(iseSecurityEnquiryService.InfoAgent)) Then
      sb.BlockBegin("Monitoring")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_SecurityItemMonitoring(ScriptInfo, mSecurityStmtItem, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()
    End If
    
    ' Security Enquiries
    If finBL.Settings.AddOns.SecurityEnquiry Then
      Select Case mSecurityStmtItem.SecurityType.BaseType
        Case isefinSecurityBaseType.MotorVehicle
          sb.BlockBegin("Enquiries")
          sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_SecurityItemEnquiries(ScriptInfo, mSecurityStmtItem, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
          sb.BlockEnd()
      End Select
    End If

    ' Insurance
    If ShowInsuranceBlock Then
      Select Case mSecurityStmtItem.BaseType
      Case isefinSecurityBaseType.Insurance, isefinSecurityBaseType.Investment
        ' Do not include Insurance Block
      Case Else
        sb.BlockBegin("Insurance")
        sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_SecurityItemInsurance(ScriptInfo, mSecurityStmtItem, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
        sb.BlockEnd()
      End Select
    End If

    ' Owner and Location
    If ShowOwnerLocationBlock Then
      If mSecurityStmtItem.BaseType = isefinSecurityBaseType.Land Then
        sb.BlockBegin("Owner")
      Else
        sb.BlockBegin("Owner and Location")
      End If
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_SecurityItemOwnerAndLocation(ScriptInfo, mSecurityStmtItem, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()
    End If

    ' User Defined (right-hand side)
    If ShowUserDefined AndAlso Not ShowUserDefinedOnLeft Then
      sb.BlockBegin("User Defined")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_SecurityItemUserDefined(ScriptInfo, mSecurityStmtItem, 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