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

namespace MileageTraker.Web.ViewModels.FuelLog
{
	public class LogMatchViewModel
	{
		public int LogId { get; set; }

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

		public string VehicleTagNumber { get; set; }

		[Display(Name = "End Odometer")]
		public int EndOdometer { 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; }

		// indicates an exact match for each of the fields
		public bool VehicleTagMatch { get; private set; }

		public bool OdometerMatch { get; private set; }

		public bool UserFullNameMatch { get; private set; }

		public bool GasPurchasedMatch { get; private set; }

		public bool DateMatch { get; private set; }

		public int MismatchCount
		{
			get
			{
				return
					(VehicleTagMatch ? 0 : 1) +
					(OdometerMatch ? 0 : 1) +
					(UserFullNameMatch ? 0 : 1) +
					(GasPurchasedMatch ? 0 : 1) +
					(DateMatch ? 0 : 1);
			}
		}

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

		public LogMatchViewModel(Models.Log log, Models.Vehicle vehicle, FuelLogViewModel fuelLog)
		{
			Mapper.Map(log, this);
			VehicleTagNumber = vehicle.TagNumber;

			VehicleTagMatch = VehicleTagNumber == fuelLog.TagNumber;
			OdometerMatch = log.EndOdometer == fuelLog.Odometer;
			UserFullNameMatch = string.Equals(log.User.FullName, fuelLog.DriverFullName, StringComparison.InvariantCultureIgnoreCase);
			GasPurchasedMatch = log.GasPurchased == fuelLog.GasPurchased;
			DateMatch = 
				log.Date.Year == fuelLog.Date.Year 
				&& log.Date.Month == fuelLog.Date.Month
				&& log.Date.Day == fuelLog.Date.Day;
		}
	}
}