using Exam.Extensions;
using Exam.Models;
using Exam.Services;
using Microsoft.AspNetCore.Mvc;

namespace Exam.Controllers
{
    [Route("/")]
    public class BaseController : Controller
    {
        private readonly IService _service;
        public BaseController(IService service) => _service = service;

        [HttpGet]
        [Route("search")]
        public IActionResult Login()
        {
            return View("~/Views/Search.cshtml");
        }

        [HttpPost]
        [Route("search")]
        public IActionResult Login(UserSearch userSearch)
        {
            HttpContext.Session.Set<UserSearch>("usersearch", userSearch);
            return Redirect("/");
        }

        [HttpGet]
        [Route("/")]
        public IActionResult Home()
        {
            if (HttpContext.Session.GetString("usersearch") == null)
            {
                return View("~/Views/Search.cshtml");
            }
            return View("~/Views/Home.cshtml");
        }

        [HttpGet]
        [Route("flights")]
        public IActionResult Flights()
        {
            var userSearch = HttpContext.Session.Get<UserSearch>("usersearch");
            if (userSearch == null)
            {
                return Redirect("/");
            }
            return View("~/Views/Flights.cshtml", _service.ShowFlightsByDateAndCity(userSearch.Date, userSearch.City));
        }

        [HttpGet]
        [Route("hotels")]
        public IActionResult Hotels()
        {
            var userSearch = HttpContext.Session.Get<UserSearch>("usersearch");
            if (userSearch == null)
            {
                return Redirect("/");
            }
            return View("~/Views/Hotels.cshtml", _service.ShowHotelsByDateAndCity(userSearch.Date, userSearch.City));
        }

        [HttpGet]
        [Route("reserve-flight/{id}")]
        public IActionResult ReserveFlight(int id)
        {
            var userSearch = HttpContext.Session.Get<UserSearch>("usersearch");
            if (userSearch == null)
            {
                return Redirect("/");
            }
            var reservation = new Reservation 
            {
                Person = userSearch.Name,
                Date = userSearch.Date,
                Type = "Flight",
                IdReservedResource = id
            };

            var result = _service.AddReservavtion(reservation);
            if(result != -1)
            {
                _service.DecrementFlightSeats(id);
                return Ok(result);
            }
            return BadRequest();
        }

        [HttpGet]
        [Route("reserve-hotel/{id}")]
        public IActionResult ReserveHotel(int id)
        {
            var userSearch = HttpContext.Session.Get<UserSearch>("usersearch");
            if (userSearch == null)
            {
                return Redirect("/");
            }
            var reservation = new Reservation
            {
                Person = userSearch.Name,
                Date = userSearch.Date,
                Type = "Hotel",
                IdReservedResource = id
            };

            var result = _service.AddReservavtion(reservation);
            if (result != -1)
            {
                _service.DecrementHotelRooms(id);
                return Ok(result);
            }
            return BadRequest();
        }
    }
}
