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 Broker : System.IDisposable
   {
      bool _disposed = false;
      private const string m_modName = "FMStocks_DB.Broker";
      private DBHelper m_dbh = null;
      public Broker() : 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;
      }
      //  Returns the PositionID or 0 if it failed
      public int Buy(int TxID,int AccountID,string Ticker,int Shares,decimal Price,decimal Commission)
      {
         int Buy = 0;


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

            int x = 0;

            x = m_dbh.RunSPReturnInteger("Broker_Buy",new object[]{Helpers.mp("@TxID",System.Data.SqlDbType.Int,4,TxID),Helpers.mp("@AccountID",System.Data.SqlDbType.Int,4,AccountID),Helpers.mp("@Ticker",System.Data.SqlDbType.Char,12,Ticker),Helpers.mp("@Shares",System.Data.SqlDbType.Int,4,Shares),Helpers.mp("@Price",System.Data.SqlDbType.Money,8,Price),Helpers.mp("@Commission",System.Data.SqlDbType.Money,8,Commission)},String.Empty);

            Buy = Helpers.NullsToZero(x).ToInt();     // changes a null return value into a zero, or returns x


            return Buy;

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

            Helpers.RaiseError(m_modName,"Buy(" + TxID + "," + AccountID + "," + Ticker + "," + Shares + "," + Price.ToStr() + "," + Commission.ToStr() + ")","",0,false);
         }
         return Buy;
      }
      public bool Sell(int TxID,decimal Price,decimal Commission)
      {
         bool Sell = false;


         try
         {
            gmRTL.Core.GlobalException.Clear();
            Sell = true;

            int x = 0;
            x = m_dbh.RunSPReturnInteger("Broker_Sell",new object[]{Helpers.mp("@TxID",System.Data.SqlDbType.Int,4,TxID),Helpers.mp("@Price",System.Data.SqlDbType.Money,8,Price),Helpers.mp("@Commission",System.Data.SqlDbType.Money,8,Commission)},String.Empty);

            if (x == 0)
            {
               Sell = false;
               throw new System.Exception();
            }
            else
            {
               Sell = true;
            }


            return Sell;

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

            Helpers.RaiseError(m_modName,"Sell(" + TxID + "," + Price.ToStr() + "," + Commission.ToStr() + ")","",0,false);
         }
         return Sell;
      }
   }
}
