using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using AutoMapper;
using MileageTraker.Web.Attributes;

namespace MileageTraker.Web.ViewModels.User
{
	public class CreateUserViewModel
	{
		[Required]
		[StringLength(128)]
		[RegularExpression(@"[A-Za-z().]+(\s+[A-Za-z().]+)+", ErrorMessage = "Need complete name")]
		[InputSize("medium")]
		[Remote("FullNameAvailable", "User", ErrorMessage = "Name already in use")]
		public string FullName { get; set; }

		[Required]
		[StringLength(64)]
		[InputSize("small")]
		[Remote("UsernameAvailable", "User", ErrorMessage = "Username already in use")]
		public string Username { get; set; }

		[Required]
		[DataType(DataType.EmailAddress)]
		[RegularExpression(@"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}", ErrorMessage = "Must be an email address")]
		[InputSize("large")]
		public string Email { get; set; }

		[Display(Name = "Roles")]
		public CheckBoxViewModel Roles { get; set; }

		[Required]
		[Display(Name = "Set password now instead of emailing invitation")]
		[NoEditLabel]
		public bool SetPassword { get; set; }

		static CreateUserViewModel()
		{
			Mapper.CreateMap<CreateUserViewModel, Models.User>()
				  .ForMember(vm => vm.Roles, opt => opt.Ignore());
		}

		public Models.User ToUser()
		{
			var user = new Models.User();
			Mapper.Map(this, user);
			return user;
		}
	}
}