using Microsoft.EntityFrameworkCore;
using PositioningWebApp.Models.DB;
using PositioningWebApp.Models.UserProfile;
using PositioningWebApp.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PositioningWebApp.DBServices
{
	public class UserService
	{
		public UserProfile AddUser(Users user, DevicesOnMapBackgroundContext context)
		{
			try
			{
				context.Users.Add(user);
				context.SaveChanges();
				return new UserProfile
				{
					Email = user.Email,
					FirstName = user.FirstName,
					ID = user.Id,
					Password = user.PasswordHash,
					Phone = user.PhoneNumber,
					Surname = user.Surname
				};
			
			} catch(Exception ex)
			{
				throw ex;
			}
		}

		public bool RegistrationEmailExist(string email, DevicesOnMapBackgroundContext context)
		{
			var users = from u in context.Users select u;
			var info = from u in context.Users where u.Email == email select u;
			bool exist = false;
			foreach (var t in info)
			{
				exist = true;
			}
			return exist;
		}

		public bool LoginCorect(string email, string password, DevicesOnMapBackgroundContext context)
		{
			HashPassword hashPasswor = new HashPassword();
			var users = from u in context.Users select u;
			List<UserProfile> loginInfo = new List<UserProfile>();
			string passwH = hashPasswor.GetHash(password);
			var user = context.Users.Where(u => u.Email == email && u.PasswordHash == passwH).ToList();
			foreach (var item in user)
			{
				loginInfo.Add(new UserProfile
				{
					Email = item.Email
				});
			}
			if (loginInfo != null && loginInfo.ToList().Count() > 0)
			{
				return true;
			} else
			{
				return false;
			}
		}

		public void UpadateUser (bool saveWithPassword, int ID, string firstName, string surname, string email, string password, string phone, DevicesOnMapBackgroundContext context)
		{
			try
			{
				var result = context.Users.FirstOrDefault(u => u.Id == ID);
				if (result != null)
				{
					result.FirstName = firstName;
					result.Surname = surname;
					result.Email = email;
					result.PhoneNumber = phone;
					if (saveWithPassword)
					{
						HashPassword hash = new HashPassword();
						result.PasswordHash = hash.GetHash(password);
					}
				}

				context.Entry(result).State = EntityState.Modified;
				context.SaveChanges();
			
			} catch (Exception ex)
			{
				throw ex;
			}
		}

		public UserProfile GetProfile (string email, DevicesOnMapBackgroundContext context)
		{
			var user = from u in context.Users where u.Email == email select u;

			if (user.ToList().Count == 0)
			{
				return null;
			} else
			{
				var profile = new UserProfile
				{
					Email = user.First().Email,
					FirstName = user.First().FirstName,
					Surname = user.First().Surname,
					ID = user.First().Id,
					Phone = user.First().PhoneNumber
				};
				return profile;
			}
		}

		public List<UserProfile> GetProfiles(List<int> ids, DevicesOnMapBackgroundContext context)
		{
			var users = from u in context.Users where ids.Contains(u.Id) select u;

			if (users.ToList().Count == 0)
			{
				return null;
			}
			else
			{
				List<UserProfile> profiles = new List<UserProfile>();
				foreach (var user in users.ToList())
				{
					profiles.Add(new UserProfile {
						Email = user.Email,
						FirstName = user.FirstName,
						Surname = user.Surname,
						ID = user.Id,
						Phone = user.PhoneNumber
					});
				}
				return profiles;
			}
		}

		public int GetUserID (string name, DevicesOnMapBackgroundContext context)
		{
			var user = from u in context.Users where u.Email == name select u;
			return user.First().Id;
		}

		public UserProfile GetUser(int idUser, DevicesOnMapBackgroundContext context)
		{
			var ids = from u in context.Users where u.Id == idUser select u;
			return new UserProfile
			{
				ID = idUser,
				Email = ids.ToList().FirstOrDefault().Email,
				FirstName = ids.ToList().FirstOrDefault().FirstName,
				Surname = ids.ToList().FirstOrDefault().Surname,
				Phone = ids.ToList().First().PhoneNumber
			};
		}
	}
}
