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

namespace KendoDateSample.Models.Repositories
{
    static public class UserRepository
    {
        public static IQueryable<UserModel> All()
        {
            var context = new FPSTestEntities();
            
            return (from entity in context.Users
                    join type in context.UserTypes on entity.UserTypeId equals type.Id
                    select new UserModel
                    {
                        Id = entity.Id,
                        Created = entity.Created,
                        UserTypeId = entity.UserTypeId,
                        FirstName = entity.FirstName,
                        LastName = entity.LastName,
                        Email = entity.Email,
                        Password = entity.Password,
                        AddressLine1 = entity.AddressLine1,
                        AddressLine2 = entity.AddressLine2,
                        City = entity.City,
                        StateId = entity.StateId,
                        Zipcode = entity.Zipcode,
                        Phone1 = entity.Phone1,
                        SalesManagerId = entity.SalesManagerId,
                        SalesRepId = entity.SalesRepId,
                        DoctorId = entity.DoctorId,
                        IsActive = entity.IsActive,
                        DOB = entity.DOB,
                        CarrierId = entity.CarrierId,
                        UserTypeName = type.Name
                    }).AsQueryable();
        }


        public static UserModel One(int id)
        {
            var context = new FPSTestEntities();

            return (from entity in context.Users
                    join type in context.UserTypes on entity.UserTypeId equals type.Id
                    where
                    entity.Id == id
                    select new UserModel
                    {
                        Id = entity.Id,
                        Created = entity.Created,
                        UserTypeId = entity.UserTypeId,
                        FirstName = entity.FirstName,
                        LastName = entity.LastName,
                        Email = entity.Email,
                        Password = entity.Password,
                        AddressLine1 = entity.AddressLine1,
                        AddressLine2 = entity.AddressLine2,
                        City = entity.City,
                        StateId = entity.StateId,
                        Zipcode = entity.Zipcode,
                        Phone1 = entity.Phone1,
                        SalesManagerId = entity.SalesManagerId,
                        SalesRepId = entity.SalesRepId,
                        DoctorId = entity.DoctorId,
                        IsActive = entity.IsActive,
                        DOB = entity.DOB,
                        CarrierId = entity.CarrierId,
                        UserTypeName = type.Name
                    }).FirstOrDefault();
        }

        public static bool Any(Func<User, bool> predicate)
        {
            var result = false;

            using (var context = new FPSTestEntities())
            {
                result = context.Users.Any<User>(predicate);
            }

            return result;
        }

        public static bool Update(UserModel model)
        {
            if (model == null) return false;

            var result = false;
            model.Password = model.PasswordUnEncrypted.EncryptPassword();

            using (var context = new FPSTestEntities())
            {
                var entity = context.Users.SingleOrDefault(u => u.Id == model.Id);

                if (entity != null)
                {
                    
                    entity.Id = model.Id;
                    entity.Created = model.Created;
                    entity.UserTypeId = model.UserTypeId;
                    entity.FirstName = model.FirstName;
                    entity.LastName = model.LastName;
                    entity.Email = model.Email;
                    entity.Password = model.Password;
                    entity.AddressLine1 = model.AddressLine1;
                    entity.AddressLine2 = model.AddressLine2;
                    entity.City = model.City;
                    entity.StateId = model.StateId;
                    entity.Zipcode = model.Zipcode;
                    entity.Phone1 = model.Phone1;
                    entity.SalesManagerId = model.SalesManagerId;
                    entity.SalesRepId = model.SalesRepId;
                    entity.DoctorId = model.DoctorId;
                    entity.IsActive = model.IsActive;
                    entity.DOB = model.DOB;
                    entity.CarrierId = model.CarrierId;
                    

                    //Reflector.Clone(model, entity);
                    context.SaveChanges();
                    model.UserTypeName = entity.UserType != null ? entity.UserType.Name : string.Empty;
                    result = true;
                }
            }

            return result;
        }

        public static bool Delete(int id)
        {
            if (id <= 0) return false;

            var result = false;

            using (var context = new FPSTestEntities())
            {
                var entity = context.Users.SingleOrDefault(u => u.Id == id);

                if (entity != null)
                {
                    context.Users.DeleteObject(entity);
                    var count = context.SaveChanges();
                    result = (count > 0);
                }
            }

            return result;
        }

        

        /*NOTE: to be used only when importing data with clear text passwords
        public static void EncryptAllPasswords()
        {
            using (FPSTestEntities context = new FPSTestEntities())
            {
                List<User> users = context.Users.ToList();

                foreach (User user in users)
                {
                    user.Password = EncryptPassword(user.Password);
                    context.SaveChanges();
                }
                 
            }
        }
         */
    }
}