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

namespace MarketData.DataAccess
{
  public class AnalystPriceTargetDA
  {
    private AnalystPriceTargetDA()
    {
    }
    public static bool CheckAnalystPriceTargetModifiedOn(String symbol,DateTime modified)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand =null;
      String strQuery = null;
      DateTime maxDate = DateTime.Parse("01-01-0001");

      try
      {
        StringBuilder sb = new StringBuilder();
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        sb.Append("select count(*) from analystpricetarget").Append(" ");
        sb.Append("where symbol='").Append(symbol).Append("'").Append(" ");
        sb.Append("and date='").Append(Utility.DateTimeToStringYYYYHMMHDD(modified)).Append("'");
        strQuery = sb.ToString(); ;
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        sqlDataReader.Read();
        int recordCount = sqlDataReader.GetInt32(0);
        return 0 == recordCount ? false : true;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        return false;
      }
      finally
      {
				if(null!=sqlCommand)sqlCommand.Dispose();
        if (null != sqlDataReader) sqlDataReader.Close();
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static AnalystPriceTarget GetAnalystPriceTarget(String symbol)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand=null;
      String strQuery = null;

      try
      {
        StringBuilder sb = new StringBuilder();
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        sb.Append("select date,symbol,mean_target,median_target,high_target,low_target from analystpricetarget where symbol=");
        sb.Append("'").Append(symbol).Append("'").Append(" ");
        sb.Append("and date=").Append(" (select max(date) from analystpricetarget where symbol='").Append(symbol).Append("')");
        strQuery = sb.ToString(); ;
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        if (!sqlDataReader.Read()) return null;
        AnalystPriceTarget analystPriceTarget = new AnalystPriceTarget();
        analystPriceTarget.Date = sqlDataReader.GetDateTime(0);
        analystPriceTarget.Symbol = sqlDataReader.GetString(1);
        if (!sqlDataReader.IsDBNull(2))analystPriceTarget.MeanTargetPrice = sqlDataReader.GetDouble(2);
        if (!sqlDataReader.IsDBNull(2))analystPriceTarget.MedianTargetPrice = sqlDataReader.GetDouble(3);
        if (!sqlDataReader.IsDBNull(2))analystPriceTarget.HighTargetPrice = sqlDataReader.GetDouble(4);
        if (!sqlDataReader.IsDBNull(2))analystPriceTarget.LowTargetPrice = sqlDataReader.GetDouble(5);
        return analystPriceTarget;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        return null;
      }
      finally
      {
				if(null!=sqlCommand)sqlCommand.Dispose();
        if (null != sqlDataReader) sqlDataReader.Close();
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static bool InsertAnalystPriceTarget(AnalystPriceTarget analystPriceTarget)
    {
      MySqlConnection sqlConnection = null;
      MySqlTransaction sqlTransaction = null;
			MySqlCommand sqlCommand=null;
      String strQuery = null;

      try
      {
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
        DeleteAnalystPriceTarget(analystPriceTarget, sqlConnection, sqlTransaction);
        sqlTransaction.Commit();
        sqlTransaction = sqlConnection.BeginTransaction();
        StringBuilder sb = new StringBuilder();
        sb.Append("insert into analystpricetarget (date,symbol,mean_target,median_target,high_target,low_target) ");
        sb.Append("values(");
        sb.Append("'").Append(Utility.DateTimeToStringYYYYHMMHDD(analystPriceTarget.Date)).Append("'").Append(",");
        sb.Append("'").Append(analystPriceTarget.Symbol).Append("'").Append(",");
        if (!Double.IsNaN(analystPriceTarget.MeanTargetPrice)) sb.Append(analystPriceTarget.MeanTargetPrice).Append(",");
        else sb.Append("null").Append(",");
        if (!Double.IsNaN(analystPriceTarget.MedianTargetPrice)) sb.Append(analystPriceTarget.MedianTargetPrice).Append(",");
        else sb.Append("null").Append(",");
        if (!Double.IsNaN(analystPriceTarget.HighTargetPrice)) sb.Append(analystPriceTarget.HighTargetPrice).Append(",");
        else sb.Append("null").Append(",");
        if (!Double.IsNaN(analystPriceTarget.LowTargetPrice)) sb.Append(analystPriceTarget.LowTargetPrice);
        else sb.Append("null");
        sb.Append(")");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlCommand.ExecuteNonQuery();
        sqlTransaction.Commit();
        return true;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        MDTrace.WriteLine(LogLevel.DEBUG,"Query was '" + strQuery + "'");
        return false;
      }
      finally
      {
				if(null!=sqlCommand)sqlCommand.Dispose();
				if(null!=sqlTransaction)sqlTransaction.Dispose();
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    private static bool DeleteAnalystPriceTarget(AnalystPriceTarget analystPriceTarget, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
    {
      StringBuilder sb = new StringBuilder();
      String strQuery = null;

      try
      {
        sb.Append("delete from analystpricetarget where ");
        sb.Append("symbol='").Append(analystPriceTarget.Symbol).Append("'");
        sb.Append(" and ");
        sb.Append("date='").Append(Utility.DateTimeToStringYYYYHMMHDD(analystPriceTarget.Date)).Append("'");
        strQuery = sb.ToString();
        MySqlCommand sqlCommand = new MySqlCommand(strQuery, sqlConnection, sqlTransaction);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlCommand.ExecuteNonQuery();
				sqlCommand.Dispose();
        return true;
      }
      catch (Exception exception)
      {
        sqlTransaction.Rollback();
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        if (null != strQuery) MDTrace.WriteLine(LogLevel.DEBUG,"Query was " + strQuery);
        return false;
      }
      finally
      {
      }
    }
  }
}
