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

namespace MileageTraker.Web.ViewModels.VehicleService
{
	public class VehicleServiceViewModel
	{
		[HiddenInput(DisplayValue = false)]
		public int? VehicleServiceId { get; set; }

		[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; }

		[DataType(DataType.DateTime)]
		[DisplayFormatAttribute(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
		[DenyFutureDate(ErrorMessage = "Future date")]
		[FormatHint("mm/dd/yyyy")]
		[InputSize("small")]
		[Required]
		public DateTime? InvoiceDate { get; set; }

		[Required]
		[StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")]
		public string ServiceCenterName { get; set; }

		[Required]
		[StringLength(32)]
		[InputSize("small")]
		public string InvoiceNumber { get; set; }

		[Required]
		[InputSize("small")]
		[Currency]
		public decimal Price { get; set; }

		[StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")]
		public string Description { get; set; }

		static VehicleServiceViewModel()
		{
			Mapper.CreateMap<VehicleServiceViewModel, Models.VehicleService>();
			Mapper.CreateMap<Models.VehicleService, VehicleServiceViewModel>()
			      .ForMember(dest => dest.VehicleId, opt => opt.MapFrom(src => src.Vehicle.VehicleId));
		}

		public VehicleServiceViewModel(Models.VehicleService vehicleService)
		{
			Mapper.Map(vehicleService, this);
		}

		public VehicleServiceViewModel()
		{
		}

		public Models.VehicleService GetVehicleService()
		{
			var vehicleService = new Models.VehicleService();
			Mapper.Map(this, vehicleService);
			return vehicleService;
		}
	}
}