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 Ticker
   {
      private const string m_modName = "FMStocks_Bus.Ticker";
      // VerifySymbol

      public bool VerifySymbol(string Ticker)
      {
         bool VerifySymbol = false;

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

            VerifySymbol = obj.VerifySymbol(Ticker);
            obj = null;


            return VerifySymbol;

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

            FMStocks_DB.Helpers.RaiseError(m_modName,"VerifySymbol(" + Ticker + ")",String.Empty,0,false);

         }
         return VerifySymbol;
      }
      //  ListByTicker

      public gmRTL.DataLib.SqlClient.Recordset ListByTicker(string Ticker)
      {
         gmRTL.DataLib.SqlClient.Recordset ListByTicker = default;

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

            ListByTicker = obj.ListByTicker(Ticker);
            obj = null;


            return ListByTicker;

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

            FMStocks_DB.Helpers.RaiseError(m_modName,"ListByTicker(" + Ticker + ")",String.Empty,0,false);
         }
         return ListByTicker;
      }
      //  ListByCompany

      public gmRTL.DataLib.SqlClient.Recordset ListByCompany(string company)
      {
         gmRTL.DataLib.SqlClient.Recordset ListByCompany = default;

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

            ListByCompany = obj.ListByCompany(company);
            obj = null;


            return ListByCompany;

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

            FMStocks_DB.Helpers.RaiseError(m_modName,"ListByCompany(" + company + ")",String.Empty,0,false);
         }
         return ListByCompany;
      }
      //  GetFundamentals
      // 
      //  Takes a ticker symbol and returns an array of fundamental metric values,
      //  the company name and the trading exchange.
      public gmRTL.DataLib.SqlClient.Recordset GetFundamentals(string Ticker,ref object company,ref object Exchange)
      {
         gmRTL.DataLib.SqlClient.Recordset GetFundamentals = default;

         FMStocks_DB.Ticker obj = null;
         gmRTL.DataLib.SqlClient.Recordset rs = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();
            obj = new FMStocks_DB.Ticker();

            rs = obj.GetFundamentals(Ticker);

            //  Construct a new recordset based on the results from the fundamental queries

            gmRTL.DataLib.SqlClient.Recordset rsnew = null;
            rsnew = new gmRTL.DataLib.SqlClient.Recordset();

            rsnew.CursorLocation = gmRTL.DataLib.CursorLocationEnum.adUseClient;

            // Add Some Fields
            rsnew.Fields.Append("ShortDesc",System.Data.SqlDbType.VarChar,50);
            rsnew.Fields.Append("LongDesc",System.Data.SqlDbType.VarChar,100);
            rsnew.Fields.Append("Criteria",System.Data.SqlDbType.VarChar,50);
            rsnew.Fields.Append("Value",System.Data.SqlDbType.VarChar,50);
            rsnew.Fields.Append("Status",System.Data.SqlDbType.Int,4);

            rsnew.Open(null, null, gmRTL.DataLib.CursorTypeEnum.adOpenStatic, gmRTL.DataLib.LockTypeEnum.adLockBatchOptimistic);

            while (!rs.EOF)
            {
               company = rs.Fields["Company"].Value;
               Exchange = rs.Fields["Exchange"].Value;

               FormatItem(rsnew,(FMStocks_DB.Helpers.NullsToZero(rs.Fields["MarketCap"]) < 1000).ToInt(),"MarketCap","$" + FMStocks_DB.Helpers.NullsToZero(rs.Fields["MarketCap"]).ToStr() + "M","< $1B","Total market capitalization less than $1 Billion.");
               FormatItem(rsnew,(FMStocks_DB.Helpers.NullsToZero(rs.Fields["Sales"]) < 100).ToInt(),"Sales","$" + FMStocks_DB.Helpers.NullsToZero(rs.Fields["Sales"]).ToStr() + "M","< $100M","Gross revenue (total sales) less than $100 Million.");
               FormatItem(rsnew,(FMStocks_DB.Helpers.NullsToZero(rs.Fields["Price"]) > 7).ToInt(),"Price","$" + FMStocks_DB.Helpers.NullsToZero(rs.Fields["Price"]).ToStr(),"> $7/share","Stock price greater than $7 per share.");
               FormatItem(rsnew,(FMStocks_DB.Helpers.NullsToZero(rs.Fields["DailyDollarVol"]) > 1).ToInt(),"Daily dollar volume","$" + FMStocks_DB.Helpers.NullsToZero(rs.Fields["DailyDollarVol"]).ToStr() + "M","> $1M per day","At least $100M in shares move on an average day.");
               FormatItem(rsnew,(FMStocks_DB.Helpers.NullsToZero(rs.Fields["SalesGrowth"]) > 25).ToInt(),"Sales growth",FMStocks_DB.Helpers.NullsToZero(rs.Fields["SalesGrowth"]).ToStr() + "%","> 25%","Gross revenue growth of 25% trailing-twelve months (TTM).");
               FormatItem(rsnew,(FMStocks_DB.Helpers.NullsToZero(rs.Fields["EPSGrowth"]) > 25).ToInt(),"Earnings growth",FMStocks_DB.Helpers.NullsToZero(rs.Fields["EPSGrowth"]).ToStr() + "%","> 25%","Earnings-per-share growth over TTM.");
               FormatItem(rsnew,(FMStocks_DB.Helpers.NullsToZero(rs.Fields["NetProfitMargin"]) > 7).ToInt(),"Profit margin",FMStocks_DB.Helpers.NullsToZero(rs.Fields["NetProfitMargin"]).ToStr() + "%","> 7%","Net profit margin at least 7%.");
               FormatItem(rsnew,(FMStocks_DB.Helpers.NullsToZero(rs.Fields["InsiderShares"]) > 10).ToInt(),"Insider holdings",FMStocks_DB.Helpers.NullsToZero(rs.Fields["InsiderShares"]).ToStr() + "%","> 10%","At least 10% of the shares should be held by insiders.");

               rs.MoveNext();
            }
            rsnew.UpdateBatch(gmRTL.DataLib.AffectEnum.adAffectAll);
            rsnew.MoveFirst();

            obj = null;
            rs = null;
            GetFundamentals = rsnew;

            return GetFundamentals;

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


            FMStocks_DB.Helpers.RaiseError(m_modName,"GetFundamentals(" + Ticker + ")",String.Empty,0,false);
         }
         return GetFundamentals;
      }
      // Helper sub to add the new records
      private void FormatItem(gmRTL.DataLib.SqlClient.Recordset rs,int Status,string ShortDesc,string Value,string Criteria,string LongDesc)
      {
         rs.AddNew(new object[]{"ShortDesc","LongDesc","Criteria","Value","Status"},new object[]{ShortDesc,LongDesc,Criteria,Value,Status});
      }
   }
}
