using DiplomovaPrace.Contracts.Customer.LoginAccounts;
using DiplomovaPrace.DataLayer.Repositories.Security;
using DiplomovaPrace.Model.Security;
using Havit.Data.Patterns.DataLoaders;
using Havit.Data.Patterns.UnitOfWorks;
using Havit.Extensions.DependencyInjection.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiplomovaPrace.Facades.Customer.Account
{
	[Service]
	public class AccountFacade : IAccountFacade
	{
		private readonly IDataLoader dataLoader;
		private readonly IUnitOfWork unitOfWork;
		private readonly ILoginAccountRepository loginAccountRepository;

		public AccountFacade(IDataLoader dataLoader, IUnitOfWork unitOfWork, ILoginAccountRepository loginAccountRepository)
		{
			this.dataLoader = dataLoader;
			this.unitOfWork = unitOfWork;
			this.loginAccountRepository = loginAccountRepository;
		}

		public async Task<UserDetailsDto> GetUserDetailsAsync(int userID)
		{
			LoginAccount loginAccount = await loginAccountRepository.GetObjectAsync(userID);

			if (loginAccount is not null)
			{
				return new UserDetailsDto()
				{
					FirstName = loginAccount.FirstName ?? string.Empty,
					LastName = loginAccount.LastName ?? string.Empty
				};
			}

			return null;
		}

		public async Task<bool> UpdateUserDetailsAsync(int userID, UserDetailsDto inputModel)
		{
			LoginAccount loginAccount = await loginAccountRepository.GetObjectAsync(userID);

			if (loginAccount is not null)
			{
				loginAccount.FirstName = inputModel.FirstName;
				loginAccount.LastName = inputModel.LastName;

				unitOfWork.AddForUpdate(loginAccount);
				await unitOfWork.CommitAsync();

				return true;
			}

			return false;
		}
	}
}
