using System;
using System.ComponentModel.DataAnnotations;
using AutoMapper;

namespace MileageTraker.Web.ViewModels.FuelLog
{
	public class ImportFuelLogViewModel
	{
		public int FuelLogId { get; set; }

		[Required]
		[DataType(DataType.Date)]
		[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
		public DateTime Date { get; set; }

		[Required]
		[StringLength(128)]
		public string DriverFullName { get; set; }

		[Required]
		[Display(Name = "Tag#")]
		public string TagNumber { get; set; }

		[Required]
		public int Odometer { get; set; }

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

		public double MPG { get; set; }

		[Required]
		public double GasPurchased { get; set; }

		[Required]
		public decimal TotalPrice { get; set; }

		public bool PreviouslyAdded { get; set; }

		public int? LogId { get; set; }

		static ImportFuelLogViewModel()
		{
			Mapper.CreateMap<ImportFuelLogViewModel, Models.FuelLog>();
			Mapper.CreateMap<Models.FuelLog, ImportFuelLogViewModel>()
				.ForMember(dest => dest.LogId,
			               opt => opt.ResolveUsing(
							   fl => fl.Log != null ? (int?)fl.Log.LogId : null
							   ));
		}

		public ImportFuelLogViewModel(Models.FuelLog fuelLog)
		{
			Mapper.Map(fuelLog, this);
		}

		public ImportFuelLogViewModel()
		{
		}

		public Models.FuelLog GetFuelLog()
		{
			var log = new Models.FuelLog();
			Mapper.Map(this, log);
			return log;
		}
	}
}