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 ExcursionController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IExcursionLogic _logic;


        public ExcursionController(ILogger<HomeController> logger, IExcursionLogic logic)
        {
            _logger = logger;
            _logic = logic;
        }
        [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 (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 name, string description, int duration)
        {
            if (APIClient.Tourist == null)
            {
                throw new Exception("Доступно только авторизованным пользователям");
            }
            //if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))
            //{
            //    throw new Exception("Введите логин, пароль и ФИО");
            //}
          _logic.Update( new ExcursionBindingModel
            {
                Id = id,
                ExcursionName = name,
                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");
        }
    }
}