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 REJECTIONREASONDbContext:DbContext
    {
        public REJECTIONREASONDbContext() : base()
        {
            Database.SetInitializer<REJECTIONREASONDbContext>(null);
            base.Database.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["RNGConnectionStrings"].ConnectionString;
        }

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

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<REJECTIONREASON>().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 REJECTIONREASON_AddNew(string ReasonName, string IsActive, string Status, string FinalYear, ref string errorDesc)
        {
            errorDesc = "";

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



            var paramReasonName = new SqlParameter("pReasonName", ReasonName);


            var paramIsActive = new SqlParameter("pIsActive", IsActive);

            var paramStatus = new SqlParameter("pStatus", Status);

            var paramFinalYear = new SqlParameter("pFinalYear", FinalYear);

         



            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_Rejectionreason_AddNew @pReasonName,@pIsActive,@pStatus,@pFinalYear",
                                                ReturnValueParam, paramReasonName, paramIsActive, paramStatus, paramFinalYear);

                    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;
            }
        }

        public virtual int REJECTIONREASON_Edit(int RejectionReasionId, string ReasonName, string IsActive, string Status, string FinalYear, ref string errorDesc)
        {
            errorDesc = "";

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

            var paramRejectionReasionId = new SqlParameter("pRejectionReasionId", RejectionReasionId);

            var paramReasonName = new SqlParameter("pReasonName", ReasonName);


            var paramIsActive = new SqlParameter("pIsActive", IsActive);

            var paramStatus = new SqlParameter("pStatus", Status);

            var paramFinalYear = new SqlParameter("pFinalYear", FinalYear);





            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_Rejectionreason_Edit @pRejectionReasionId,@pReasonName,@pIsActive,@pStatus,@pFinalYear",
                                                ReturnValueParam, paramRejectionReasionId, paramReasonName, paramIsActive, paramStatus, paramFinalYear);

                    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;
            }
        }



        public virtual List<REJECTIONREASON> REJECTIONREASON_GetAll(int RejectionReasionId)
        {
            var paramRejectionReasionId = new SqlParameter("pRejectionReasionId", RejectionReasionId);



            return this.Database.SqlQuery<REJECTIONREASON>(
                            "Sp_Rejectionreason_GetAll @pRejectionReasionId",
                            paramRejectionReasionId).ToList();
        }
    }
}