using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using AutoMapper;
using LeafWeb.Core.Entities;
using LeafWeb.Core.Utility;

namespace LeafWeb.WebCms.Models
{
	public class LeafInputDetails
	{
		[HiddenInput(DisplayValue = false)]
		public int LeafInputId { get; set; }

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

		[Display(Name = "Site Id")]
		[Required(ErrorMessage = "The site's name is required")]
		public string SiteId { get; set; }

		[Display(Name = "Photosyn. Pathway")]
		[Required(ErrorMessage = "A Photosynthetic pathway must be chosen")]
		public string PhotosynthesisType { get; set; }

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

		[Display(Name = "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; }

		[DataType(DataType.Date)]
		[Required]
		public DateTime Added { get; set; }

		[Display(Name = "Piscal Error")]
		[UIHint("PreReadMore")]
		public string OutputErrorMessage { get; set; }

		[Display(Name = "Piscal Warning")]
		[UIHint("PreReadMore")]
		public string OutputWarningMessage { get; set; }

		[Display(Name = "Time In Progress")]
		public TimeSpan TimeInProgress { get; set; }

		[UIHint("LeafInputStatusViewModels")]
		public List<LeafInputStatusViewModel> StatusHistory { get; set; }

		[HiddenInput(DisplayValue = false)]
		public bool HasLeafChart { get; set; }

		[HiddenInput(DisplayValue = false)]
		public bool HasOutputFiles { get; set; }

		[HiddenInput(DisplayValue = false)]
		public bool IsRunning { get; set; }

		[HiddenInput(DisplayValue = false)]
		public bool IsComplete { get; set; }

		[HiddenInput(DisplayValue = false)]
		public bool IsDeletable { get; set; }

		[HiddenInput(DisplayValue = false)]
		public bool IsCancellable { get; set; }

		static LeafInputDetails()
		{
			Mapper.CreateMap<LeafInputFile, string>().ConvertUsing(file => file?.Contents.GetString());
			Mapper.CreateMap<LeafOutputFile, string>().ConvertUsing(file => file?.FileContents.Contents.GetString());
			Mapper.CreateMap<LeafInputStatusType, string>().ConvertUsing(st => st.ToString());
			Mapper.CreateMap<LeafInputStatusType, LeafInputStatus>().ConvertUsing(st => new LeafInputStatus());
			Mapper.CreateMap<LeafInputStatus, LeafInputStatusViewModel>();
			Mapper.CreateMap<LeafInput, LeafInputDetails>()
				.ForMember(dest => dest.LeafInputId, opt => opt.MapFrom(src => src.Id))
				.ForMember(dest => dest.HasLeafChart, opt => opt.ResolveUsing(src => src.OutputFiles.Any(o => o.IsLeafChartFile)));
		}

		public LeafInputDetails(LeafInput leafInput)
		{
			Mapper.Map(leafInput, this);
		}
	}
}