using System.ComponentModel.DataAnnotations;
using AutoMapper;
using LeafWeb.Core.DAL;
using LeafWeb.Core.Entities;
using LeafWeb.WebCms.Controllers;

namespace LeafWeb.WebCms.Models
{
	public class LeafInputCreate
	{
		[Display(Name = "Your name")]
		[Required(ErrorMessage = "Name required")]
		[RegularExpression(@"[A-Za-z().]+(\s+[A-Za-z().]+)+", ErrorMessage = "Please provide your full name")]
		public string Name { get; set; }

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

		[Display(Name = "Confirm email address")]
		[Required(ErrorMessage = "Enter email exactly as above")]
		[System.ComponentModel.DataAnnotations.Compare("Email")]
		public string EmailConfirm { get; set; }

		[Display(Name = "A unique identifier for this data")]
		[Required(ErrorMessage = "A unique identifier is required")]
		public string Identifier { get; set; }

		[Display(Name = "The site's name/Fluxnet ID, if known")]
		[Required(ErrorMessage = "The site's name is required")]
		public string SiteId { get; set; }

		[Display(Name = "Photosynthetic Pathways")]
		[Required(ErrorMessage = "A Photosynthetic pathway must be chosen")]
		public SelectListViewModel PhotosynthesisType { get; set; }

		[UIHint("TermsOfService")]
		[EnforceTrue(ErrorMessage = "Terms of Service must be accepted to continue")]
		public bool TermsOfService { get; set; }

		static LeafInputCreate()
		{
			Mapper.CreateMap<LeafInputCreate, LeafInput>()
				.ForMember(dest => dest.PhotosynthesisType, opt => opt.Ignore());
			Mapper.CreateMap<LeafInput, LeafInputCreate>()
				.ForMember(dest => dest.PhotosynthesisType, opt => opt.Ignore());
		}

		public LeafInputCreate()
		{
			PhotosynthesisType = new SelectListViewModel();
		}

		public LeafInput GetLeafInput(DataService db)
		{
			var leafInput = new LeafInput();
			Mapper.Map(this, leafInput);

			leafInput.PhotosynthesisType = db.GetPhotosynthesisType(PhotosynthesisType.Selected);
			return leafInput;
		}
	}
}