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 Microsoft.VisualBasic.CompilerServices;
using System.Linq;
using System.Collections.Generic;

namespace FMStocks_DB
{
   public class Ticker : System.IDisposable
   {
      bool _disposed = false;
      private const string m_modName = "FMStocks_DB.Ticker";
      private DBHelper m_dbh = null;
      public Ticker() : 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 bool VerifySymbol(string Ticker)
      {
         bool VerifySymbol = false;

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

            VerifySymbol = m_dbh.RunSPReturnInteger("Ticker_VerifySymbol",new object[]{Helpers.mp("@Ticker",System.Data.SqlDbType.Char,12,Ticker)},String.Empty) != 0;


            return VerifySymbol;

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

            Helpers.RaiseError(m_modName,"VerifySymbol(" + Ticker + ")","",0,false);
         }
         return VerifySymbol;
      }
      public decimal GetPrice(string Ticker)
      {
         decimal GetPrice = 0.00M;

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

            int x = 0;
            x = m_dbh.RunSPReturnInteger("Ticker_GetPrice",new object[]{Helpers.mp("@Ticker",System.Data.SqlDbType.Char,12,Ticker)},String.Empty);

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


            return GetPrice;

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

            Helpers.RaiseError(m_modName,"GetPrice(" + Ticker + ")","",0,false);
         }
         return GetPrice;
      }
      public gmRTL.DataLib.SqlClient.Recordset ListByTicker(string Ticker)
      {
         gmRTL.DataLib.SqlClient.Recordset ListByTicker = default;

         try
         {
            gmRTL.Core.GlobalException.Clear();
            ListByTicker = m_dbh.RunSPReturnRS("Ticker_ListByTicker",new object[]{Helpers.mp("@Ticker",System.Data.SqlDbType.VarChar,12,Ticker)},String.Empty);

            return ListByTicker;

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

            Helpers.RaiseError(m_modName,"ListByTicker(" + Ticker + ")","",0,false);
         }
         return ListByTicker;
      }
      public gmRTL.DataLib.SqlClient.Recordset ListByCompany(string Company)
      {
         gmRTL.DataLib.SqlClient.Recordset ListByCompany = default;

         try
         {
            gmRTL.Core.GlobalException.Clear();
            ListByCompany = m_dbh.RunSPReturnRS("Ticker_ListByCompany",new object[]{Helpers.mp("@Company",System.Data.SqlDbType.VarChar,60,Company)},String.Empty);

            return ListByCompany;

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

            Helpers.RaiseError(m_modName,"ListByCompany(" + Company + ")","",0,false);
         }
         return ListByCompany;
      }
      public gmRTL.DataLib.SqlClient.Recordset GetFundamentals(string Ticker)
      {
         gmRTL.DataLib.SqlClient.Recordset GetFundamentals = default;

         try
         {
            gmRTL.Core.GlobalException.Clear();
            GetFundamentals = m_dbh.RunSPReturnRS("Ticker_Fundamentals",new object[]{Helpers.mp("@Ticker",System.Data.SqlDbType.VarChar,12,Ticker)},String.Empty);

            return GetFundamentals;

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

            Helpers.RaiseError(m_modName,"GetFundamentals(" + Ticker + ")","",0,false);
         }
         return GetFundamentals;
      }
   }
}
