using Microsoft.AspNetCore.Mvc;
using TravelAgencyContracts.BindingModels;
using TravelAgencyContracts.BusinessLogicsContracts;
using TravelAgencyContracts.SearchModels;

namespace TravelAgencyWebApp.Controllers
{
    public class TourController : Controller
    {
		private readonly ILogger<TourController> _logger;

        private readonly ITourLogic _tourLogic;

        public TourController(ILogger<TourController> logger, ITourLogic tourLogic)
        {
            _logger = logger;
            _tourLogic = tourLogic;
        }

		[HttpGet]
        public IActionResult Tours()
        {
            if (LoggedinUser.User == null)
            {
                return Redirect("~/Home/Enter");
            }

            return View(_tourLogic.ReadList(new TourSearchModel
            {
                UserId = LoggedinUser.User.Id,
            }));
        }

		[HttpGet]
        public IActionResult CreateTour()
        {
            if (LoggedinUser.User == null)
            {
                return Redirect("~/Home/Enter");
            }

            return View();
        }

        [HttpPost]
        public void CreateTour(string tourName, string tourDescription, double price, DateTime tourDate)
        {
            if (LoggedinUser.User == null)
            {
                throw new Exception("Необходимо авторизоваться!");
            }

            if (string.IsNullOrEmpty(tourName) || string.IsNullOrEmpty(tourDescription) || price <= 0 || tourDate == DateTime.MinValue)
            {
                throw new Exception("Введены не все данные!");
            }

            _tourLogic.Create(new TourBindingModel
            {
                TourName = tourName,
                TourDescription = tourDescription,
                Price = price,
                TourDate = tourDate,
                UserId = LoggedinUser.User.Id
            });

            Response.Redirect("/Tour/Tours");
        }

		[HttpGet]
        public IActionResult UpdateTour(int id)
        {
            if (LoggedinUser.User == null)
            {
                return Redirect("~/Home/Enter");
            }

            return View(_tourLogic.ReadElement(new TourSearchModel
            {
                Id = id
            }));
        }

        [HttpPost]
        public void UpdateTour(int id, string tourName, string tourDescription, double price, DateTime tourDate)
        {
            if (LoggedinUser.User == null)
            {
                throw new Exception("Необходимо авторизоваться!");
            }

            if (string.IsNullOrEmpty(tourName) || string.IsNullOrEmpty(tourDescription) || price <= 0 || tourDate == DateTime.MinValue)
            {
                throw new Exception("Введены не все данные!");
            }

            _tourLogic.Update(new TourBindingModel
            {
                Id = id,
                TourName = tourName,
                TourDescription = tourDescription,
                Price = price,
                TourDate = tourDate,
                UserId = LoggedinUser.User.Id
            });

            Response.Redirect("/Tour/Tours");
        }

		[HttpPost]
        public void DeleteTour(int id)
        {
            if (LoggedinUser.User == null)
            {
                throw new Exception("Необходимо авторизоваться!");
            }

            _tourLogic.Delete(new TourBindingModel
            {
                Id = id
            });

            Response.Redirect("/Tour/Tours");
        }
    }
}