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

namespace MileageTraker.Web.ViewModels.User
{
	public class EditUserViewModel
	{
		[HiddenInput(DisplayValue = false)]
		public Guid UserId { get; set; }

		[Required]
		[StringLength(128)]
		[RegularExpression(@"[A-Za-z().]+(\s+[A-Za-z().]+)+", ErrorMessage = "Need complete name")]
		[InputSize("medium")]
		public string FullName { get; set; }

		[Required]
		[StringLength(64)]
		[InputSize("small")]
		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; }

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

		public EditUserViewModel()
		{
		}

		public EditUserViewModel(Models.User user)
		{
			Mapper.Map(user, this);
		}

		public void UpdateUser(Models.User user)
		{
			user.Username = Username;
			user.FullName = FullName;
			user.Email = Email;
		}
	}
}