using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using MySql.Data.MySqlClient;

namespace fatd_faturamento.DAL.Conexao
{
    public class GerenciadorTransacaoMySql : IGerenciadorTransacao
    {

        private MySqlConnection conexao;
        private MySqlTransaction transacao;

        public IDbConnection Conexao
        {
            get
            {
                if (this.conexao == null)
                {

                    this.conexao = new MySqlConnection(ConfigurationManager.ConnectionStrings["cnnfatd_faturamento"].ConnectionString.Replace("#SERVER#", FabricaGerenciadorTransacao.Servidor).Replace("#USER#", FabricaGerenciadorTransacao.LoginBanco).Replace("#PSW#", FabricaGerenciadorTransacao.SenhaBanco).Replace("#TAB#", FabricaGerenciadorTransacao.Catalogo).Replace("#PORT#", FabricaGerenciadorTransacao.PortaServidor));

                }
                return conexao;
            }
        }

        public GerenciadorTransacaoMySql()
        {
            try
            {
                if (this.Conexao.State == ConnectionState.Closed)
                {
                    this.Conexao.Open();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        #region "Métodos de gerência de transações com banco de dados"

        public void IniciarTransacao()
        {
            if (this.Conexao.State == ConnectionState.Closed)
                this.Conexao.Open();

            this.transacao = ((MySqlConnection)this.Conexao).BeginTransaction();
        }

        public void Commit()
        {
            this.transacao.Commit();
        }

        public void Rollback()
        {
            this.transacao.Rollback();
        }

        #endregion

        public DataTable GetDataTable(IDbCommand cmd)
        {
            ((MySqlCommand)cmd).Connection = (MySqlConnection)this.Conexao;
            DataTable dt = null;
            MySqlDataAdapter da = null;

            try
            {
                if (this.transacao != null)
                {
                    cmd.Transaction = this.transacao;
                }

                da = new MySqlDataAdapter((MySqlCommand)cmd);

                dt = new DataTable();

                da.Fill(dt);

                return dt;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public DataSet GetDataSet(IDbCommand cmd)
        {
            ((MySqlCommand)cmd).Connection = (MySqlConnection)this.Conexao;
            DataSet ds = null;
            MySqlDataAdapter da = null;

            try
            {
                if (this.transacao != null)
                    cmd.Transaction = this.transacao;

                da = new MySqlDataAdapter((MySqlCommand)cmd);

                ds = new DataSet();

                da.Fill(ds);

                return ds;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public object ExecuteScalar(IDbCommand cmd)
        {
            cmd.Connection = (MySqlConnection)this.Conexao;

            try
            {
                if (this.transacao != null)
                    cmd.Transaction = this.transacao;

                return cmd.ExecuteScalar();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public object Execute(IDbCommand cmd)
        {
            cmd.Connection = (MySqlConnection)this.Conexao;

            try
            {
                if (this.transacao != null)
                    cmd.Transaction = this.transacao;

                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public void Dispose()
        {
            if (this.Conexao.State == ConnectionState.Open)
                this.Conexao.Close();

            if (this.transacao != null)
                this.transacao.Dispose();

            this.Conexao.Dispose();

            GC.SuppressFinalize(this);
        }

        ~GerenciadorTransacaoMySql()
        {
            this.Dispose();
        }


    }
}
