using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using MarketData.MarketDataModel;
using MarketData.Utils;

namespace MarketData.DataAccess
{
  public class DividendPaymentDA
  {
    private DividendPaymentDA()
    {
    }
    public static DividendPayments GetDividendPayments()
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand =null;
      DividendPayments dividendPayments=new DividendPayments();
      String strQuery = null;

      try
      {
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
        StringBuilder sb = new StringBuilder();
        sb.Append("select symbol,payment_date,account,amount from dividends order by payment_date desc, symbol asc;");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        while (sqlDataReader.Read())
        {
          DividendPayment dividendPayment=new DividendPayment();
          dividendPayment.Symbol = sqlDataReader.GetString(0);
          dividendPayment.PaymentDate=sqlDataReader.GetDateTime(1);
          dividendPayment.Account=sqlDataReader.GetString(2);
          dividendPayment.Amount=sqlDataReader.GetDouble(3);
          dividendPayments.Add(dividendPayment);
        }
        sqlCommand.Dispose();
        sqlDataReader.Close();
        return dividendPayments;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        return null;
      }
      finally
      {
				if(null!=sqlCommand)sqlCommand.Dispose();
				if(null!=sqlDataReader)sqlDataReader.Dispose();
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static DividendPayments GetDividendPaymentsForSymbol(String symbol)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand =null;
      DividendPayments dividendPayments=new DividendPayments();
      String strQuery = null;

      try
      {
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
        StringBuilder sb = new StringBuilder();
        sb.Append("select symbol,payment_date,account,amount from dividends ");
        sb.Append("where symbol=").Append("'").Append(symbol).Append("'");
        sb.Append(" order by payment_date desc, symbol asc;");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        while (sqlDataReader.Read())
        {
          DividendPayment dividendPayment=new DividendPayment();
          dividendPayment.Symbol = sqlDataReader.GetString(0);
          dividendPayment.PaymentDate=sqlDataReader.GetDateTime(1);
          dividendPayment.Account=sqlDataReader.GetString(2);
          dividendPayment.Amount=sqlDataReader.GetDouble(3);
          dividendPayments.Add(dividendPayment);
        }
        sqlCommand.Dispose();
        sqlDataReader.Close();
        return dividendPayments;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        return null;
      }
      finally
      {
				if(null!=sqlCommand)sqlCommand.Dispose();
				if(null!=sqlDataReader)sqlDataReader.Dispose();
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static DividendPayments GetDividendPaymentsForAccount(String account)
    {
      List<String> accounts = new List<String>();
      accounts.Add(account);
      return GetDividendPaymentsForAccounts(accounts);
    }
    public static DividendPayments GetDividendPaymentsForAccounts(List<String> accounts)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
      MySqlCommand sqlCommand = null;
      DividendPayments dividendPayments = new DividendPayments();
      String strQuery = null;

      try
      {
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
        StringBuilder sb = new StringBuilder();
        sb.Append("select symbol,payment_date,account,amount from dividends ");
        sb.Append("where account in ").Append(SqlUtils.CreateInClause(accounts));
        sb.Append(" order by payment_date desc, symbol asc;");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        while (sqlDataReader.Read())
        {
          DividendPayment dividendPayment = new DividendPayment();
          dividendPayment.Symbol = sqlDataReader.GetString(0);
          dividendPayment.PaymentDate = sqlDataReader.GetDateTime(1);
          dividendPayment.Account = sqlDataReader.GetString(2);
          dividendPayment.Amount = sqlDataReader.GetDouble(3);
          dividendPayments.Add(dividendPayment);
        }
        sqlCommand.Dispose();
        sqlDataReader.Close();
        return dividendPayments;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG, exception);
        return null;
      }
      finally
      {
        if (null != sqlCommand) sqlCommand.Dispose();
        if (null != sqlDataReader) sqlDataReader.Dispose();
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static DividendPayments GetDividendPaymentsForAccountsAndSymbols(List<String> accounts,List<String> symbols)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
      MySqlCommand sqlCommand = null;
      DividendPayments dividendPayments = new DividendPayments();
      String strQuery = null;

      try
      {
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
        StringBuilder sb = new StringBuilder();
        sb.Append("select symbol,payment_date,account,amount from dividends ");
        if ((null == accounts || 0 == accounts.Count) && (null == symbols || 0 == symbols.Count)) return GetDividendPayments();
        if (!accounts.Contains(Constants.CONST_ALL)&&!accounts.Contains(""))
        {
          sb.Append("where account in ").Append(SqlUtils.CreateInClause(accounts)).Append(" ");
          if (!symbols.Contains(Constants.CONST_ALL)&&0!=symbols.Count&&!symbols.Contains("")) sb.Append("and symbol in ").Append(SqlUtils.CreateInClause(symbols)).Append(" ");
        }
        else if (!symbols.Contains(Constants.CONST_ALL)&&0!=symbols.Count&&!symbols.Contains("")) sb.Append("where symbol in ").Append(SqlUtils.CreateInClause(symbols)).Append(" ");
        sb.Append(" order by payment_date desc, account, symbol asc;");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        while (sqlDataReader.Read())
        {
          DividendPayment dividendPayment = new DividendPayment();
          dividendPayment.Symbol = sqlDataReader.GetString(0);
          dividendPayment.PaymentDate = sqlDataReader.GetDateTime(1);
          dividendPayment.Account = sqlDataReader.GetString(2);
          dividendPayment.Amount = sqlDataReader.GetDouble(3);
          dividendPayments.Add(dividendPayment);
        }
        sqlCommand.Dispose();
        sqlDataReader.Close();
        return dividendPayments;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG, exception);
        return null;
      }
      finally
      {
        if (null != sqlCommand) sqlCommand.Dispose();
        if (null != sqlDataReader) sqlDataReader.Dispose();
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static DividendPayments GetDividendPaymentsMaxDate(String symbol,DateTime maxDate)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand =null;
      DividendPayments dividendPayments=new DividendPayments();
      String strQuery = null;

      try
      {
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
        StringBuilder sb = new StringBuilder();
        sb.Append("select symbol,payment_date,account,amount from dividends ");
        sb.Append("where symbol=").Append("'").Append(symbol).Append("'");
        sb.Append(" and payment_date<=").Append("'").Append(SqlUtils.ToSqlDateTime(maxDate)).Append("'");
        sb.Append(" order by payment_date desc, symbol asc;");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        while (sqlDataReader.Read())
        {
          DividendPayment dividendPayment=new DividendPayment();
          dividendPayment.Symbol = sqlDataReader.GetString(0);
          dividendPayment.PaymentDate=sqlDataReader.GetDateTime(1);
          dividendPayment.Account=sqlDataReader.GetString(2);
          dividendPayment.Amount=sqlDataReader.GetDouble(3);
          dividendPayments.Add(dividendPayment);
        }
        sqlCommand.Dispose();
        sqlDataReader.Close();
        return dividendPayments;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        return null;
      }
      finally
      {
				if(null!=sqlCommand)sqlCommand.Dispose();
				if(null!=sqlDataReader)sqlDataReader.Dispose();
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
// ***********
    public static List<String> GetDividendPaymentAccounts()
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
      MySqlCommand sqlCommand = null;
      List<String> accounts = new List<String>();
      String strQuery = null;

      try
      {
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
        StringBuilder sb = new StringBuilder();
        sb.Append("select distinct(account) from dividends ");
        sb.Append(" order by account asc;");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        while (sqlDataReader.Read())
        {
          accounts.Add(sqlDataReader.GetString(0));
        }
        sqlCommand.Dispose();
        sqlDataReader.Close();
        return accounts;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG, exception);
        return null;
      }
      finally
      {
        if (null != sqlCommand) sqlCommand.Dispose();
        if (null != sqlDataReader) sqlDataReader.Dispose();
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static List<String> GetDividendPaymentSymbols()
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
      MySqlCommand sqlCommand = null;
      List<String> symbols = new List<String>();
      String strQuery = null;

      try
      {
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
        StringBuilder sb = new StringBuilder();
        sb.Append("select distinct(symbol) from dividends ");
        sb.Append(" order by symbol asc;");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        while (sqlDataReader.Read())
        {
          symbols.Add(sqlDataReader.GetString(0));
        }
        sqlCommand.Dispose();
        sqlDataReader.Close();
        return symbols;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG, exception);
        return null;
      }
      finally
      {
        if (null != sqlCommand) sqlCommand.Dispose();
        if (null != sqlDataReader) sqlDataReader.Dispose();
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static List<String> GetDividendPaymentSymbols(String account)
    {
      List<String> accounts = new List<String>();
      accounts.Add(account);
      return GetDividendPaymentSymbols(accounts);
    }
    public static List<String> GetDividendPaymentSymbols(List<String> accounts)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
      MySqlCommand sqlCommand = null;
      List<String> symbols = new List<String>();
      String strQuery = null;

      try
      {
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("portfolio_data"));
        StringBuilder sb = new StringBuilder();
        sb.Append("select distinct(symbol) from dividends where account in ").Append(SqlUtils.CreateInClause(accounts));
        sb.Append(" order by symbol asc;");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        while (sqlDataReader.Read())
        {
          symbols.Add(sqlDataReader.GetString(0));
        }
        sqlCommand.Dispose();
        sqlDataReader.Close();
        return symbols;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG, exception);
        return null;
      }
      finally
      {
        if (null != sqlCommand) sqlCommand.Dispose();
        if (null != sqlDataReader) sqlDataReader.Dispose();
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
  }
}
