' SummaryPage2_AccountAppCollateralItem_Summary
Option Explicit On
Option Strict On

' #################################################################
' Account Application Collateral/ Security Item Details

' Version: 1.04 (03/11/2020)

' Usage: Account Applications form, Collateral page
' #################################################################

'#Const "ShowLogs",Boolean,True,"Show Logs relating to this Collateral Item."
'#Const "ShowSecurityWarnings",Boolean,True,"Show Security Warnings block."
'#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)"

' Constants
Private ShowLogs As Boolean = True
Private ShowSecurityWarnings As Boolean = True
Private ShowUserDefined As Boolean = True
Private ShowUserDefinedOnLeft As Boolean = False

' Parameters
Private mIncludeShowTempObjectButton As Boolean = True

' Objects
Private mAccountAppCollateralItem As finAccountAppCollateralItem

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
  mAccountAppCollateralItem = DirectCast(source, finAccountAppCollateralItem)

  ' Get Parameters
  mIncludeShowTempObjectButton = eventArgs.GetBoolean("IncludeShowTempObjectButton", mIncludeShowTempObjectButton)

  ' Get Constants
  With ScriptInfo.Constants
    ShowSecurityWarnings = .GetBoolean("ShowSecurityWarnings", ShowSecurityWarnings)
    ShowLogs = .GetBoolean("ShowLogs", ShowLogs)
    ShowUserDefined = .GetBoolean("ShowUserDefined", ShowUserDefined)
    ShowUserDefinedOnLeft = .GetBoolean("ShowUserDefinedOnLeft", ShowUserDefinedOnLeft)
  End With

  ' Override User Defined
  If ShowUserDefined Then
    If mAccountAppCollateralItem.SecurityTypePk = 0 Then
      ShowUserDefined = finBL.Settings.HasSecurityStmtItemUser(False)
    Else 
      ShowUserDefined = finBL.Settings.HasSecurityStmtItemUser(True, mAccountAppCollateralItem.SecurityBaseType)
    End If
  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 Account As finAccount
  Dim CreateAccountErrorMessage As String
  Dim Ok As Boolean
  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim SecurityStmtItem As finSecurityStmtItemBase
  Dim SecurityStmtItemForCollateralItem As finSecurityStmtItemBase
  Dim Warnings As finWarnings

  ' Assume Success
  Ok = True

  ' Create Account which will include this Collateral Item on its Security Statement unless it is flagged as not being used
  If mAccountAppCollateralItem.AccountApp.CreateAccount(Account) Then
    ' Locate Security Item
    For Each SecurityStmtItem In Account.SecurityStmt.SecurityItems
      If SecurityStmtItem.AccountAppCollateralItem Is mAccountAppCollateralItem Then
        SecurityStmtItemForCollateralItem = SecurityStmtItem
        Exit For
      End If
    Next
  Else
    Account = Nothing
    CreateAccountErrorMessage = finBL.Error.Message(True)
  End If

  ' 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%")

  ' Collateral Item Summary
  sb.BlockBegin("Collateral Item Summary", "", isefinSummaryPageBlockHeadingStyle.Main)
  sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountApp_CollateralItemSummary(ScriptInfo, mAccountAppCollateralItem, mIncludeShowTempObjectButton))
  If mAccountAppCollateralItem.Used AndAlso SecurityStmtItemForCollateralItem Is Nothing AndAlso mAccountAppCollateralItem.SecurityTypePk = 0 Then
    sb.Append("{{warning|No Security Type specified so this Collateral Item will not be included on the Security Statement.}}")
  End If
  sb.BlockEnd()

  ' Security Statement Item Summary
  If SecurityStmtItemForCollateralItem IsNot Nothing Then
    sb.BlockBegin("Security Item Summary", "(New Security Item updated from Collateral details)")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_SecurityItemSummary(ScriptInfo, SecurityStmtItemForCollateralItem, True, True, False))
    sb.BlockEnd()
  End If

  ' Valuation
  If SecurityStmtItemForCollateralItem IsNot Nothing Then
    If SecurityStmtItemForCollateralItem.BaseType = isefinSecurityBaseType.Insurance Then
      ' Not Applicable
    Else
      sb.BlockBegin("Valuation")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_SecurityItemValuation(ScriptInfo, SecurityStmtItemForCollateralItem, False))
      sb.BlockEnd()
    End If
  End If

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

  ' End Column 1
  sb.ColumnEnd()

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

  ' Warnings (not for closed Account Applications though since they will probably make little sense, e.g., expirted WOF)
  If ShowSecurityWarnings AndAlso SecurityStmtItem IsNot Nothing AndAlso Not mAccountAppCollateralItem.AccountApp.IsClosed Then
    Warnings = SecurityStmtItem.GetWarnings()
    If Warnings.Count <> 0 Then
      sb.BlockBegin("Warnings", "", If(Warnings.Count = 0, isefinSummaryPageBlockHeadingStyle.Normal, isefinSummaryPageBlockHeadingStyle.Warning))
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.WarningsSummary(ScriptInfo, Warnings))
      sb.BlockEnd()
    End If
  End If

  ' Security Enquiries
  If finBL.Settings.AddOns.SecurityEnquiry Then
    Select Case mAccountAppCollateralItem.SecurityBaseType
      Case isefinSecurityBaseType.MotorVehicle
        sb.BlockBegin("Enquiries")
        sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountApp_CollateralItemEnquiries(ScriptInfo, mAccountAppCollateralItem, ScriptInfo.FormRecordMode = iseFormRecordMode.Loaded))
        sb.BlockEnd()
    End Select
  End If

  ' Insurance
  If SecurityStmtItemForCollateralItem IsNot Nothing Then
    Select Case SecurityStmtItemForCollateralItem.BaseType
      Case isefinSecurityBaseType.Insurance, isefinSecurityBaseType.Investment
        ' Not Applicable

      Case Else
        sb.BlockBegin("Insurance")
        sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.SecurityStmt_SecurityItemInsurance(ScriptInfo, SecurityStmtItemForCollateralItem, False))
        sb.BlockEnd()
    End Select
  End If

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

  ' Logs
  If ShowLogs Then
    sb.BlockBegin("Logs")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountApp_CollateralItemLogs(ScriptInfo, mAccountAppCollateralItem))
    sb.BlockEnd()
  End If

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