using Azure;
using FluentNHibernate;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using TravelCompanyClientApp.Models;
using TravelCompanyContracts.BindingModels.TouristBindingModels;
using TravelCompanyContracts.BusinessLogicsContracts.TouristBusinessLogicsContracts;
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 PlaceController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IPlaceLogic _logic;
        private readonly IExcursionLogic _excursionLogic;

        public PlaceController(ILogger<HomeController> logger, IPlaceLogic logic, IExcursionLogic excursionLogic)
        {
            _logger = logger;
            _logic = logic;
            _excursionLogic = excursionLogic;
        }
        [HttpGet]
        public IActionResult Place()
        {
            if (APIClient.Tourist == null)
            {
                return Redirect("~/Home/Enter");
            }
            return View(_logic.ReadList(new PlaceSearchModel { TouristId = APIClient.Tourist.Id }));

        }

        [HttpGet]
        public IActionResult CreatePlace()
        {
            ViewBag.Excursions = _excursionLogic.ReadList(new ExcursionSearchModel {TouristId= APIClient.Tourist.Id});
            return View();
        }

        [HttpPost]
        public void CreatePlace(string placename, string address, int excursion)
        {
            if (string.IsNullOrEmpty(placename) || string.IsNullOrEmpty(address) || excursion==null)
            {
                throw new Exception("Введите логин, пароль и ФИО");
            }
            _logic.Create( new PlaceBindingModel
            {
                PlaceName = placename,
                PlaceAddress = address,
                ExcursionId = excursion,
                TouristId = APIClient.Tourist.Id
            });
            Response.Redirect("Place");
            return;
        }
        [HttpGet("/place/updateplace/{id?}")]
        public IActionResult UpdatePlace(int id)
        {
            if (APIClient.Tourist == null)
            {
                return Redirect("~/Home/Enter");
            }
            ViewBag.Excursions = _excursionLogic.ReadList(new ExcursionSearchModel { TouristId = APIClient.Tourist.Id });
            return View(_logic.ReadElement(new PlaceSearchModel { TouristId = APIClient.Tourist.Id, Id=id})) ;
        }
        [HttpPost("/place/updateplace/{id?}")]
        public void UpdatePlace(int id, string placename, string address, int excursion)
        {
            if (string.IsNullOrEmpty(placename) || string.IsNullOrEmpty(address) || excursion == null)
            {
                throw new Exception("Введите логин, пароль и ФИО");
            }
            _logic.Update( new PlaceBindingModel
            {
                Id = id,
              PlaceName = placename,
              PlaceAddress = address,
              ExcursionId = excursion,
           TouristId=APIClient.Tourist.Id
          });
            Response.Redirect("/Place/Place");
            return;
        }
        

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

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