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

namespace MarketData.DataAccess
{
  public class DictionaryDA
  {
    private DictionaryDA()
    {
    }
    public static bool TruncateDictionary()
    {
      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);
        StringBuilder sb = new StringBuilder();
        sb.Append("truncate table dictionary");
        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();
      }
    }
    public static bool InsertDictionaryElement(DictionaryElement dictionaryElement)
    {
      DictionaryCollection dictionaryCollection=new DictionaryCollection();
      dictionaryCollection.Add(dictionaryElement);
      InsertDictionaryCollection(dictionaryCollection);
      return true;
    }
    public static bool InsertDictionaryCollection(DictionaryCollection dictionaryCollection)
    {
      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);
        StringBuilder sb = new StringBuilder();
        DateTime modified=DateTime.Now;
        DateTime created=modified;
        foreach(DictionaryElement dictionaryElement in dictionaryCollection)
        {
          sb=new StringBuilder();
          if(ContainsDictionaryElement(dictionaryElement))
          {
            continue;
          }
          sb.Append("insert into dictionary (word,part_of_speech) ");
          sb.Append("values(");
          sb.Append(SqlUtils.ToSqlString(dictionaryElement.Word)).Append(",");
          sb.Append("'").Append(dictionaryElement.PartOfSpeech).Append("'");
          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();
      }
    }
    public static bool ContainsDictionaryElement(DictionaryElement dictionaryElement)
    {
      MySqlConnection sqlConnection = null;
      MySqlDataReader sqlDataReader = null;
			MySqlCommand sqlCommand=null;
      String strQuery = null;
      int recordCount = 0;

      try
      {
        StringBuilder sb = new StringBuilder();
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        sb.Append("select count(*) from dictionary where word=").Append(SqlUtils.ToSqlString(dictionaryElement.Word));
        sb.Append(" and part_of_speech='").Append(dictionaryElement.PartOfSpeech).Append("'");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        if (sqlDataReader.Read())
        {
          if (!sqlDataReader.IsDBNull(0)) recordCount = sqlDataReader.GetInt32(0);
        }
				sqlCommand.Dispose();
        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 bool UpdateDictionaryElement(DictionaryElement dictionaryElement)
    {
      DictionaryCollection dictionaryCollection=new DictionaryCollection();
      dictionaryCollection.Add(dictionaryElement);
      return UpdateDictionaryElements(dictionaryCollection);
    }
    public static bool UpdateDictionaryElements(DictionaryCollection dictionaryCollection)
    {
      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);
        foreach(DictionaryElement dictionaryElement in dictionaryCollection)
        {
          if(!ContainsDictionaryElement(dictionaryElement))continue;
          if(null==dictionaryElement.PartOfSpeech||0==dictionaryElement.PartOfSpeech.Length)continue;
          StringBuilder sb = new StringBuilder();
          DateTime modified=DateTime.Now;
          sb.Append("update dictionary set ");
          sb.Append("part_of_speech=").Append("'").Append(dictionaryElement.PartOfSpeech).Append("'");
          sb.Append(" where word=").Append(SqlUtils.ToSqlString(dictionaryElement.Word));
          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();
      }
    }
    public static DictionaryCollection GetWord(String word)
    {
      MySqlConnection sqlConnection = null;
      MySqlCommand sqlCommand=null; 
      MySqlDataReader sqlDataReader=null;
      DictionaryCollection dictionaryCollection=new DictionaryCollection();
      String strQuery = null;

      try
      {
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        StringBuilder sb = new StringBuilder();
        DateTime modified=DateTime.Now;
        sb.Append("select word,part_of_speech from dictionary ");
        sb.Append(" where word=").Append(SqlUtils.ToSqlString(word));
        sb.Append(" order by word desc");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader=sqlCommand.ExecuteReader();
        while(sqlDataReader.Read())
        {
          DictionaryElement dictionaryElement=new DictionaryElement();
          dictionaryElement.Word=sqlDataReader.GetString(0);
          if(!sqlDataReader.IsDBNull(1))dictionaryElement.PartOfSpeech=sqlDataReader.GetString(1);
          dictionaryCollection.Add(dictionaryElement);
        }
        return dictionaryCollection;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        MDTrace.WriteLine(LogLevel.DEBUG,"Query was '" + strQuery + "'");
        return null;
      }
      finally
      {
        if(null!=sqlCommand)sqlCommand.Dispose();
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static DictionaryCollection GetPartsOfSpeech(String partOfSpeech)
    {
      MySqlConnection sqlConnection = null;
      MySqlCommand sqlCommand=null; 
      MySqlDataReader sqlDataReader=null;
      DictionaryCollection dictionaryCollection=new DictionaryCollection();
      String strQuery = null;

      try
      {
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        StringBuilder sb = new StringBuilder();
        DateTime modified=DateTime.Now;
        sb.Append("select word,part_of_speech from dictionary ");
        sb.Append(" where part_of_speech='").Append(partOfSpeech).Append("'");
        sb.Append(" order by word desc");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader=sqlCommand.ExecuteReader();
        while(sqlDataReader.Read())
        {
          DictionaryElement dictionaryElement=new DictionaryElement();
          dictionaryElement.Word=sqlDataReader.GetString(0);
          if(!sqlDataReader.IsDBNull(1))dictionaryElement.PartOfSpeech=sqlDataReader.GetString(1);
          dictionaryCollection.Add(dictionaryElement);
        }
        return dictionaryCollection;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        MDTrace.WriteLine(LogLevel.DEBUG,"Query was '" + strQuery + "'");
        return null;
      }
      finally
      {
        if(null!=sqlCommand)sqlCommand.Dispose();
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    public static DictionaryCollection GetDictionaryCollection()
    {
      MySqlConnection sqlConnection = null;
      MySqlCommand sqlCommand=null; 
      MySqlDataReader sqlDataReader=null;
      DictionaryCollection dictionaryCollection=new DictionaryCollection();
      String strQuery = null;

      try
      {
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data"));
        StringBuilder sb = new StringBuilder();
        DateTime modified=DateTime.Now;
        sb.Append("select word,part_of_speech from dictionary ");
        sb.Append(" order by word desc");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader=sqlCommand.ExecuteReader();
        while(sqlDataReader.Read())
        {
          DictionaryElement dictionaryElement=new DictionaryElement();
          dictionaryElement.Word=sqlDataReader.GetString(0);
          dictionaryElement.PartOfSpeech=sqlDataReader.GetString(1);
          dictionaryCollection.Add(dictionaryElement);
        }
        return dictionaryCollection;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        MDTrace.WriteLine(LogLevel.DEBUG,"Query was '" + strQuery + "'");
        return null;
      }
      finally
      {
        if(null!=sqlCommand)sqlCommand.Dispose();
        if (null != sqlConnection) sqlConnection.Close();
      }
    }
    private static bool DeleteDictionaryCollection(DictionaryCollection dictionaryCollection, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
    {
      foreach(DictionaryElement dictionaryElement in dictionaryCollection)DeleteDictionaryElement(dictionaryElement,sqlConnection,sqlTransaction);
      return true;
    }
    private static bool DeleteDictionaryElement(DictionaryElement dictionaryElement, MySqlConnection sqlConnection, MySqlTransaction sqlTransaction)
    {
      StringBuilder sb = new StringBuilder();
      String strQuery = null;

      try
      {
        sb.Append("delete from dictionary where ");
        sb.Append("word=").Append(SqlUtils.ToSqlString(dictionaryElement.Word));
        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
      {
      }
    }
  }
}
