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 FMSStore_Bus
{
   public class ShoppingCart
   {
      private const string m_modName = "FMSStore_Bus.ShoppingCart";
      private bool m_SendEvents = false;
      public ShoppingCart() : base()
      {
         Class_Initialize();
      }
      private void Class_Initialize()
      {
         m_SendEvents = true;     // send events by default.
      }
      private void IObjectConstruct_Construct(object pCtorObj)
      {
         // eval construction string and find out what the admin wants us to do
         string[] arrParams = Array.Empty<string>();
         int x = 0;

         arrParams = ((object)((dynamic)pCtorObj).ConstructString).ToStr().Split(",");
         for(x = arrParams.GetLowerBound(0); x <= arrParams.GetUpperBound(0); x += 1)
         {
            switch(FMStocks_DB.Helpers.GetKey(arrParams[x]).UCase())
            {
            case "EVENTS":
               m_SendEvents = FMStocks_DB.Helpers.GetValue(arrParams[x]).ToBool();
               break;
            case "DISABLEORDERING":
               m_SendEvents = !FMStocks_DB.Helpers.GetValue(arrParams[x]).ToBool();
               break;
            default:
               break;
            }
         }
      }
      public gmRTL.DataLib.SqlClient.Recordset GetByKey(int AccountID,int SKU)
      {
         gmRTL.DataLib.SqlClient.Recordset GetByKey = default;

         FMSStore_DB.ShoppingCart obj = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();


            obj = new FMSStore_DB.ShoppingCart();
            GetByKey = obj.GetByKey(AccountID,SKU);
            obj = null;


            return GetByKey;

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

            FMStocks_DB.Helpers.RaiseError(m_modName,"GetByKey(" + AccountID + "," + SKU + ")",String.Empty,0,false);
         }
         return GetByKey;
      }
      public void Add(int AccountID,int SKU)
      {
         FMSStore_DB.ShoppingCart obj = null;
         gmRTL.DataLib.SqlClient.Recordset rs = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();


            obj = new FMSStore_DB.ShoppingCart();
            rs = obj.GetByKey(AccountID,SKU);
            // check for the empty recordset.
            if (rs.EOF && rs.BOF)
            {
               obj.Add(AccountID,SKU,1);
            }
            else
            {
               //  Do nothing.  Add() is called only to create the first entry.
            }
            rs = null;
            obj = null;


            return;

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

            FMStocks_DB.Helpers.RaiseError(m_modName,"Add(" + AccountID + "," + SKU + ")",String.Empty,0,false);
         }
      }
      public void Buy(int AccountID)
      {
         // send event for External Order Processing
         FMSStore_Events.ShoppingCart obj = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();

            if (m_SendEvents)
            {
               obj = new FMSStore_Events.ShoppingCart();
               obj.ExecuteBuy(AccountID);
               obj = null;
            }


            return;

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

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

         FMSStore_DB.ShoppingCart obj = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();


            obj = new FMSStore_DB.ShoppingCart();
            ListByAccount = obj.ListByAccount(AccountID);
            obj = null;


            return ListByAccount;

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

            FMStocks_DB.Helpers.RaiseError(m_modName,"ListByAccount(" + AccountID + ")",String.Empty,0,false);
         }
         return ListByAccount;
      }
      public void SetQuantity(int AccountID,int SKU,int Quantity)
      {

         FMSStore_DB.ShoppingCart obj = null;
         gmRTL.DataLib.SqlClient.Recordset rs = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();


            obj = new FMSStore_DB.ShoppingCart();
            rs = obj.GetByKey(AccountID,SKU);
            if (rs.EOF && rs.BOF && Quantity > 0)
            {
               obj.Add(AccountID,SKU,Quantity);
            }
            else if (Quantity == 0)
            {
               obj.Remove(AccountID,SKU);
            }
            else
            {
               obj.Modify(AccountID,SKU,Quantity);
            }
            rs = null;
            obj = null;


            return;

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

            FMStocks_DB.Helpers.RaiseError(m_modName,"SetQuantity(" + AccountID + "," + SKU + "," + Quantity + ")",String.Empty,0,false);
         }
      }
      public gmRTL.DataLib.SqlClient.Recordset TotalByAccount(int AccountID)
      {
         gmRTL.DataLib.SqlClient.Recordset TotalByAccount = default;

         FMSStore_DB.ShoppingCart obj = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();


            obj = new FMSStore_DB.ShoppingCart();
            TotalByAccount = obj.TotalByAccount(AccountID);
            obj = null;


            return TotalByAccount;

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

            FMStocks_DB.Helpers.RaiseError(m_modName,"TotalByAccount(" + AccountID + ")",String.Empty,0,false);
         }
         return TotalByAccount;
      }
      public void EmptyShoppingCart(int AccountID)
      {
         FMSStore_DB.ShoppingCart obj = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();


            obj = new FMSStore_DB.ShoppingCart();
            obj.RemoveAll(AccountID);
            obj = null;


            return;

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

            FMStocks_DB.Helpers.RaiseError(m_modName,"EmptyShoppingCart(" + AccountID + ")",String.Empty,0,false);
         }
      }
   }
}
