using System.Text;
using MySql.Data.MySqlClient;
using MarketData.Utils;
using MarketData.MarketDataModel.User;

namespace MarketData.DataAccess
{
  public class UserDA
  {
    private UserDA()
    {
    }

    public static bool UserExists(String username)
    {
      MySqlConnection sqlConnection = null;
			MySqlCommand sqlCommand=null;
      MySqlDataReader sqlDataReader = null;
      String strQuery = null;

      try
      {
        if(String.IsNullOrEmpty(username))return false;
        username = username.ToLower();
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("user_data"));
        StringBuilder sb = new StringBuilder();
        sb.Append("select count(*) from users where username=").Append("'").Append(username).Append("'");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        if(!sqlDataReader.Read())return false;
        int count = sqlDataReader.GetInt32(0);
        return count>0?true:false;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        return false;
      }
      finally
      {
				if(null != sqlCommand)sqlCommand.Dispose();
        if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();}
        if(null != sqlConnection) sqlConnection.Close();
      }
    }

    public static User GetUser(String username)
    {
      MySqlConnection sqlConnection = null;
			MySqlCommand sqlCommand=null;
      MySqlDataReader sqlDataReader = null;
      String strQuery = null;
      User user = new User();

      try
      {
        if(null == username)return null;
        if(!UserExists(username))return null;
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("user_data"));
        StringBuilder sb = new StringBuilder();
        sb.Append("select username, salt, hash from users where ");
        sb.Append("username=").Append("'").Append(username).Append("'");
        sb.Append(";");
        strQuery = sb.ToString();
        sqlCommand = new MySqlCommand(strQuery, sqlConnection);
        sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT;
        sqlDataReader = sqlCommand.ExecuteReader();
        if(sqlDataReader.Read())
        {
          user.Username = sqlDataReader.GetString(0);
          user.Salt = sqlDataReader.GetString(1);
          user.Hash = sqlDataReader.GetString(2);
        }
        return user;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception);
        return null;
      }
      finally
      {
				if(null!=sqlCommand)sqlCommand.Dispose();
        if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();}
        if (null != sqlConnection) sqlConnection.Close();
      }
    }

    public static bool AddUser(User user)
    {
      MySqlConnection sqlConnection = null;
      MySqlTransaction sqlTransaction = null;
			MySqlCommand sqlCommand=null;
      String strQuery = null;

      try
      {
        if(null == user || null == user.Username || null == user.Hash || null == user.Salt)return false;
        if(UserExists(user.Username))return false;
        sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("user_data"));
        sqlTransaction = sqlConnection.BeginTransaction();
        StringBuilder sb = new StringBuilder();
        sb.Append("insert into users(username,hash,salt,created_by,created_on,modified_by,modified_on) values(");
        sb.Append(SqlUtils.AddQuotes(user.Username)).Append(",");
        sb.Append(SqlUtils.AddQuotes(user.Hash)).Append(",");
        sb.Append(SqlUtils.AddQuotes(user.Salt)).Append(",");
        sb.Append("CURRENT_USER").Append(",");
        sb.Append("CURRENT_TIMESTAMP").Append(",");
        sb.Append("CURRENT_USER").Append(",");
        sb.Append("CURRENT_TIMESTAMP").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);
        return false;
      }
      finally
      {
				if(null!=sqlCommand)sqlCommand.Dispose();
        if (null != sqlConnection) sqlConnection.Close();
        if (null != sqlTransaction) sqlTransaction.Dispose();
      }
    }
  }
}
