using System;
using System.ComponentModel.DataAnnotations;
using AutoMapper;
using MileageTraker.Web.Models;

namespace MileageTraker.Web.ViewModels.Log
{
	public class LogIndexViewModel
	{
		public int? PreviousLogId { get; set; }
		public int? PreviousLogEndOdometer { get; set; }

		public int LogId { get; set; }

		[Display(Name = "Vehicle ID")]
		public string VehicleId { get; set; }

		[Display(Name = "End Odometer")]
		public int EndOdometer { get; set; }

		public int Miles { get; set; }

		[Display(Name = "Type")]
		public MileageLogTypeWrapper LogType { get; set; }

		[Display(Name = "City Name")]
		public string CityName { get; set; }

		[Display(Name = "Purpose")]
		public string PurposePurpose { get; set; }

		public string Notes { get; set; }

		[Display(Name = "Driver Name")]
		public string UserFullName { get; set; }

		[Display(Name = "Gas Purchased")]
		[DisplayFormat(DataFormatString = "{0:0.000}", ApplyFormatInEditMode = true)]
		public double GasPurchased { get; set; }

		public DateTime Date { get; set; }

		[Display(Name = "Time Created")]
		[DisplayFormat(DataFormatString = @"{0:MM/dd/yyyy h:mm tt}")]
		public DateTime Created { get; set; }

		static LogIndexViewModel()
		{
			Mapper.CreateMap<Models.Log, LogIndexViewModel>();
		}

		public LogIndexViewModel(Models.Log log, Models.Log previousLog)
		{
			Mapper.Map(log, this);
			if (previousLog != null)
			{
				PreviousLogId = previousLog.LogId;
				PreviousLogEndOdometer = previousLog.EndOdometer;
				Miles = EndOdometer - previousLog.EndOdometer;
			}
		}
	}
}