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

namespace MarketData.DataAccess
{
	public class EconomicIndicatorDA
	{
		private EconomicIndicatorDA()
		{
		}
    public static EconomicIndicators GetEconomicIndicators()
    {
      MySqlConnection sqlConnection=null;
      MySqlCommand sqlCommand=null;
      MySqlDataReader sqlDataReader=null;
      EconomicIndicators economicIndicators=new EconomicIndicators();
		  try
			{
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        sqlCommand=sqlConnection.CreateCommand();
        StringBuilder sb=new StringBuilder();
        sb.Append("select country_code,country_name,indicator_code,indicator_name,indicator_value,year,source from EconomicIndicators");
        sqlCommand.CommandText=sb.ToString();
        sqlDataReader=sqlCommand.ExecuteReader();
        while(sqlDataReader.Read())
        {
          EconomicIndicator economicIndicator=new EconomicIndicator();
          if(!sqlDataReader.IsDBNull(0))economicIndicator.CountryCode=sqlDataReader.GetString(0);
          if(!sqlDataReader.IsDBNull(1))economicIndicator.CountryName=sqlDataReader.GetString(1);
          if(!sqlDataReader.IsDBNull(2))economicIndicator.IndicatorCode=sqlDataReader.GetString(2);
          if(!sqlDataReader.IsDBNull(3))economicIndicator.IndicatorName=sqlDataReader.GetString(3);
          if(!sqlDataReader.IsDBNull(4))economicIndicator.IndicatorValue=sqlDataReader.GetDouble(4);
          if(!sqlDataReader.IsDBNull(5))economicIndicator.Year=sqlDataReader.GetInt32(5);
          if(!sqlDataReader.IsDBNull(6))economicIndicator.Source=sqlDataReader.GetString(6);
          economicIndicators.Add(economicIndicator);
        }
        sqlDataReader.Close();
        sqlDataReader.Dispose();
        sqlCommand.Dispose();
        sqlConnection.Close();
        sqlConnection.Dispose();
			  return economicIndicators;
			}
			catch(Exception exception)
			{
        MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString()));
			  return null;
			}
      finally
      {
        if(null!=sqlDataReader)
        {
          sqlDataReader.Close();
          sqlDataReader.Dispose();
        }
        if(null!=sqlCommand)
        {
          sqlCommand.Dispose();
        }
        if(null!=sqlConnection)
        {
          sqlConnection.Close();
          sqlConnection.Dispose();
        }
      }
    }
		public static bool InsertUpdateEconomicIndicators(EconomicIndicators economicIndicators)
		{
      MySqlConnection sqlConnection=null;
      MySqlCommand sqlCommand=null;
      MySqlTransaction sqlTransaction=null;
		  try
			{
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted);
        sqlCommand=sqlConnection.CreateCommand();
        sqlCommand.Transaction=sqlTransaction;
			  List<String> distinctCountryCodes=(from EconomicIndicator economicIndicator in economicIndicators select economicIndicator.CountryCode).Distinct().ToList();
        for(int index=0;index<distinctCountryCodes.Count;index++)
        {
          String countryCode=distinctCountryCodes[index];
          List<int> years=(from EconomicIndicator economicIndicator in economicIndicators select economicIndicator.Year).Distinct().ToList();
          DeleteEconomicIndicator(countryCode,years,sqlCommand,sqlTransaction);
        }
        InsertEconomicIndicators(economicIndicators,sqlCommand,sqlTransaction);
        sqlTransaction.Commit();
        sqlTransaction.Dispose();
        sqlCommand.Dispose();
        sqlConnection.Close();
        sqlConnection.Dispose();
        sqlConnection=null;
        sqlCommand=null;
        sqlTransaction=null;
			  return true;
			}
			catch(Exception exception)
			{
        MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString()));
			  return false;
			}
      finally
      {
        if(null!=sqlConnection)
        {
          sqlConnection.Close();
          sqlConnection.Dispose();
        }
        if(null!=sqlCommand)
        {
          sqlCommand.Dispose();
        }
        if(null!=sqlTransaction)
        {
          sqlTransaction.Dispose();
        }
      }
		}
		public static bool DeleteEconomicIndicator(String countryCode,List<int> years,MySqlCommand sqlCommand,MySqlTransaction sqlTransaction)
		{
      String strQuery=null;
		  try
			{
        StringBuilder sb=new StringBuilder();
        sb.Append("delete from EconomicIndicators ").Append(" where ");
        sb.Append("country_code=").Append("'").Append(countryCode).Append("'").Append(" and ");
        sb.Append("year in ").Append(SqlUtils.CreateInClause(years));
        strQuery=sb.ToString();
        sqlCommand.CommandText=strQuery;
        sqlCommand.ExecuteNonQuery();
        return true;
			}
			catch(Exception exception)
			{
        MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}, query was {1}",exception.ToString(),strQuery));
			  return false;
			}
		}
    public static bool InsertEconomicIndicators(EconomicIndicators economicIndicators,MySqlCommand sqlCommand,MySqlTransaction sqlTransaction)
    {
      String strQuery=null;
      try
      {
        StringBuilder sb=new StringBuilder();
        foreach(EconomicIndicator economicIndicator in economicIndicators)
        {
          sb.Append("insert into EconomicIndicators(country_code,country_name,indicator_code,indicator_name,indicator_value,year,source)values(");
          sb.Append(SqlUtils.ToSqlString(economicIndicator.CountryCode)).Append(",");
          sb.Append(SqlUtils.ToSqlString(economicIndicator.CountryName)).Append(",");
          sb.Append(SqlUtils.ToSqlString(economicIndicator.IndicatorCode)).Append(",");
          sb.Append(SqlUtils.ToSqlString(economicIndicator.IndicatorName)).Append(",");
          sb.Append(economicIndicator.IndicatorValue).Append(",");
          sb.Append(economicIndicator.Year).Append(",");
          sb.Append(SqlUtils.ToSqlString(economicIndicator.Source));
          sb.Append(");");
        }
        strQuery=sb.ToString();
        sqlCommand.CommandText=sb.ToString();
        sqlCommand.ExecuteNonQuery();
        return true;
      }
      catch(Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString()));
			  return false;
      }
      finally
      {
      }
    }
	}
}
