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;
using VisioForge.MediaFramework.Helpers;

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]
       
   
    [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) || string.IsNullOrEmpty(email) 
                || string.IsNullOrEmpty(surname) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(patronymic) || string.IsNullOrEmpty(birthday))
            {
                throw new Exception("Неверно введены личные данные");
            }
            _logic.Update( 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");
        }
        
        [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()
        {
            if (APIClient.Tourist == null)
            {
                return Redirect("~/Home/Enter");
            }
            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) || string.IsNullOrEmpty(email)
                 || string.IsNullOrEmpty(surname) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(patronymic) || string.IsNullOrEmpty(birthday))
            {
                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;
        }
       
        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}