using Azure;
using FluentNHibernate;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using TravelCompanyClientApp.Models;
using TravelCompanyContracts.BindingModels.TouristBindingModels;
using TravelCompanyContracts.BusinessLogicsContracts.OperatorBusinessLogicsContracts;
using TravelCompanyContracts.BusinessLogicsContracts.TouristBusinessLogicsContracts;
using TravelCompanyContracts.SearchModels.OperatorSearchModels;
using TravelCompanyContracts.SearchModels.TouristSearchModels;
using TravelCompanyContracts.ViewModels.OperatorViewModels;
using TravelCompanyContracts.ViewModels.TouristViewModels;
using TravelCompanyDatabaseImplement.Models.TouristModel;
using TravelCompanyDataModels.Models.OperatorModels;
using TravelCompanyDataModels.Models.TouristModels;

namespace TravelCompanyClientApp.Controllers
{
    public class ExcursionController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IExcursionLogic _logic;
        private readonly ITravelLogic _travelLogic;
        private readonly IGuideLogic _guideLogic;

        public ExcursionController(ILogger<HomeController> logger, IExcursionLogic logic, ITravelLogic travelLogic, IGuideLogic guideLogic)
        {
            _logger = logger;
            _logic = logic;
            _travelLogic = travelLogic;
            _guideLogic = guideLogic;
        }
        [HttpGet]
        public IActionResult Excursion()
        {
            if (APIClient.Tourist == null)
            {
                return Redirect("~/Home/Enter");
            }
            return View(_logic.ReadList(new ExcursionSearchModel { TouristId = APIClient.Tourist.Id }));

        }

        [HttpGet]
        public IActionResult CreateExcursion()
        {
            return View();
        }

        [HttpPost]
        public void CreateExcursion(string excursionname, string description, int duration)
        {
            if (APIClient.Tourist == null)
            {
                throw new Exception("Доступно только авторизованным пользователям");
            }
            if (string.IsNullOrEmpty(excursionname) || string.IsNullOrEmpty(description) || duration <= 0)
            {
                throw new Exception("Неправильноо введенные данные");
            }
            _logic.Create(new ExcursionBindingModel
            {
                ExcursionName = excursionname,
                ExcursionDescrition = description,
                Duration = duration,
                TouristId = APIClient.Tourist.Id
            });
            Response.Redirect("Excursion");
            return;
        }
        [HttpGet("/excursion/updateexcursion/{id?}")]
        public IActionResult UpdateExcursion(int id)
        {
            if (APIClient.Tourist == null)
            {
                return Redirect("~/Home/Enter");
            }
            return View(_logic.ReadElement(new ExcursionSearchModel { TouristId = APIClient.Tourist.Id, Id = id }));
        }
        [HttpPost("/excursion/updateexcursion/{id?}")]
        public void UpdateExcursion(int id, string excursionname, string description, int duration)
        {
            if (APIClient.Tourist == null)
            {
                throw new Exception("Доступно только авторизованным пользователям");
            }
            if (string.IsNullOrEmpty(excursionname) || string.IsNullOrEmpty(description) || duration <= 0)
            {
                throw new Exception("Неправильноо введенные данные");
            }
            _logic.Update(new ExcursionBindingModel
            {
                Id = id,
                ExcursionName = excursionname,
                ExcursionDescrition = description,
                Duration = duration,
                TouristId = APIClient.Tourist.Id
            });
            Response.Redirect("/Excursion/Excursion");
            return;
        }
        [HttpPost("/excursion/deleteexcursion/{id?}")]
        public void DeleteExcursion(int id)
        {
            if (APIClient.Tourist == null)
            {
                throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
            }
            _logic.Delete(new ExcursionBindingModel { Id = id });
            Response.Redirect("/Excursion/Excursion");
        }
        [HttpGet]
        public IActionResult CreateTravelExcursion()
        {
            if (APIClient.Tourist == null)
            {
                return Redirect("~/Home/Enter");
            }
            ViewBag.Travels = _travelLogic.ReadList(new TravelSearchModel { TouristId = APIClient.Tourist.Id});
            ViewBag.Excursions = _logic.ReadList(new ExcursionSearchModel { TouristId = APIClient.Tourist.Id });
            return View();
        }
        [HttpPost]
        public void CreateTravelExcursion(int travelselect, List<int> excursionselect)
        {
            if (APIClient.Tourist == null)
            {
                throw new Exception("Доступно только авторизованным пользователям");
            }
            if (travelselect <= 0 || excursionselect==null)
            {
                throw new Exception("Неправильноо введенные данные");
            }
            Dictionary<int, IExcursionModel> te = new Dictionary<int, IExcursionModel>();
            foreach (int exc in excursionselect)
            {
                te.Add(exc, _logic.ReadElement(new ExcursionSearchModel { Id = exc }));
            }
            var travel=_travelLogic.ReadElement(new TravelSearchModel { Id=travelselect });
            _travelLogic.Update(new TravelBindingModel
            {
                Id = travel.Id,
                TravelName = travel.TravelName,
                DateStart = travel.DateStart,
                TravelDescription = travel.TravelDescription,
                TourTravels=travel.TourTravels,
                ExcursionTravels = te
            });
            Response.Redirect($"/Travel/UpdateTravel/{travel.Id}");
            return;
        }
        [HttpGet]
        public IActionResult CreateGuideExcursion()
        {
            if (APIClient.Tourist == null)
            {
                return Redirect("~/Home/Enter");
            }
            ViewBag.Guides = _guideLogic.ReadList(null);
            ViewBag.Excursions = _logic.ReadList(new ExcursionSearchModel { TouristId = APIClient.Tourist.Id });
            return View();
        }
        [HttpPost]
        public void CreateGuideExcursion(int excursionselect, List<int> guideselect)
        {
            if (APIClient.Tourist == null)
            {
                throw new Exception("Доступно только авторизованным пользователям");
            }
            if (excursionselect <= 0 || guideselect == null)
            {
                throw new Exception("Неправильноо введенные данные");
            }
            Dictionary<int, IGuideModel> te = new Dictionary<int, IGuideModel>();
            foreach (int exc in guideselect)
            {
                te.Add(exc, _guideLogic.ReadElement(new GuideSearchModel { Id = exc }));
            }
            var excursion = _logic.ReadElement(new ExcursionSearchModel { Id = excursionselect });
            _logic.Update(new ExcursionBindingModel
            {
                Id = excursion.Id,
                ExcursionName= excursion.ExcursionName,
                Duration = excursion.Duration,
                ExcursionDescrition= excursion.ExcursionDescrition,
                GuideExcursions = te
            });
            Response.Redirect($"/Excursion/UpdateExcursion/{excursion.Id}");
            return;
        }
    }
}