using System.Linq;
using System.Web.Mvc;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.DAL;
using MileageTraker.Web.ViewModels.ServiceReminder;

namespace MileageTraker.Web.Controllers
{
	[Authorize(Roles = "Administrator, Developer")]
	public class ServiceReminderController : ControllerBase
	{
		[RequireRequestValue("vehicleId")]
		public ActionResult Index(string vehicleId)
		{
			var serviceReminders = DataService.GetServiceReminders(vehicleId).ToList();
			var viewModel = new ServiceReminderResultsViewModel
				{
					ServiceReminderItems = serviceReminders.Select(sr => new ServiceReminderViewModel(sr)),
					VehicleId = vehicleId
				};
			return View(viewModel);
		}

		[HttpGet]
		[RequireRequestValue("vehicleId")]
		public ActionResult Create(string vehicleId)
		{
			var vehicle = DataService.GetVehicle(vehicleId);

			var viewModel = new ServiceReminderViewModel
			{
				VehicleId = vehicleId,
				CurrentOdometer = vehicle.CurrentOdometer
			};

			if (vehicle.CurrentOdometer.HasValue)
				viewModel.TargetOdometer = vehicle.CurrentOdometer.Value + DataService.ServiceReminderDefaultMileageDelta;

			return View(viewModel);
		}

		[HttpPost]
		[ActionLog]
		public ActionResult Create(ServiceReminderViewModel viewModel)
		{
			if (ModelState.IsValid)
			{
				var serviceReminder = viewModel.GetServiceReminder();

				serviceReminder.Vehicle = DataService.GetVehicle(viewModel.VehicleId);

				DataService.AddServiceReminder(serviceReminder);

				SetStatusMessage(
					string.Format("Service Reminder at {0} miles created", viewModel.TargetOdometer), StatusType.Success);
				return RedirectToAction("Index", new {vehicleId = viewModel.VehicleId});
			}

			return View(viewModel);
		}

		[HttpGet]
		public ActionResult Edit(int id)
		{
			var serviceReminder = DataService.GetServiceReminder(id);
			var viewModel = new ServiceReminderViewModel(serviceReminder);
			return View(viewModel);
		}

		[HttpPost]
		[ActionLog]
		public ActionResult Edit(ServiceReminderViewModel viewModel)
		{
			if (ModelState.IsValid)
			{
				var serviceReminder = viewModel.GetServiceReminder();

				serviceReminder.Vehicle = DataService.GetVehicle(viewModel.VehicleId);
				
				DataService.UpdateServiceReminder(serviceReminder);

				SetStatusMessage(
					string.Format("Service Reminder at {0} miles updated", viewModel.TargetOdometer), StatusType.Success);
				return RedirectToAction("Index", new {vehicleId = viewModel.VehicleId});
			}

			return View(viewModel);
		}
		
		public ActionResult Delete(int id)
		{
			var vehicleService = DataService.GetServiceReminder(id);
			var viewModel = new ServiceReminderViewModel(vehicleService);
			return View(viewModel);
		}

		[HttpPost, ActionName("Delete")]
		[ActionLog]
		public ActionResult DeleteConfirmed(int id)
		{
			var serviceReminder = DataService.GetServiceReminder(id);
			var vehicleId = serviceReminder.Vehicle.VehicleId;
			DataService.DeleteServiceReminder(id);

			SetStatusMessage("Service Reminder deleted");
			return RedirectToAction("Index", new { vehicleId });
		}
	}
}