' SummaryPage2_AccountClose_CloseSummary
Option Explicit On
Option Strict On

' #################################################################
' Account Close Summary
'
' Version: 2.00 (31/08/2016)
'
' Usage: Close Account wizard
' #################################################################

' Objects
Private mAccountCloseSummaryItem As finAccountCloseSummaryItem
Private mAccountCloseRefinanceOption As isefinAccountCloseRefinanceOption
Private mAccountClose As finAccountClose

' Other
Private mIndex As Integer

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 PageName As String

  ' Assume Success
  Main = True

  ' Initialise
  mAccountClose = DirectCast(source, finAccountClose)

  ' Get Event Args
  With eventArgs
    mIndex = .GetInteger("Index")
    PageName = .GetString("PageName")
  End With

  ' Get Constants
  With ScriptInfo.Constants
  End With

  Select Case eventId
    Case ""
      ' General event, i.e., return general page content
      ' NOTE: This Summary Page can produce serveral different pages for the Account Close wizard
      Select Case PageName
        Case "General"
          Main = CreatePageGeneral(text)

        Case "Refinance"
          Main = CreatePageRefinance(text)

        Case "Summary"
          Main = CreatePageSummary(text)
      End Select
  End Select

End Function

' Create Page General
Private Function CreatePageGeneral(ByRef html As String) As Boolean

  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim Success As Boolean

  ' Assume Success
  Success = True

  ' Initialise
  sb = finBL.CreateHtmlSummaryPage2StringBuilder()

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

  If mAccountClose.Account.IsNew Then
    ' No Account loaded
    sb.SectionColumnsAddQuickBlock("Summary", "{{info|Please select an Account.}}")
  ElseIf mAccountClose.Account.Status <> isefinAccountStatus.Open Then
    ' Invalid Account Status
    sb.SectionColumnsAddQuickBlock("Summary", "{{info|Please select an 'Open' Account.}}")
  Else
    ' Begin Section
    sb.SectionColumnsBegin()

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

    ' Summary
    sb.BlockBegin("Account Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountClose_Summary(ScriptInfo, mAccountClose))
    sb.BlockEnd()

    ' End Column 1
    sb.ColumnEnd()

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

    ' Financial Summary
    sb.BlockBegin("Financial Summary", "", isefinSummaryPageBlockHeadingStyle.Main, False)
    sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountClose_FinancialSummary(ScriptInfo, mAccountClose, True, False))
    sb.BlockEnd()

    ' End Section
    sb.SectionColumnsEnd()
  End If

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Success

End Function

' Create Page Refinance
Private Function CreatePageRefinance(ByRef html As String) As Boolean

  Dim Success As Boolean

  ' Assume Success
  Success = True

  ' Resolve Templates
  Select Case mAccountClose.RefinanceOption
    Case isefinAccountCloseRefinanceOption.ExistingAccount
      html = Main_Refinance_ExistingAccount(mAccountClose)
    Case isefinAccountCloseRefinanceOption.NewAccount
      html = Main_Refinance_NewAccount(mAccountClose, mIndex)
  End Select

  Return Success

End Function

' Create Page Summary - Where Rebates/ Credits entered
Private Function CreatePageSummary(ByRef html As String) As Boolean

  Dim sb As finHtmlSummaryPage2StringBuilder
  Dim Success As Boolean

  ' Assume Success
  Success = True

  ' Get Index
  If mIndex <> -1 Then mAccountCloseSummaryItem = mAccountClose.Summary(mIndex)

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

  ' Summary
  sb.BlockBegin("Summary Credit", "", isefinSummaryPageBlockHeadingStyle.Main, False)
  sb.SummaryTablesAppend(finBL.SummaryPageStandardBlocks.AccountClose_SummaryCredits(ScriptInfo, mAccountClose, mAccountCloseSummaryItem))
  sb.BlockEnd()

  ' End Column 1
  sb.ColumnEnd()

  ' End Section
  sb.SectionColumnsEnd()

  ' Finalise HTML
  sb.HtmlFinalise()

  ' Resolve Templates
  html = sb.ToString(True)

  Return Success

End Function

' Main Refinance Existing Account
Private Function Main_Refinance_ExistingAccount(accountClose As finAccountClose) As String

  Dim AccountWithdrawal As finAccountWithdrawal
  Dim Success As Boolean

  ' Use built-in Account Withdrawal Summary Page (since this is effectively what we're doing)
  If accountClose.RefinanceIntoAccountPk = 0 Then
    Return ""
  Else
    AccountWithdrawal = finBL.CreateAccountWithdrawal()
    With AccountWithdrawal
      Success = .AccountLoadPk(accountClose.RefinanceIntoAccountPk, True)
      .WithdrawalDate = accountClose.CloseDate
      .WithdrawalValue = accountClose.BalanceDue
    End With

    If Success Then
      Return finBL.ScriptFunctions.BuiltInAccountWithdrawalSummaryScript2(AccountWithdrawal, "", Nothing, False, Nothing, ScriptInfo.ContextData.ToKeyValueList(), ScriptInfo.Properties)
    Else
      Return finBL.GetHtmlErrorMessage(finBL.Error.Message, finBL.Error.Details)
    End If
  End If

End Function

' Main Refinance New Account
Private Function Main_Refinance_NewAccount(accountClose As finAccountClose, index As Integer) As String

  Dim Account As finAccount

  ' Use built-in Account Financial Summary Page
  If accountClose.CreateRefinanceAccount(Account) Then
    Return finBL.ScriptFunctions.BuiltInAccountFinancialSummaryScript2(Account, index, "", Nothing, False, Nothing, ScriptInfo.ContextData.ToKeyValueList(), ScriptInfo.Properties)
  Else
    Return finBL.GetHtmlErrorMessage(finBL.Error.Message, finBL.Error.Details)
  End If

End Function