using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.DAL;

namespace MileageTraker.Web.ViewModels.VehicleService
{
	public class VehicleSelectViewModel : IValidatableObject
	{
		[Required(ErrorMessage = "Required")]
		[Remote("Exists", "Vehicle", ErrorMessage = "ID not found")]
		[StringLength(6, MinimumLength = 4, ErrorMessage = "Must be a 4 digit number")]
		[Display(Name = "Vehicle ID")]
		[InputSize("mini")]
		[RegularExpression(@"\d+", ErrorMessage = "Must be all numbers")]
		public string ServiceVehicleId { get; set; }

		public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
		{
			using (var dataService = new DataService())
			{
                var inactiveDate = dataService.GetVehicle(ServiceVehicleId).InactiveDate;
				if (inactiveDate.HasValue)
				{
					yield return new ValidationResult(
						string.Format("Vehicle was set inactive on {0:MM/dd/yyyy}", inactiveDate.Value),
						new[] { "VehicleId" });
				}
			}
		}

		public override string ToString()
		{
			return string.Format("vehicle: {0}", ServiceVehicleId);
		}
	}
}