' SummaryPage2_Branch_KeyDetails
Option Explicit On
Option Strict On

' #################################################################
' Branch Key Details
' 
' Version: 1.00 (08/02/2024)
' 
' Usage: Branches form
' #################################################################
' Version        Date  User     Job       Description
' -------  ----------  -------  --------  -------------------------
'    1.00  08/02/2024  ISS      J032732   Created
' #################################################################

'#Const "ImageWidthPixels",Integer,240,"The number of pixels to fix the Client image or video width to. Set to zero to never show the Client image or video."
'#Const "IncludeInvalidContactMethods",Boolean,False,"Include Invalid Contact Methods in Summary."
'#Const "ShowOtherContactsNotes",Boolean,False,"Show Other Contacts 'Notes' column."
'#Const "ShowMap",Boolean,False,"Show a map of the primary physical Contact Address."

' Constants
Private ImageWidthPixels As Integer = 240
Private IncludeInvalidContactMethods As Boolean = False
Private ShowOtherContactsNotes As Boolean = False
Private ShowRelatedViaHashtagsBlock As Boolean = True
Private ShowMap As Boolean = True

' Objects
Private mAccountClient As finAccountClient
Private mBranch As finBranch
Private mClient As finClient

' Other
Private mAccountClientProductTypeId As String

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
    mBranch = finBL.CreateBranch()
    If mBranch.Load(CStr(source)) Then
      mClient = mBranch.Client
    Else
      Throw New ApplicationException(finBL.Error.Message)
    End If  

    ' Check not filtered out
    If finBL.CurrentUserInformation.IsClientFilteredOut(mClient.ClientId) Then
      Throw New ApplicationException(finBL.Error.Message)
    End If
  Else
    mBranch = DirectCast(source, finBranch)
    mClient = mBranch.Client
  End If

  ' Is Account Client supplied (e.g., previewing from Accounts form, Clients page)?
  If TypeOf eventArgs.GetObject("AccountClient") Is finAccountClient Then
    mAccountClient = DirectCast(eventArgs.GetObject("AccountClient"), finAccountClient)
    mAccountClientProductTypeId = eventArgs.GetString("ProductTypeId")
  End If

  ' Get Constants
  With ScriptInfo.Constants
    ImageWidthPixels = .GetInteger("ImageWidthPixels", ImageWidthPixels)
    IncludeInvalidContactMethods = .GetBoolean("IncludeInvalidContactMethods", IncludeInvalidContactMethods)
    ShowOtherContactsNotes = .GetBoolean("ShowOtherContactsNotes", ShowOtherContactsNotes)
    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 IncludedContactMethodPks As ISList
  Dim kvl As ISKeyValueList
  Dim Ok As Boolean
  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim SummaryTable As ISSummaryTable 
  Dim SummaryTables As ISSummaryTables
  Dim Warnings As finWarnings

  ' Assume Success
  Ok = True

  ' Initialise
  kvl = finBL.CreateKeyValueList
  sb = finBL.CreateHtmlSummaryPage2StringBuilder()

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

  If mClient.IsNew AndAlso mClient.AccountAppApplicantPk = 0 Then
    ' No Client loaded
    sb.SectionColumnsAddQuickBlock("Summary", "{{info|Please enter a code to view a Client record.}}")
  ElseIf finBL.CurrentUserInformation.IsClientFilteredOut(mClient.ClientId) Then
    ' Filtered out Client
    ' Begin Section
    sb.SectionColumnsBegin()

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

    ' Summary
    sb.BlockBegin("Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Client_SummaryFilteredOut(ScriptInfo, mClient, Me.ScriptInfo.FormKey <> "Clients"))
    sb.BlockEnd()

    ' End Column 1
    sb.ColumnEnd()

    ' End Section
    sb.SectionColumnsEnd()
  Else
    ' Client
    ' Begin Section
    sb.SectionColumnsBegin()

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

    ' Summary
    ' Other Parameters
    kvl.Clear
    kvl.SetInteger("ImageWidthPixels", ImageWidthPixels)
    kvl.SetBoolean("SingleTable", True)

    sb.BlockBegin("Summary", "", If(Len(mClient.AlertMessage) = 0, isefinSummaryPageBlockHeadingStyle.Main, isefinSummaryPageBlockHeadingStyle.Warning), False)
    SummaryTables = finBL.SummaryPageStandardBlocks.Client_Summary3(ScriptInfo, mClient, IncludedContactMethodPks, (Me.ScriptInfo.FormKey <> "Clients"), IncludeInvalidContactMethods, False, kvl)
    sb.SummaryTablesAppend(SummaryTables)
    sb.BlockEnd()

    ' Addresses
    sb.BlockBegin("Addresses")
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.Client_Addresses(ScriptInfo, mClient))
    sb.BlockEnd()

    ' Other Contacts
    SummaryTables = finBL.SummaryPageStandardBlocks.Client_OtherContacts(ScriptInfo, mClient, IncludedContactMethodPks, ShowOtherContactsNotes)
    sb.BlockBegin("Other Contacts")
    sb.SummaryTablesAppend(SummaryTables)
    sb.BlockEnd()

    ' End Column 1
    sb.ColumnEnd()

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

    ' Account Client
    If mAccountClient IsNot Nothing Then
      sb.BlockBegin("Account Client")
      sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountClient_Summary(ScriptInfo, mAccountClient, mAccountClientProductTypeId, False))
      sb.BlockEnd()
    End If

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

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