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 FMSTest
{
   public partial class frmFMSTest : System.Windows.Forms.Form
   {
      private bool _disposed = false;
      public frmFMSTest()
      {
         if(m_vb6FormDefInstance == null)
         {
            m_vb6FormDefInstance = this;
         }
         //
         // Required for Windows Form Designer support
         //
         InitializeComponent();

         //
         // TODO: Add any constructor code after InitializeComponent call
         //
         if (DesignMode) return;
         Form_Load(null, null);
      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
      protected override void Dispose(bool disposing)
      {
         if (_disposed)
         {
            return;
         }
         if (disposing)
         {
            if (components != null)
            {
               components.Dispose();
            }
         }
         _disposed = true;
         base.Dispose(disposing);
      }

      string logPath = "";
      private const string CLSTEXT = "<CLS>";
      public void cmdTest_Click(object sender, EventArgs e)
      {
         RunTests();
      }
      public void RunTests()
      {
         logPath = utils.gLogFile;

         if (System.IO.File.Exists(logPath))
         {
            System.IO.File.Delete(logPath);
         }

         FMStocks_DB.DBHelper o = null;
         o = new FMStocks_DB.DBHelper();
         msgText("Database = " + o.GetConnectionString());
         o = null;


         Test1();
         Test2();
         Test3();
         Test4();
         Test5();

      }
      private void Test1()
      {
         gmRTL.Core.ErrorHandling.ResumeNext(() =>
         {

            FMStocks_Bus.Account obj = null;
            obj = new FMStocks_Bus.Account();
            //  Common deployment error: this error handling block catches the common
            //  case where the FMStocks_Bus component isn't registered on the app server.
            if (gmRTL.Core.GlobalException.Instance.Number != 0)
            {
               msgText("Could not create an instance of the FMStocks_Bus.Account. Refer to the setup instructions for FMStocks." + "\r\n" + "Error: " + gmRTL.Core.GlobalException.Instance.Description + " (" + gmRTL.Core.GlobalException.Instance.Number + ")");
            }

            int AccountID = 0;
            string Email = "";
            string Password = "";

            Password = "pw";
            Email = "scott@vertigosoftware.com";

            FMStocks_Bus.Account objAccount = null;
            string FullName = "";
            bool bIsValid = false;

            objAccount = new FMStocks_Bus.Account();

            bIsValid = objAccount.VerifyLogin(Email,Password,ref FullName,ref AccountID);

            //  Common database error: if the DSN is incorrect, or the database is down or
            //  not reachable, this is the first place
            if (bIsValid)
            {
               msgText("VerifyLogin call was successful");
            }
            else
            {
               if (gmRTL.Core.GlobalException.Instance.Number != 0)
               {
                  msgText("VerifyLogin call failed." + "\r\n" + "Error: " + gmRTL.Core.GlobalException.Instance.Description + " (" + gmRTL.Core.GlobalException.Instance.Number + ")");
               }
               else
               {
                  msgText("VerifyLogin call failed");
               }

               AccountID = obj.Add("Scott","Stanfield",Password,Email);

               if (gmRTL.Core.GlobalException.Instance.Number != 0 || AccountID == 0)
               {
                  msgText("Unable to add a new account. This usually means you tried to create an account using an email address that is already in use." + "\r\n" + "Error: " + gmRTL.Core.GlobalException.Instance.Description + " (" + gmRTL.Core.GlobalException.Instance.Number + ")");
               }
               else
               {
                  msgText("A new account was successfully added with the ID of " + AccountID);
               }
            }

            objAccount = null;
            obj = null;

         });
      }
      private void Test2()
      {
         const string FUNCNAME = "Test2";
         FMStocks_Bus.Account obj = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();

            obj = new FMStocks_Bus.Account();

            int AccountID = 0;
            string FullName = "";
            string Email = "";
            decimal Balance = 0.00M;

            obj.VerifyLogin("ta1","ta",ref FullName,ref AccountID);

            obj.GetAccountInfo(AccountID,ref FullName,ref Email,ref Balance);

            msgText("AccountID =" + AccountID + "\r\n" + "FullName =" + FullName + "\r\n" + "Email =" + Email + "\r\n" + "Balance =" + Balance.ToStr());

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

         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            msgText(FUNCNAME + ": " + gmRTL.Core.GlobalException.Instance.Description);
         }
         else
         {
            msgText(FUNCNAME + ": " + "Completed");
         }

      }
      private void Test3()
      {
         const string FUNCNAME = "Test3";
         FMStocks_DB.Account obj = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();

            obj = new FMStocks_DB.Account();

            int AccountID = 0;
            string FullName = "";
            string Email = "";
            decimal Balance = 0.00M;

            obj.VerifyUser("ta1","ta",ref AccountID,ref FullName);

            msgText(obj.GetTypeName() + "VerifyUser" + "\r\n" + "AccountID =" + AccountID + "\r\n" + "FullName  =" + FullName + "\r\n");

            obj.GetAccountInfo(AccountID.ToInt(),ref FullName,ref Email,ref Balance);

            msgText(obj.GetTypeName() + "GetAccountInfo" + "\r\n" + "AccountID =" + AccountID + "\r\n" + "FullName  =" + FullName + "\r\n" + "Email     =" + Email + "\r\n" + "Balance   =" + Balance.ToStr());

            gmRTL.DataLib.SqlClient.Recordset rs = null;
            rs = obj.Summary(AccountID);
            msgText(obj.GetTypeName() + "Summary" + "\r\n" + utils.reportRecordSet(rs));

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

         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            msgText(FUNCNAME + ": " + gmRTL.Core.GlobalException.Instance.Description);
         }
         else
         {
            msgText(FUNCNAME + ": " + "Completed");
         }

      }
      private void Test4()
      {
         const string FUNCNAME = "Test4";
         FMSStore_Bus.Product obj = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();

            obj = new FMSStore_Bus.Product();

            gmRTL.DataLib.SqlClient.Recordset rs = null;

            rs = obj.List();
            msgText(obj.GetTypeName() + "List" + "\r\n" + utils.reportRecordSet(rs));

            rs = obj.FindByDescription("Gorilla");
            msgText(obj.GetTypeName() + "FindByDescription" + "\r\n" + utils.reportRecordSet(rs));

            rs = obj.GetBySKU(1004007);
            msgText(obj.GetTypeName() + "GetBySKU" + "\r\n" + utils.reportRecordSet(rs));


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

         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            msgText(FUNCNAME + ": " + gmRTL.Core.GlobalException.Instance.Description);
         }
         else
         {
            msgText(FUNCNAME + ": " + "Completed");
         }

      }
      private void Test5()
      {
         const string FUNCNAME = "Test5";
         FMStocks_Bus.Ticker obj = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();

            obj = new FMStocks_Bus.Ticker();

            gmRTL.DataLib.SqlClient.Recordset rs = null;

            object company = null;
            object exchange = null;

            rs = obj.GetFundamentals("GE",ref company,ref exchange);
            msgText(obj.GetTypeName() + "GetFundamentals" + "\r\n" + utils.reportRecordSet(rs));

            rs = obj.ListByCompany("General Electric");
            msgText(obj.GetTypeName() + "ListByCompany" + "\r\n" + utils.reportRecordSet(rs));

            rs = obj.ListByTicker("GEER");
            msgText(obj.GetTypeName() + "ListByTicker" + "\r\n" + utils.reportRecordSet(rs));


            bool boolRC = false;
            boolRC = obj.VerifySymbol("GE");
            msgText(obj.GetTypeName() + "VerifySymbol" + "\r\n" + "bool = " + boolRC.ToStr());

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

         if (gmRTL.Core.GlobalException.Instance.Number != 0)
         {
            msgText(FUNCNAME + ": " + gmRTL.Core.GlobalException.Instance.Description);
         }
         else
         {
            msgText(FUNCNAME + ": " + "Completed");
         }

      }
      private void msgText(string str)
      {
         if (str == CLSTEXT)
         {
            Text1.Text = "";
         }
         else
         {
            string rpt = "";
            rpt = "-------------------------------------------" + "\r\n" + str + "\r\n";
            writeLog(rpt);
            System.Diagnostics.Debug.WriteLine(rpt);
            Text1.Text = Text1.Text + "\r\n" + rpt;
         }
      }
      private void Form_Load(object sender, EventArgs e)
      {
         if (gmRTL.Core.InteractionHelper.Command().Like("/batch*"))
         {
            RunTests();
            System.Windows.Forms.Application.DoEvents();
            // End
         }
      }
      public void writeLog(string rpt)
      {
         System.IO.StreamWriter txt = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();


            txt = (new System.IO.FileInfo(logPath)).AppendText();
            txt.WriteLine(rpt);

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

         txt.Close();

         txt = null;

      }
      public string readLog(string logPath)
      {
         string readLog = "";

         System.IO.StreamReader txt = null;
         try
         {
            gmRTL.Core.GlobalException.Clear();

            txt = new System.IO.StreamReader(new System.IO.FileStream(logPath,System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.ReadWrite));
            if ((txt.Peek() == -1))
            {
               readLog = logPath + "  is empty";
            }
            else
            {
               readLog = txt.ReadToEnd();
            }
         }
         catch(System.Exception exc)
         {
            gmRTL.Core.GlobalException.Initialize(exc);
         }

         txt.Close();

         txt = null;
         return readLog;
      }
   }
}
