' SummaryPage2_AccountApp_ApplicantSummary
Option Explicit On
Option Strict On

' #################################################################
' Account Application Applicant/ Client Details

' Version: 1.03 (31/03/2016)

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

'#Const "ShowClientWarnings",Boolean,True,"Show Client Warnings block."
'#Const "ShowEnquiriesAndDecisions",Boolean,True,"Show Enquiries & Decisions block."
'#Const "ShowLogs",Boolean,True,"Show Logs relating to this Applicant."
'#Const "ShowMap",Boolean,False,"Show a map of the primary physical Contact Address."

' Constants
Private ShowClientWarnings As Boolean = True
Private ShowEnquiriesAndDecisions As Boolean = True
Private ShowLogs As Boolean = True
Private ShowMap As Boolean = False

' Parameters
Private mIncludeShowTempObjectButton As Boolean = True

' Objects
Private mAccountAppApplicant As finAccountAppApplicant

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
  mAccountAppApplicant = DirectCast(source, finAccountAppApplicant)

  ' Get Parameters
  mIncludeShowTempObjectButton = eventArgs.GetBoolean("IncludeShowTempObjectButton", mIncludeShowTempObjectButton)
  
  ' Get Constants
  With ScriptInfo.Constants
    ShowClientWarnings = .GetBoolean("ShowClientWarnings", ShowClientWarnings)
    ShowEnquiriesAndDecisions = .GetBoolean("ShowEnquiriesAndDecisions", ShowEnquiriesAndDecisions)
    ShowLogs = .GetBoolean("ShowLogs", ShowLogs)
    ShowMap = .GetBoolean("ShowMap", ShowMap)
  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 Client As finClient
  Dim ClientCreateErrorMessage As String
  Dim Ok As Boolean
  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim strTemp As String
  Dim SummaryTables As ISSummaryTables
  Dim Warnings As finWarnings

  ' Assume Success
  Ok = True

  ' Create Client
  If Not mAccountAppApplicant.CreateClient(False, Client) Then
    Client = Nothing
    ClientCreateErrorMessage = 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%")

  ' Applicant Summary
  sb.BlockBegin("Applicant Summary", "", isefinSummaryPageBlockHeadingStyle.Main)
  SummaryTables = finBL.SummaryPageStandardBlocks.AccountApp_ApplicantSummary(ScriptInfo, mAccountAppApplicant, mIncludeShowTempObjectButton)
  If Client Is Nothing AndAlso summarytables.Count <> 0 Then
    SummaryTables.ItemByIndex(summarytables.Count - 1).TextAfter &= "{{error|" & ClientCreateErrorMessage & "}}"
  End If
  sb.SummaryTablesAppend(SummaryTables)
  sb.BlockEnd()

  ' Client Summary
  If Client IsNot Nothing Then
    If mAccountAppApplicant.AccountApp.IsClosed Then
      strTemp = ""
    ElseIf mAccountAppApplicant.ClientPk = 0 Then
      strTemp = "(New Client)"
    ElseIf mAccountAppApplicant.UpdateClient Then
      strTemp = "(Existing Client updated from Applicant details)"
    End If

    sb.BlockBegin("Client Summary",strTemp)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Client_Summary(ScriptInfo, Client, False))
    sb.BlockEnd()
  End If

  ' End Column 1
  sb.ColumnEnd()

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

  ' Enquiries and Decisions
  If ShowEnquiriesAndDecisions Then
    ' Title
    strTemp = "Enquiries"
    If finBL.DecisionCards.Count <> 0 Then strTemp &= " & Decisions"

    sb.BlockBegin(strTemp)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountApp_ApplicantEnquiriesAndDecisions(ScriptInfo, mAccountAppApplicant))
    sb.BlockEnd()
  End If

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

  ' Client Warnings
  If ShowClientWarnings AndAlso Client IsNot Nothing Then
    Warnings = Client.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

  ' Addresses
  If Client IsNot Nothing Then
    sb.BlockBegin("Addresses")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Client_Addresses(ScriptInfo, Client))
    sb.BlockEnd()
  End If

  ' Map
  If ShowMap AndAlso Client IsNot Nothing Then
    If Client.ContactAddresses.GetCurrentPhysicalAddressContactAddress() IsNot Nothing Then
      sb.BlockBegin("Map")
      sb.Append(finBL.SummaryPageStandardBlocks.Client_Map(ScriptInfo, Client))
      sb.BlockEnd()
    End If
  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