using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RNGClassLibrary;
using System.Data.Entity;
using System.Data.SqlClient;

namespace RNGWcfService1.ContextClass
{
    public class AdminLoginDbContext:DbContext
    {
        public AdminLoginDbContext() : base()
        {
            Database.SetInitializer<AdminLoginDbContext>(null);
            base.Database.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["RNGConnectionStrings"].ConnectionString;
        }

        public System.Data.Entity.DbSet<AdminLogin> User { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<AdminLogin>().MapToStoredProcedures();
        }


        public void GetErrorDescription(int ErrorNo, ref string ErrorDesc)
        {
            var pErrCodeParam = new SqlParameter("pErrorNo", ErrorNo);

            var pErrDescParam = new SqlParameter("pErrorDesc", System.Data.SqlDbType.VarChar, 200);
            pErrDescParam.Direction = System.Data.ParameterDirection.Output;
            pErrDescParam.Value = "";

            this.Database.ExecuteSqlCommand("spErrors_GetErrorDesc @pErrorNo, @pErrorDesc out", pErrCodeParam, pErrDescParam);

            ErrorDesc = Convert.ToString(pErrDescParam.Value);
        }

        public virtual int Admin_Login(string UserName, string Password, ref int Admin_Id, ref string errorDesc)
        {
            errorDesc = "";
            var ReturnValueParam = new SqlParameter("@ReturnValue", System.Data.SqlDbType.Int);
            ReturnValueParam.Direction = System.Data.ParameterDirection.Output;

            var paramUserName = new SqlParameter("pUserName", UserName);
            var paramPassword = new SqlParameter("pPassword", Password);

            var paramAdmin_Id = new SqlParameter("pAdmin_Id", Admin_Id);
            paramAdmin_Id.Direction = System.Data.ParameterDirection.InputOutput;

         

            var dbContext = this.Database;

            //  using (var dbContextTransaction = dbContext.BeginTransaction())
            //{
            int errorNo = 0;

            try
            {
                dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_Admin_Login @pUserName,@pPassword,@pAdmin_Id out",
                                            ReturnValueParam, paramUserName, paramPassword, paramAdmin_Id);

                errorNo = Convert.ToInt32(ReturnValueParam.Value);

                if (errorNo >= 0) //**** Success
                {
                    //dbContextTransaction.Commit();
                    this.SaveChanges();

                    Admin_Id = Convert.ToInt32(paramAdmin_Id.Value);
                    UserName = Convert.ToString(paramUserName.Value);

                }
                else //**** Error ****
                {
                    // dbContextTransaction.Rollback();
                    GetErrorDescription(errorNo, ref errorDesc);
                }
            }
            catch (Exception ex)
            {
                //dbContextTransaction.Rollback();

                errorNo = -1;
                errorDesc = Convert.ToString(ex.Message);
            }
            return errorNo;
        }

        public virtual int AdminChangeMaster_Edit(int Admin_Id, string Password,ref string errorDesc)
        {
            errorDesc = "";

            var ReturnValueParam = new SqlParameter("@ReturnValue", System.Data.SqlDbType.Int);
            ReturnValueParam.Direction = System.Data.ParameterDirection.Output;

            var paramAdmin_Id = new SqlParameter("pAdmin_Id", Admin_Id);
            var paramPassword = new SqlParameter("pPassword", Password);
           



            var dbContext = this.Database;

            using (var dbContextTransaction = dbContext.BeginTransaction())
            {
                int errorNo = 0;

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_AdminLoginChange_Edit @pAdmin_Id,@pPassword",
                                                  ReturnValueParam, paramAdmin_Id, paramPassword);

                    errorNo = Convert.ToInt32(ReturnValueParam.Value);

                    if (errorNo >= 0) //**** Success
                    {
                        dbContextTransaction.Commit();
                        this.SaveChanges();
                    }
                    else //**** Error ****
                    {
                        dbContextTransaction.Rollback();
                        GetErrorDescription(errorNo, ref errorDesc);
                    }
                }
                catch (Exception ex)
                {
                    dbContextTransaction.Rollback();

                    errorNo = -1;
                    errorDesc = Convert.ToString(ex.Message);
                }
                return errorNo;
            }
        }
    }
}