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


        public HomeController(ILogger<HomeController> logger,ITouristLogic logic)
        {
            _logger = logger;
            _logic = logic;
        }
        [HttpGet]
        public IActionResult Index()
        {
            if (APIClient.Tourist == null)
            {
                return Redirect("~/Home/Enter");
            }
            return View(APIClient.GetRequest<List<TravelViewModel>>($"api/travel/gettravels?touristId={APIClient.Tourist.Id}"));
        }
    
    [HttpGet]
    public IActionResult CreateTravel()
    {
            if (APIClient.Tourist == null)
            {
                throw new Exception("Вы как суда попали? Суда вход только авторизованным");
            }
            return View(APIClient.GetRequest<List<TourViewModel>>($"api/tour/gettours"));
    }

        [HttpPost]
        public void CreateTravel(List<int> selectedTours, string travelname, string datestart, string description)
        {
            //if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))
            //{
            //    throw new Exception("Введите логин, пароль и ФИО");
            //}
            var model = new TravelBindingModel
            {
                TravelName = travelname,
                TravelDescription = description,
                DateStart = DateTime.Parse(datestart),
                TourTravels = new Dictionary<int, ITourModel>(),
                TouristId = APIClient.Tourist.Id
            };
            foreach(var tourId in selectedTours)
            {
                var modelTour = APIClient.GetRequest<TourViewModel>($"api/tour/gettour?id={tourId}");
                model.TourTravels.Add(tourId, modelTour);
            };

            APIClient.PostRequest("api/travel/createtravel", model);
            Response.Redirect("Excursion");
            return;
        }
    [HttpGet]
        public IActionResult Privacy()
        {
            if (APIClient.Tourist == null)
            {
                return Redirect("~/Home/Enter");
            }
            return View(APIClient.Tourist);
        }
        [HttpPost]
        public void Privacy(string login, string email, string password, string surname, string name, string patronymic, string birthday)
        {
            if (APIClient.Tourist == null)
            {
                throw new Exception("Вы как суда попали? Суда вход только авторизованным");
            }
            //доделать проверку
            if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
            {
                throw new Exception("Введите логин, пароль и ФИО");
            }
            APIClient.PostRequest("api/tourist/updatedata", new TouristBindingModel
            {
                Id = APIClient.Tourist.Id,
                Surname=surname,
                Name=name,
                Patronymic=patronymic,
                BirthDay= DateTime.Parse(birthday),
                Login = login,
                Email = login,
                Password = password
            });

            APIClient.Tourist.Surname = surname;
            APIClient.Tourist.Name = name;
            APIClient.Tourist.Patronymic = patronymic;
            APIClient.Tourist.BirthDay = DateTime.Parse(birthday);
            APIClient.Tourist.Login = login;
            APIClient.Tourist.Email = email;
            APIClient.Tourist.Password = password;
            Response.Redirect("Privacy");
        }
        
              //crud place
     
        
        [HttpGet]
        public IActionResult Enter()
        {
            return View();
        }

        [HttpPost]
        public void Enter(string login, string password)
        {
            if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
            {
                throw new Exception("Введите логин и пароль");
            }
            APIClient.Tourist = _logic.ReadElement(new TouristSearchModel
            {
                Login = login,
                Password = password
            });
            if (APIClient.Tourist == null)
            {
                throw new Exception("Неверный логин/пароль");
            }
            Response.Redirect("Privacy");
        }

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

        [HttpPost]
        public void Register(string login, string email, string password, string surname, string name, string patronymic, string birthday)
        {
            if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) )
            {
                throw new Exception("Введите логин, пароль и ФИО");
            }
			_logic.Create( new TouristBindingModel
            {
                Login = login,
                Email = email,
                Password = password,
                Surname = surname,
                Name = name,
                Patronymic = patronymic,
                BirthDay = DateTime.Parse(birthday)
            });
            Response.Redirect("Enter");
            return;
        }
       








        /*[HttpPost]
        public void CreateTravel(string travelname, string datestart, string traveldesc, List<int> tours)
        {
            if (APIClient.Client == null)
            {
                throw new Exception("Вы как суда попали? Суда вход только авторизованным");
            }
            if (count <= 0)
            {
                throw new Exception("Количество и сумма должны быть больше 0");
            }
            APIClient.PostRequest("api/main/createorder", new OrderBindingModel
            {
                ClientId = APIClient.Client.Id,
                ReinforcedId = Reinforced,
                Count = count,
                Sum = Calc(count, Reinforced)
            });
            Response.Redirect("Index");
        }
        */
        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}