using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Data;
using VBNET = Microsoft.VisualBasic;
using gmRTL.Core;
using Microsoft.VisualBasic.CompilerServices;
using System.Linq;
using System.Collections.Generic;

namespace FMStocks_Bus
{
   public class Account
   {
      private const string m_modName = "FMStocks_Bus.Account";
      private const int m_errDuplicatedAccount = -2147217900;
      public bool VerifyLogin(string Email,string Password,ref string FullName,ref int AccountID)
      {
         bool VerifyLogin = false;


         FMStocks_DB.Account objDB = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();
            VerifyLogin = false;

            objDB = new FMStocks_DB.Account();

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

            objDB = null;


            return VerifyLogin;

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);

            objDB = null;

            FMStocks_DB.Helpers.RaiseError(m_modName,"VerifyLogin(" + Email + ",...)",String.Empty,0,false);
         }
         return VerifyLogin;
      }
      public gmRTL.DataLib.SqlClient.Recordset ListPositionsForSale(int AccountID)
      {
         gmRTL.DataLib.SqlClient.Recordset ListPositionsForSale = default;

         FMStocks_DB.Position obj = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();

            obj = new FMStocks_DB.Position();

            ListPositionsForSale = obj.ListForSale(AccountID);


            return ListPositionsForSale;

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);

            obj = null;

            FMStocks_DB.Helpers.RaiseError(m_modName,"ListPositionsForSale(" + AccountID + ")",String.Empty,0,false);
         }
         return ListPositionsForSale;
      }
      public gmRTL.DataLib.SqlClient.Recordset ListPositions(int AccountID)
      {
         gmRTL.DataLib.SqlClient.Recordset ListPositions = default;

         FMStocks_DB.Position objDB = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();

            objDB = new FMStocks_DB.Position();

            ListPositions = objDB.ListSummary(AccountID);

            objDB = null;

            return ListPositions;

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);

            objDB = null;

            FMStocks_DB.Helpers.RaiseError(m_modName,"ListPositions(" + AccountID + ")",String.Empty,0,false);
         }
         return ListPositions;
      }
      public object GetSummary(int AccountID,ref decimal MarketValue,ref decimal TotalInvestment,ref decimal CashBalance)
      {
         object GetSummary = null;


         FMStocks_DB.Account objAccount = null;
         gmRTL.DataLib.SqlClient.Recordset rs = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();

            objAccount = new FMStocks_DB.Account();

            rs = objAccount.Summary(AccountID);
            objAccount = null;

            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);


            return GetSummary;

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);

            MarketValue = 0;
            TotalInvestment = 0;
            CashBalance = 0;

            rs = null;
            objAccount = null;


            FMStocks_DB.Helpers.RaiseError(m_modName,"GetSummary(" + AccountID + ")",String.Empty,0,false);
         }
         return GetSummary;
      }
      public int Add(string FirstName,string LastName,string Password,string Email)
      {
         int Add = 0;


         FMStocks_DB.Account db = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();

            db = new FMStocks_DB.Account();

            decimal defBalance = 0.00M;
            defBalance = "10000".ToDec();

            int NewAccountID = 0;

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

            // set return value
            Add = NewAccountID;

            db = null;

            return Add;

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);

            db = null;


            if (gmRTL.Core.GlobalException.Instance.Number != m_errDuplicatedAccount)
            {
               // -- 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);
            }

         }
         return Add;
      }
      public void GetAccountInfo(int AccountID,ref string FullName,ref string Email,ref decimal Balance)
      {
         FMStocks_DB.Account objDB = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();
            objDB = new FMStocks_DB.Account();

            objDB.GetAccountInfo(AccountID,ref FullName,ref Email,ref Balance);
            objDB = null;


            return;

         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);

            objDB = null;

            FMStocks_DB.Helpers.RaiseError(m_modName,"GetAccountInfo(" + AccountID + ")",String.Empty,0,false);
         }
      }
      public Account() : base()
      {
         Class_Initialize();
      }
      private void Class_Initialize()
      {
         System.Diagnostics.Debug.WriteLine("INIT");
      }
   }
}
