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

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

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ShiftMaster>().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 ShiftMaster_AddNew(string ShiftName,string ShiftHours, string IsActive, string UserID, string FinalYear, string Status, ref string errorDesc)
        {
            errorDesc = "";

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

            var paramShiftName = new SqlParameter("pShiftName", ShiftName);
            var paramShiftHours = new SqlParameter("pShiftHours", ShiftHours);
            var paramIsActive = new SqlParameter("pIsActive", IsActive);
            var paramUserID = new SqlParameter("pUserID", UserID);
            var paramFinalYear = new SqlParameter("pFinalYear", FinalYear);
            var paramStatus = new SqlParameter("pStatus", Status);



            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_ShiftMaster_AddNew @pShiftName,@pShiftHours,@pIsActive,@pUserID,@pFinalYear,@pStatus",
                                                ReturnValueParam, paramShiftName, paramShiftHours, paramIsActive, paramUserID, paramFinalYear, paramStatus);

                    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 ShiftMaster_Edit(int Shift_ID, string ShiftName, string ShiftHours, string IsActive, string UserID, string FinalYear, string Status, ref string errorDesc)
        {
            errorDesc = "";

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

            var paramShift_ID = new SqlParameter("pShift_ID", Shift_ID);
            var paramShiftName = new SqlParameter("pShiftName", ShiftName);
            var paramShiftHours = new SqlParameter("pShiftHours", ShiftHours);
            var paramIsActive = new SqlParameter("pIsActive", IsActive);
            var paramUserID = new SqlParameter("pUserID", UserID);
            var paramFinalYear = new SqlParameter("pFinalYear", FinalYear);
            var paramStatus = new SqlParameter("pStatus", Status);



            var dbContext = this.Database;

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

                try
                {
                    dbContext.ExecuteSqlCommand("Exec @ReturnValue = Sp_ShiftMaster_Edit @pShift_ID,@pShiftName,@pShiftHours,@pIsActive,@pUserID,@pFinalYear,@pStatus",
                                                ReturnValueParam, paramShift_ID, paramShiftName, paramShiftHours, paramIsActive, paramUserID, paramFinalYear, paramStatus);

                    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<ShiftMaster> ShiftMaster_GetAll(int Shift_ID)
        {
            var paramShift_ID = new SqlParameter("pShift_ID", Shift_ID);
         


            return this.Database.SqlQuery<ShiftMaster>(
                            "Sp_ShiftMaster_GetAll @pShift_ID",
                            paramShift_ID).ToList();
        }

    }
}