using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApp_Kendo.Models
{
    public static class EmpRepository
    {
        public static IList<EmployeeListModel> GetAllRecords()
        {
            IList<EmployeeListModel> lstEmp = (IList<EmployeeListModel>)HttpContext.Current.Session["EmpDetails"];
            using (ReckonPayrollEntities dbEmployeeInfo = new ReckonPayrollEntities())
            {
                if (dbEmployeeInfo.DatabaseExists())
                {
                    var employeeList = (from per in dbEmployeeInfo.People
                                        join emp in dbEmployeeInfo.Employees on per.Id equals emp.Id
                                        join tit in dbEmployeeInfo.Titles on per.TitleId equals tit.Id
                                        where emp.IsDeleted == false
                                        select new
                                        {
                                            EmployeeId = emp.Id,
                                            FirstName = per.FirstName,
                                            LastName = per.LastName,
                                            DateOfBirth = per.DateOfBirth,
                                            Reference = emp.Reference,
                                            TitleName = tit.Name,
                                            TitleId = tit.Id,
                                            WeeklyHours = emp.WeeklyHours
                                        }).ToList();

                    //var TitleList = dbEmployeeInfo.Titles.Take(10).ToList();

                    //var objTitle = new Titles();
                    //var lstTitle = new List<Titles>();
                    //foreach (var title in TitleList)
                    //{
                    //    objTitle = new Titles();
                    //    objTitle.Id = title.Id;
                    //    objTitle.Name = title.Name;
                    //    lstTitle.Add(objTitle);
                    //}

                    lstEmp = new List<EmployeeListModel>();
                    var objEmp = new EmployeeListModel();

                    foreach (var emp in employeeList)
                    {
                        objEmp = new EmployeeListModel();

                        objEmp.Id = emp.EmployeeId;

                        objEmp.TitleName = emp.TitleName;
                        objEmp.TitleId = emp.TitleId;
                        //if (string.IsNullOrEmpty(emp.LastName))
                        objEmp.FirstName = emp.FirstName; //emp.FirstName + " , " + emp.LastName;
                        //else
                        //    objEmp.EmployeeName = emp.FirstName + " , " + emp.LastName;

                        objEmp.DateOfBirth = Convert.ToDateTime(emp.DateOfBirth);
                        objEmp.Reference = emp.Reference;
                        objEmp.WeeklyHours = emp.WeeklyHours;
                        //objEmp.Titles = lstTitle;
                        lstEmp.Add(objEmp);
                    }
                    HttpContext.Current.Session["EmpDetails"] = lstEmp;
                }
                return lstEmp;
            }
        }

        public static void Add(EmployeeListModel employeeListModel)
        {
            using (ReckonPayrollEntities db = new ReckonPayrollEntities())
            {
                Person objPerson = new Person();
                objPerson.FirstName = employeeListModel.FirstName;
                objPerson.DateOfBirth = Convert.ToDateTime(employeeListModel.DateOfBirth);
                objPerson.LastName = employeeListModel.FirstName;

                db.AddToPeople(objPerson);

                Employee objEmp = new Employee();
                objEmp.Reference = employeeListModel.Reference;
                db.AddToEmployees(objEmp);

                db.SaveChanges();
            }
            //GetAllRecords().Insert(0, employeeListModel);
        }

        public static void Add(IEnumerable<EmployeeListModel> employeeListModels)
        {
            using (ReckonPayrollEntities db = new ReckonPayrollEntities())
            {
                foreach (var employeeListModel in employeeListModels)
                {
                    Person objPerson = new Person();
                    objPerson.FirstName = employeeListModel.FirstName;
                    objPerson.DateOfBirth = Convert.ToDateTime(employeeListModel.DateOfBirth);
                    objPerson.LastName = employeeListModel.FirstName;
                    objPerson.TitleId = 1;

                    db.AddToPeople(objPerson);

                    Employee objEmp = new Employee();
                    objEmp.Reference = employeeListModel.Reference;
                    db.AddToEmployees(objEmp);

                    db.SaveChanges();
                }
            }
            //foreach (var employeeListModel in employeeListModels)
            //    GetAllRecords().Insert(0, employeeListModel);
        }

        public static EmployeeListModel Select(int? empID)
        {

            EmployeeListModel result = GetAllRecords().Where(o => o.Id == empID).FirstOrDefault();

            return result;
        }

        public static void Delete(int empId)
        {
            using (ReckonPayrollEntities db = new ReckonPayrollEntities())
            {
                Employee objEmp = db.Employees.FirstOrDefault(emp => emp.Id == empId);
                objEmp.IsDeleted = true;

                db.SaveChanges();
                //EmployeeListModel employeeListModel = GetAllRecords().Where(emp => emp.Id == empId).FirstOrDefault();
                //GetAllRecords().Remove(employeeListModel);
            }
        }

        public static void Delete(IEnumerable<EmployeeListModel> employeeListModels)
        {
            using (ReckonPayrollEntities db = new ReckonPayrollEntities())
            {
                foreach (var res in employeeListModels)
                {
                    Employee objEmp = db.Employees.FirstOrDefault(emp => emp.Id == res.Id);
                    objEmp.IsDeleted = true;

                    db.SaveChanges();
                    //EmployeeListModel result = GetAllRecords().Where(o => o.Id == res.Id).FirstOrDefault();
                    //GetAllRecords().Remove(result);
                }
            }
        }

        public static void Update(EmployeeListModel employeeListModel)
        {
            using (ReckonPayrollEntities db = new ReckonPayrollEntities())
            {
                Employee objEmp = db.Employees.FirstOrDefault(emp => emp.Id == employeeListModel.Id);
                if (objEmp != null)
                {
                    objEmp.Reference = employeeListModel.Reference;
                    objEmp.WeeklyHours = employeeListModel.WeeklyHours;
                }

                Person objPerson = db.People.FirstOrDefault(emp => emp.Id == employeeListModel.Id);
                if (objPerson != null)
                {
                    //objPerson.Id = employeeListModel.Id;
                    objPerson.FirstName = employeeListModel.FirstName;
                    //result.Title = employeeListModel.Title;
                    objPerson.DateOfBirth = Convert.ToDateTime(employeeListModel.DateOfBirth);
                    objPerson.TitleId = employeeListModel.TitleId;
                }
                db.SaveChanges();
            }
        }

        public static void Update(IEnumerable<EmployeeListModel> employeeListModels)
        {
            using (ReckonPayrollEntities db = new ReckonPayrollEntities())
            {
                foreach (var employeeListModel in employeeListModels)
                {
                    Employee objEmp = db.Employees.FirstOrDefault(emp => emp.Id == employeeListModel.Id);
                    if (objEmp != null)
                    {
                        objEmp.Reference = employeeListModel.Reference;
                        objEmp.WeeklyHours = employeeListModel.WeeklyHours;
                    }

                    Person objPerson = db.People.FirstOrDefault(emp => emp.Id == employeeListModel.Id);
                    if (objPerson != null)
                    {
                        objPerson.FirstName = employeeListModel.FirstName;
                        objPerson.DateOfBirth = Convert.ToDateTime(employeeListModel.DateOfBirth);
                        objPerson.TitleId = employeeListModel.TitleId;
                    }
                    db.SaveChanges();
                    //EmployeeListModel result = GetAllRecords().Where(emp => emp.Id == employeeListModel.Id).FirstOrDefault();
                    //if (result != null)
                    //{
                    //    result.Id = employeeListModel.Id;
                    //    result.FirstName = employeeListModel.FirstName;
                    //    //result.Title = employeeListModel.Title;
                    //    result.DateOfBirth = employeeListModel.DateOfBirth;
                    //    result.Reference = employeeListModel.Reference;
                    //}

                }
            }
        }
    }
}