Option Strict Off
Option Explicit On
Imports System.Linq
Imports System.Collections.Generic
Namespace FMStocks_Bus
   Public Class Account
   Private Const m_modName As String = "FMStocks_Bus.Account"
   Private Const m_errDuplicatedAccount As Integer = -2147217900
   Public Function VerifyLogin(ByVal Email As String,ByVal Password As String,ByRef FullName As String,ByRef AccountID As Integer) As Boolean
      VerifyLogin = False

      Dim objDB As FMStocks_DB.Account = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()
         VerifyLogin = False

         objDB = New FMStocks_DB.Account

         VerifyLogin = objDB.VerifyUser(Email,Password,AccountID,FullName)

         objDB = Nothing


         Exit Function

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)

         objDB = Nothing

         FMStocks_DB.Helpers.RaiseError(m_modName,"VerifyLogin(" & Email & ",...)",String.Empty,0,False)
      End Try
   End Function
   Public Function ListPositionsForSale(ByVal AccountID As Integer) As gmRTL.DataLib.SqlClient.Recordset
      ListPositionsForSale = Nothing
      Dim obj As FMStocks_DB.Position = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         obj = New FMStocks_DB.Position

         ListPositionsForSale = obj.ListForSale(AccountID)


         Exit Function

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)

         obj = Nothing

         FMStocks_DB.Helpers.RaiseError(m_modName,"ListPositionsForSale(" & AccountID & ")",String.Empty,0,False)
      End Try
   End Function
   Public Function ListPositions(ByVal AccountID As Integer) As gmRTL.DataLib.SqlClient.Recordset
      ListPositions = Nothing
      Dim objDB As FMStocks_DB.Position = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         objDB = New FMStocks_DB.Position

         ListPositions = objDB.ListSummary(AccountID)

         objDB = Nothing

         Exit Function

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)

         objDB = Nothing

         FMStocks_DB.Helpers.RaiseError(m_modName,"ListPositions(" & AccountID & ")",String.Empty,0,False)
      End Try
   End Function
   Public Sub GetSummary(ByVal AccountID As Integer,ByRef MarketValue As Decimal,ByRef TotalInvestment As Decimal,ByRef CashBalance As Decimal)

      Dim objAccount As FMStocks_DB.Account = Nothing
      Dim rs As gmRTL.DataLib.SqlClient.Recordset = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         objAccount = New FMStocks_DB.Account

         rs = objAccount.Summary(AccountID)
         objAccount = Nothing

         MarketValue = FMStocks_DB.Helpers.NullsToZero(rs.Fields("MarketValue").Value)
         TotalInvestment = FMStocks_DB.Helpers.NullsToZero(rs.Fields("TotalInvestment").Value)
         CashBalance = FMStocks_DB.Helpers.NullsToZero(rs.Fields("CashBalance").Value)


         Exit Sub

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)

         MarketValue = 0
         TotalInvestment = 0
         CashBalance = 0

         rs = Nothing
         objAccount = Nothing


         FMStocks_DB.Helpers.RaiseError(m_modName,"GetSummary(" & AccountID & ")",String.Empty,0,False)
      End Try
   End Sub
   Public Function Add(ByVal FirstName As String,ByVal LastName As String,ByVal Password As String,ByVal Email As String) As Integer
      Add = 0

      Dim db As FMStocks_DB.Account = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()

         db = New FMStocks_DB.Account

         Dim defBalance As Decimal = 0.00
         defBalance = "10000"

         Dim NewAccountID As Integer = 0

         NewAccountID = db.Add(FirstName,LastName,Password,Email,defBalance)

         'set return value
         Add = NewAccountID

         db = Nothing

         Exit Function

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)

         db = Nothing


         If (Err.Number <> m_errDuplicatedAccount) Then
            '-- Only raise an error if not trying to add a duplicate account...something we can handle
            FMStocks_DB.Helpers.RaiseError(m_modName,"Add(" & FirstName & "," & LastName & ",...," & Email & ")",String.Empty,0,False)
         End If

      End Try
   End Function
   Public Sub GetAccountInfo(ByVal AccountID As Integer,ByRef FullName As String,ByRef Email As String,ByRef Balance As Decimal)
      Dim objDB As FMStocks_DB.Account = Nothing
      Try
         gmRTL.Core.GlobalException.ClearErrorObject()
         objDB = New FMStocks_DB.Account

         objDB.GetAccountInfo(AccountID,FullName,Email,Balance)
         objDB = Nothing


         Exit Sub

      Catch exc As System.Exception
         gmRTL.Core.GlobalException.SetErrorObject(exc)

         objDB = Nothing

         FMStocks_DB.Helpers.RaiseError(m_modName,"GetAccountInfo(" & AccountID & ")",String.Empty,0,False)
      End Try
   End Sub
   Public Sub New()
      MyBase.New()
      Class_Initialize()
   End Sub
   Private Sub Class_Initialize()
      Debug.WriteLine("INIT")
   End Sub
   End Class
End Namespace
