using Azure;
using FluentNHibernate;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using TravelCompanyBusinessLogic.BusinessLogic.TouristLogics;
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;

namespace TravelCompanyClientApp.Controllers
{
    public class TravelController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly ITravelLogic _logic;
        private readonly ITourLogic _tourLogic;

        public TravelController(ILogger<HomeController> logger, ITravelLogic logic, ITourLogic tourLogic)
        {
            _logger = logger;
            _logic = logic;
            _tourLogic = tourLogic;
        }
        [HttpGet]
        public IActionResult Travel()
        {
            if (APIClient.Tourist == null)
            {
                return Redirect("~/Home/Enter");
            }
            return View(_logic.ReadList(new TravelSearchModel { TouristId = APIClient.Tourist.Id }));
        }

        [HttpGet]
        public IActionResult CreateTravel()
        {
            ViewBag.Tours = _tourLogic.ReadList(null);
            return View();
        }

        [HttpPost]
        public void CreateTravel(string travelname, DateTime datestart, string description, List<int> tourselect)
        {
            if (string.IsNullOrEmpty(travelname) || string.IsNullOrEmpty(description) || datestart==null || tourselect==null|| tourselect.Count<=0)
            {
                throw new Exception("Неверный ввод");
            }
            Dictionary<int,ITourModel> ts = new Dictionary<int, ITourModel>();
            foreach (int tour in tourselect)
            {
                ts.Add(tour,_tourLogic.ReadElement(new TourSearchModel { Id = tour }));
            }
            _logic.Create( new TravelBindingModel
            {
                TravelName = travelname,
                TravelDescription = description,
                DateStart = datestart,
                TouristId = APIClient.Tourist.Id,
                TourTravels= ts
            });
            Response.Redirect("Travel");
            return;
        }
        [HttpGet("/travel/updatetravel/{id?}")]
        public IActionResult UpdateTravel(int id)
        {
            if (APIClient.Tourist == null)
            {
                return Redirect("~/Home/Enter");
            }
            ViewBag.Tours = _tourLogic.ReadList(null);
            return View(_logic.ReadElement(new TravelSearchModel { TouristId = APIClient.Tourist.Id, Id=id})) ;
        }
        [HttpPost("/travel/updatetravel/{id?}")]
        public void UpdateTravel(int id, string travelname, DateTime datestart, string description, List<int> tourselect)
        {
            if (string.IsNullOrEmpty(travelname) || string.IsNullOrEmpty(description) || datestart == null || tourselect == null || tourselect.Count <= 0)
            {
                throw new Exception("Неверный ввод");
            }
            Dictionary<int, ITourModel> ts = new Dictionary<int, ITourModel>();
            foreach (int tour in tourselect)
            {
                ts.Add(tour, _tourLogic.ReadElement(new TourSearchModel { Id = tour }));
            }
            _logic.Update(new TravelBindingModel
            {
                Id = id,
                TravelName = travelname,
                TravelDescription = description,
                DateStart = datestart,
                TourTravels = ts
            }); ;
            Response.Redirect("/Travel/Travel");
            return;
        }
        

        [HttpPost("/travel/deletetravel/{id?}")]
        public void DeleteTravel(int id)
        {
            
                if (APIClient.Tourist == null)
                {
                    throw new Exception("Вы как сюда попали? Сюда вход только авторизованным");
                }

            _logic.Delete( new TravelBindingModel { Id = id });
            Response.Redirect("/Travel/Travel");
        }
    }
}