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

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

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<DespatchEntry>().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 DespatchEntry_AddNew(string UnitName,DateTime date,string itemName,string qty,string VendorName,string Remarks,DateTime CreatedDate, 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 paramUnitName = new SqlParameter("pUnitName", UnitName);
            var paramdate = new SqlParameter("pdate", date);
            var paramitemName = new SqlParameter("pitemName", itemName);
            var paramqty = new SqlParameter("pqty", qty);
            var paramVendorName = new SqlParameter("pVendorName", VendorName);
            var paramRemarks = new SqlParameter("pRemarks", Remarks);
            var paramCreatedDate = new SqlParameter("pCreatedDate", CreatedDate);
            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_DespatchEntry_AddNew @pUnitName,@pdate,@pitemName,@pqty,@pVendorName,@pRemarks,@pCreatedDate,@pIsActive,@pUserID,@pFinalYear,@pStatus",
                                                ReturnValueParam, paramUnitName, paramdate, paramitemName, paramqty, paramVendorName, paramRemarks, paramCreatedDate, 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 DespatchEntry_Edit(int DespatchId, string UnitName, DateTime date, string itemName, string qty, string VendorName, string Remarks, 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 paramDespatchId = new SqlParameter("pDespatchId", DespatchId);
            var paramUnitName = new SqlParameter("pUnitName", UnitName);
            var paramdate = new SqlParameter("pdate", date);
            var paramitemName = new SqlParameter("pitemName", itemName);
            var paramqty = new SqlParameter("pqty", qty);
            var paramVendorName = new SqlParameter("pVendorName", VendorName);
            var paramRemarks = new SqlParameter("pRemarks", Remarks);
     
            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_DespatchEntry_Edit @pDespatchId,@pUnitName,@pdate,@pitemName,@pqty,@pVendorName,@pRemarks,@pIsActive,@pUserID,@pFinalYear,@pStatus",
                                                ReturnValueParam, paramDespatchId, paramUnitName, paramdate, paramitemName, paramqty, paramVendorName, paramRemarks, 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<DespatchEntry> DespatchEntry_GetAll(int DespatchId)
        {
            var paramDespatchId = new SqlParameter("pDespatchId", DespatchId);
          

            return this.Database.SqlQuery<DespatchEntry>(
                            "Sp_DespatchEntry_GetAll @pDespatchId",
                            paramDespatchId).ToList();
        }
    }
}