using System;
using DiplomovaPrace.DataLayer.Repositories.Security;
using DiplomovaPrace.Model.Security;
using Havit.Data.Patterns.DataSeeds;
using Havit.Data.Patterns.DataSeeds.Profiles;
using Havit.Data.Patterns.Exceptions;
using Havit.Data.Patterns.UnitOfWorks;

namespace DiplomovaPrace.DataLayer.Seeds
{
	public class LoginSeed : DataSeed<DefaultProfile>
	{
		private readonly IRoleRepository roleRepository;
		private readonly IUnitOfWork unitOfWork;

		public LoginSeed(IRoleRepository roleRepository, IUnitOfWork unitOfWork)
		{
			this.roleRepository = roleRepository;
			this.unitOfWork = unitOfWork;
		}

		public override void SeedData()
		{
			Role role;
			try
			{
				role = roleRepository.GetObject((int)Role.Entry.Admin);
			}
			catch (ObjectNotFoundException)
			{
				role = new Role()
				{
					ID = (int)Role.Entry.Admin,
					Name = nameof(Role.Entry.Admin)
				};

				unitOfWork.AddForInsert(role);
				unitOfWork.Commit();
			}

			LoginAccount loginAccount = new LoginAccount
			{
				FirstName = "František",
				LastName = "Štefl",
				Email = "stefl@moon.fish",
				PasswordHash = "00D169DD05CC26193D00AAE78579F3BF2A3DBC219BDD5EBB2CC924534AD341A7FD37B887035F018991D352890E0C4C4CE822322C57BB42C8E86ED5430CEE8BFD",
				PasswordSalt = "vNnSwLQL",
				RoleID = role.ID
			};

			Seed(For(loginAccount).PairBy(i => i.Email).WithoutUpdate());
		}
	}
}
