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

namespace MileageTraker.Web.ViewModels.ServiceReminder
{
	public class ServiceReminderViewModel : IComparable<ServiceReminderViewModel>, IValidatableObject
	{
		[HiddenInput(DisplayValue = false)]
		public int? ServiceReminderId { get; set; }

		[HiddenInput(DisplayValue = false)]
		[Required]
		[Remote("Exists", "Vehicle", ErrorMessage = "ID not found")]
		[StringLength(6, MinimumLength = 4, ErrorMessage = "Must be at least a 4 digit number")]
		[Display(Name = "Vehicle ID")]
		[RegularExpression(@"\d+", ErrorMessage = "Vehicle ID must be all numbers")]
		[InputSize("mini")]
		public string VehicleId { get; set; }

		[HiddenInput(DisplayValue = false)]
		[DisplayFormat(NullDisplayText = "Unassigned")]
		public string AssignedDriver { get; set; }

		[HiddenInput(DisplayValue = false)]
		public int? CurrentOdometer { get; set; }

		[InputSize("small")]
		[Range(1, 500000, ErrorMessage = "Between 1 and 500k")]
		public int TargetOdometer { get; set; }

		[StringLength(64)]
		public string Description { get; set; }

		[HiddenInput(DisplayValue = false)]
		public bool IsServiceOverdue { get { return ServiceDueInMiles <= 0; } }

		[HiddenInput(DisplayValue = false)]
		public int ServiceDueInMiles { get { return CurrentOdometer.HasValue ? TargetOdometer - CurrentOdometer.Value : int.MaxValue; } }

		static ServiceReminderViewModel()
		{
			Mapper.CreateMap<ServiceReminderViewModel, Models.ServiceReminder>();
			Mapper.CreateMap<Models.ServiceReminder, ServiceReminderViewModel>()
			      .ForMember(dest => dest.VehicleId, opt => opt.MapFrom(src => src.Vehicle.VehicleId))
			      .ForMember(dest => dest.CurrentOdometer, opt => opt.MapFrom(src => src.Vehicle.CurrentOdometer))
			      .ForMember(dest => dest.AssignedDriver, opt => opt.MapFrom(src => src.Vehicle.Assigned));
		}

		public ServiceReminderViewModel(Models.ServiceReminder serviceReminder)
		{
			Mapper.Map(serviceReminder, this);
		}

		public ServiceReminderViewModel()
		{
		}

		public Models.ServiceReminder GetServiceReminder()
		{
			var serviceReminder = new Models.ServiceReminder();
			Mapper.Map(this, serviceReminder);
			return serviceReminder;
		}

		public int CompareTo(ServiceReminderViewModel other)
		{
			return ServiceDueInMiles - other.ServiceDueInMiles;
		}

		public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
		{
			using (var dataService = new DataService())
			{
				var duplicateServiceReminder = dataService.FindDuplicateServiceReminder(VehicleId, TargetOdometer, Description);

				if (duplicateServiceReminder != null)
					yield return new ValidationResult("Exact duplicate of this reminder already exists");

				var vehicle = dataService.GetVehicle(VehicleId);

				if (vehicle.InactiveDate.HasValue)
					yield return new ValidationResult("Vehicle is inactive, no service reminder permitted");

				// no reminders for mileage less than the current odometer
				if (vehicle.CurrentOdometer.HasValue && TargetOdometer <= vehicle.CurrentOdometer)
					yield return new ValidationResult(
						string.Format("Target odometer {0} must be greater than current odometer {1}",
									TargetOdometer, vehicle.CurrentOdometer.Value), new[]{"TargetOdometer"});
			}
		}
	}
}