using System;
using System.Linq;
using System.Web.Mvc;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.Models;
using MileageTraker.Web.ViewModels;
using MileageTraker.Web.ViewModels.CreateLog;

namespace MileageTraker.Web.Controllers
{
	[Authorize(Roles = "Driver, Administrator, Developer")]
	public class CreateLogController : ControllerBase
	{
		private const string CookeNameVehicleid = "mr_vehicleId";
		private const string CookieKeyLogtype = "mr_logType";

		public ViewResult Index()
		{
			var createLogModel = NewCreateLogModel();

			return View(createLogModel);
		}

		[HttpPost]
		public ActionResult Index(CreateLogViewModel model)
		{
			if (ModelState.IsValid)
			{
				var confirmCreateLogViewModel = new ConfirmCreateLogViewModel(model);
				confirmCreateLogViewModel.Purpose.Available = GetPurposeTypesSelectList();
				return View("Confirm", confirmCreateLogViewModel);
			}

			model.Purpose.Available = GetPurposeTypesSelectList();
			return View(model);
		}

		[HttpParamAction]
		[HttpPost]
		public ViewResult Edit(CreateLogViewModel model)
		{
			model.Purpose.Available = GetPurposeTypesSelectList();
			return View("Index", model);
		}

		[HttpParamAction]
		[HttpPost]
		[ActionLog]
		public ActionResult Confirm(CreateLogViewModel model)
		{
			if (ModelState.IsValid)
			{
				SetCookieValue(CookieKeyLogtype, model.LogType.Enum.ToString());
				SetCookieValue(CookeNameVehicleid, model.VehicleId);

				// Save the new log
				var log = model.GetLog();
				log.User = DataService.FindUserByUsername(User.Identity.Name);
				log.Source = HttpContext.Request.Url.AbsolutePath;
				log.UserHostAddress = HttpContext.Request.UserHostAddress;
				log.UserAgent = HttpContext.Request.UserAgent;

				log.Purpose = 
					DataService.GetPurposeTypes()
					.First(pt => pt.PurposeTypeId == model.Purpose.Selected);

				DataService.AddLog(log);
				TempData["StatusMessage-Type"] = "alert-success";
				TempData["StatusMessage"] =
					@"You've successfully created an entry
					traveling to <strong>" + model.CityName + @"</strong> 
					for <strong>" + log.Purpose.Purpose + @"</strong> 
					on <strong>" + model.Date.ToShortDateString() + @"</strong>
					in Vehicle Id <strong>" + model.VehicleId + @"</strong>
					ending in <strong>" + model.EndOdometer + @"</strong>
					miles on the odometer.";
				return RedirectToAction("Index");
			}

			model.Purpose.Available = GetPurposeTypesSelectList();
			return View("Index", model);
		}

		public PartialViewResult RecentLogs()
		{
			var username = User.Identity.Name;
			var user = DataService.FindUserByUsername(username);
			var logs = DataService.GetRecentLogsByUsername(user.Username);
			ViewData["name"] = user.FullName;
			return PartialView(logs);
		}

		private CreateLogViewModel NewCreateLogModel()
		{
			MileageLogType logType;
			Enum.TryParse(GetCookieValue(CookieKeyLogtype), true, out logType);
			var vehicleId = GetCookieValue(CookeNameVehicleid);

			return new CreateLogViewModel
			{
				LogType = logType,
				VehicleId = vehicleId,
				Date = DateTime.Today,
				Purpose =
					new SelectListViewModel
					{
						Available =
							GetPurposeTypesSelectList()
					}
			};
		}
	}
}