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_DB
{
   public class Account : System.IDisposable
   {
      bool _disposed = false;
      private const string m_modName = "FMStocks_DB.Account";
      private DBHelper m_dbh = null;
      public Account() : base()
      {
         Class_Initialize();
      }
      private void Class_Initialize()
      {
         // must call CreateObject or COM+ will
         // not call IObjectConstructor inferface that DBHelper needs
         m_dbh = new FMStocks_DB.DBHelper();
      }
      public void Dispose()
      {
         Dispose(true);
         GC.SuppressFinalize(this);
      }
      protected virtual void Dispose(bool disposing)
      {
         if (_disposed)
         {
            return;
         }
         if (disposing)
         {
            Class_Terminate();
         }
         _disposed = true;
      }
      private void Class_Terminate()
      {
         m_dbh = null;
      }
      public int Add(string FirstName,string LastName,string password,string Email,decimal Balance)
      {
         int Add = 0;


         try
         {
            gmRTL.Core.GlobalException.Clear();

            Add = m_dbh.RunSPReturnInteger("Account_Add",new object[]{Helpers.mp("@FirstName",System.Data.SqlDbType.VarChar,30,FirstName),Helpers.mp("@LastName",System.Data.SqlDbType.VarChar,30,LastName),Helpers.mp("@Password",System.Data.SqlDbType.VarChar,15,password),Helpers.mp("@EMail",System.Data.SqlDbType.VarChar,50,Email),Helpers.mp("@Balance",System.Data.SqlDbType.Money,8,Balance)},String.Empty);


            return Add;

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

            Helpers.RaiseError(m_modName,"Add","",0,false);
         }
         return Add;
      }
      public gmRTL.DataLib.SqlClient.Recordset Summary(int AccountID)
      {
         gmRTL.DataLib.SqlClient.Recordset Summary = default;

         try
         {
            gmRTL.Core.GlobalException.Clear();

            Summary = m_dbh.RunSPReturnRS("Account_Summary",new object[]{Helpers.mp("@AccountID",System.Data.SqlDbType.Int,4,AccountID)},String.Empty);


            return Summary;

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

            Helpers.RaiseError(m_modName,"Summary(" + AccountID + ")","",0,false);
         }
         return Summary;
      }
      public bool VerifyUser(string Email,string password,ref int AccountID,ref string FullName)
      {
         bool VerifyUser = false;


         System.Data.SqlClient.SqlCommand cmd = null;
         string strUserInfo = "";
         try
         {
            gmRTL.Core.GlobalException.Clear();

            //  We couldn't use the generic RunSP* functions because this stored proc has 2 output parameters
            //  The following code is a modified version of RunSPWithString to support 2 outputs and 1 return value

            cmd = new System.Data.SqlClient.SqlCommand();

            // Run the procedure
            string connectionString = "";
            connectionString = m_dbh.GetConnectionString();
            cmd.Connection = new System.Data.SqlClient.SqlConnection(connectionString);
            cmd.CommandText = "Account_VerifyLogin";
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            // set Params
            cmd.Parameters.Add(gmRTL.DataLib.SqlClient.SqlCommandExtensions.CreateParameter(cmd,"@retval",System.Data.SqlDbType.Int,System.Data.ParameterDirection.Output,0,null));
            cmd.Parameters.Add(gmRTL.DataLib.SqlClient.SqlCommandExtensions.CreateParameter(cmd,"@EMail",System.Data.SqlDbType.VarChar,System.Data.ParameterDirection.Input,50,Email));
            cmd.Parameters.Add(gmRTL.DataLib.SqlClient.SqlCommandExtensions.CreateParameter(cmd,"@Password",System.Data.SqlDbType.VarChar,System.Data.ParameterDirection.Input,50,password));
            cmd.Parameters.Add(gmRTL.DataLib.SqlClient.SqlCommandExtensions.CreateParameter(cmd,"@AccountID",System.Data.SqlDbType.Int,System.Data.ParameterDirection.Output,4,null));
            cmd.Parameters.Add(gmRTL.DataLib.SqlClient.SqlCommandExtensions.CreateParameter(cmd,"@FullName",System.Data.SqlDbType.VarChar,System.Data.ParameterDirection.Output,50,null));

            // execute Stored Proc
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();

            if (cmd.Parameters["@retval"].Value.ToInt() == 0)
            {
               VerifyUser = false;
            }
            else
            {
               AccountID = cmd.Parameters[3].Value.ToInt();
               FullName = cmd.Parameters[4].Value.ToStr();
               VerifyUser = true;
            }

            cmd = null;


            return VerifyUser;

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

            Helpers.RaiseError(m_modName,"VerifyUser(" + Email + ",...)" + "  [** TURN OFF FIREWALL **]","",0,false);
         }
         return VerifyUser;
      }
      public void GetAccountInfo(int AccountID,ref string FullName,ref string Email,ref decimal Balance)
      {
         gmRTL.DataLib.SqlClient.Recordset rs = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();


            rs = m_dbh.RunSPReturnRS("Account_GetInfo",new object[]{Helpers.mp("AccountID",System.Data.SqlDbType.Int,4,AccountID)},String.Empty);

            FullName = rs.Fields["Firstname"].Value.ToStr() + " " + rs.Fields["Lastname"].Value.ToStr();
            Email = rs.Fields["email"].Value.ToStr();
            Balance = rs.Fields["Balance"].Value.ToDec();

            rs = null;
            return;

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

            Helpers.RaiseError(m_modName,"GetAccountInfo(" + AccountID + ")","",0,false);
         }
      }
   }
}
