' SummaryPage2_AccountSecurity_SecuritySummary
Option Explicit On
Option Strict On

' #################################################################
' Account Security Summary

' Version: 2.03 (06/05/2016)

' Usage: Accounts form, Security page
' #################################################################

'#const "ShowInsuranceBlock",Boolean,True,"Show Insurance details?"
'#const "ShowItemFullDescription",Boolean,True,"Show full description of Security Item?"
'#const "ShowOwnerLocationBlock",Boolean,True,"Show Owner and Location 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 ShowInsuranceBlock As Boolean = True
Private ShowItemFullDescription 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

' Other
Private mShowSecurityStmtSummary 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 Account As finAccount
Dim Index As Integer

  ' Assume Success
  Main = True

  ' Initialise
  If TypeOf source Is String Then
    Account = finBL.CreateAccount()
    Account.Load(CStr(source))

    ' Check not filtered out
  Else
    Account = DirectCast(source, finAccount)
  End If
  mSecurityStmt = Account.SecurityStmt
  Index = eventArgs.GetInteger("Index")
  If Index >= 0 AndAlso Index < mSecurityStmt.SecurityItems.Count Then
    mSecurityStmtItem = mSecurityStmt.SecurityItems(Index)
  End If
  mShowSecurityStmtSummary = eventArgs.GetBoolean("ShowSecurityStmtSummary", False)

  ' Get Constants
  With ScriptInfo.Constants
    ShowInsuranceBlock = .GetBoolean("ShowInsuranceBlock", ShowInsuranceBlock)
    ShowItemFullDescription = .GetBoolean("ShowItemFullDescription", ShowItemFullDescription)
    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 = finBL.Settings.HasSecurityStmtItemUser(True, mSecurityStmtItem.BaseType)
  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 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()

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

  ' Begin Section
  sb.SectionColumnsBegin()

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

  ' Security Statement Summary (including PPSR)
  If mShowSecurityStmtSummary Then
    sb.BlockBegin("Security Statement Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)

    ' Summary
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_Summary(ScriptInfo, mSecurityStmt, Not mSecurityStmt.IsNew, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))

    ' PPSR
    If ShowPPSRBlock AndAlso finBL.Settings.AddOns.SecurityRegisterPPSR AndAlso Not mSecurityStmt.IsNew AndAlso mSecurityStmt.SecurityItems.Count <> 0 Then
      Select Case finBL.CountryCode
        Case "AU"
          ' PPSR is item-based
        Case "NZ"
          SummaryTables = finBL.SummaryPageStandardBlocks.SecurityStmt_PPSRSummary(ScriptInfo, mSecurityStmt, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded)
          If SummaryTables.Exists("Main") Then SummaryTables("Main").Caption = "PPSR"
          sb.SummaryTablesAppend(SummaryTables)
      End Select
    End If

    sb.BlockEnd()
  End If

  ' Security Item Summary
  If mSecurityStmtItem IsNot Nothing Then
    ' 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()
  End If

  ' Valuation
  If mSecurityStmtItem IsNot Nothing AndAlso (ShowValuationBlock OrElse (mSecurityStmtItem.BaseType = isefinSecurityBaseType.Land AndAlso ShowValuationLandBlock)) Then
    If mSecurityStmtItem.BaseType = isefinSecurityBaseType.Insurance Then
      ' Not Applicable
    Else
      sb.BlockBegin("Valuation")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_SecurityItemValuation(ScriptInfo, mSecurityStmtItem, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()
    End If
  End If

  ' User Defined (left-hand side)
  If mSecurityStmtItem IsNot Nothing AndAlso 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 (Security Statement)
  If mShowSecurityStmtSummary Then
    Warnings = mSecurityStmt.GetWarnings()
    sb.BlockBegin("Security Statement Warnings", "", If(Warnings.Count = 0, isefinSummaryPageBlockHeadingStyle.Normal, isefinSummaryPageBlockHeadingStyle.Warning))
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.WarningsSummary(ScriptInfo, Warnings))
    sb.BlockEnd()
  End If

  ' Warnings (Security Item)
  If mSecurityStmtItem IsNot Nothing Then
    Warnings = mSecurityStmtItem.GetWarnings()
    sb.BlockBegin("Warnings", "", If(Warnings.Count = 0, isefinSummaryPageBlockHeadingStyle.Normal, isefinSummaryPageBlockHeadingStyle.Warning))
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.WarningsSummary(ScriptInfo, Warnings))
    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 mSecurityStmtItem IsNot Nothing AndAlso ShowInsuranceBlock Then
    If mSecurityStmtItem.BaseType = isefinSecurityBaseType.Insurance Then
      ' Not Applicable
    Else
      sb.BlockBegin("Insurance")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_SecurityItemInsurance(ScriptInfo, mSecurityStmtItem, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
      sb.BlockEnd()
    End If
  End If

  ' Owner and Location
  If mSecurityStmtItem IsNot Nothing AndAlso 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 mSecurityStmtItem IsNot Nothing AndAlso 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()

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Ok

End Function