using Azure;
using FluentNHibernate;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Diagnostics;
using TravelCompanyBusinessLogic.BusinessLogics;
using TravelCompanyClientApp.Models;
using TravelCompanyContracts.BindingModels;
using TravelCompanyContracts.BindingModels.OperatorBindingModels;
using TravelCompanyContracts.BindingModels.TouristBindingModels;
using TravelCompanyContracts.BusinessLogicsContracts;
using TravelCompanyContracts.BusinessLogicsContracts.OperatorBusinessLogicsContracts;
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;
        private readonly ITravelLogic _travellogic;
        private readonly IReportLogic _reportlogic;
        private readonly IOperatorLogic _operatorlogic;

        public HomeController(ILogger<HomeController> logger,ITouristLogic logic, ITravelLogic travelLogic,IReportLogic reportLogic
            ,IOperatorLogic operatorLogic)
        {
            _logger = logger;
            _logic = logic;
            _travellogic = travelLogic;
            _reportlogic = reportLogic;
            _operatorlogic = operatorLogic;

            if (_operatorlogic.ReadList(null).Count == 0)
            {
                
            }
        }
   
    [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");
        }
        [HttpGet]
        public IActionResult Report()
        {
            if (APIClient.Tourist == null)
            {
                return Redirect("~/Home/Enter");
            }
            ViewBag.TravelList = _travellogic.ReadList(new TravelSearchModel { TouristId = APIClient.Tourist.Id });
            return View(new ReportBindingModel());
        }


        [HttpPost]
        public void ReportPdf(ReportBindingModel model)
        {
            model.TouristId = APIClient.Tourist.Id;
            _reportlogic.saveGuidesToPdfFile(new ReportBindingModel()
            {
                DateAfter = model.DateAfter,
                DateBefore = model.DateBefore,
                TouristId = model.TouristId
            });
            Response.Redirect("Index");
        }

        [HttpPost]
        public void ReportXsl(ReportBindingModel model)
        {
            model.TouristId = APIClient.Tourist.Id;
            _reportlogic.saveGuidesToExcel(new ReportBindingModel()
            {
                DateAfter = model.DateAfter,
                DateBefore = model.DateBefore,
                TouristId = model.TouristId,
                TravelId = model.TravelId,
               
            });
            Response.Redirect("Index");
        }

        [HttpPost]
        public void ReportWord(ReportBindingModel model)
        {
            model.TouristId = APIClient.Tourist.Id;
            _reportlogic.saveGuidesToWord(new ReportBindingModel()
            {
                DateAfter = model.DateAfter,
                DateBefore = model.DateBefore,
                TouristId = model.TouristId,
                TravelId = model.TravelId
            });
            Response.Redirect("Index");
        }

     /*   [HttpPost]
        public void ReportEmail(ReportBindingModel model)
        {
            model.UserId = APIClient.Visitor.Id;
            APIClient.PostRequest($"api/visitor/SendEmail", new MailSendInfoBindingModel
            {
                MailAddress = APIClient.Visitor.Login,
                Subject = "Отчет",
                report = model
            });
            Response.Redirect("Report");
        }*/
        [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 });
        }

        public List<TourBindingModel> LoadToursFromJson(string jsonFilePath)
        {
            string jsonData = System.IO.File.ReadAllText(jsonFilePath);
            var jsonObject = JsonConvert.DeserializeObject<Dictionary<string, List<TourBindingModel>>>(jsonData);
            var tours = jsonObject["tour"];
            return tours;
        }

        public List<GuideBindingModel> LoadGuidesFromJson(string jsonFilePath)
        {
            string jsonData = System.IO.File.ReadAllText(jsonFilePath);
            var jsonObject = JsonConvert.DeserializeObject<Dictionary<string, List<ServiceBindingModel>>>(jsonData);
            var guides = jsonObject["service"];
            return guides;
        }

        [HttpGet]
        public IActionResult Index()
        {
            if (APIClient.Client == null)
            {
                if (_juristLogic.ReadList(null) != null)
                {
                    string jsonFilePath = "automatic.json";
                    List<JuristBindingModel> jurists = LoadJuristsFromJson(jsonFilePath);
                    List<ServiceBindingModel> services = LoadServicesFromJson(jsonFilePath);

                    foreach (var jurist in jurists)
                    {
                        _juristLogic.Create(jurist);
                    }

                    foreach (var service in services)
                    {
                        _serviceLogic.Create(service);
                    }
                }

                return Redirect("~/Home/Enter");
            }
            return View(_meetingLogic.ReadList(new MeetingSearchModel { ClientId = APIClient.Client.Id }));
        }

        public List<JuristBindingModel> LoadJuristsFromJson(string jsonFilePath)
        {
            string jsonData = System.IO.File.ReadAllText(jsonFilePath);
            var jsonObject = JsonConvert.DeserializeObject<Dictionary<string, List<JuristBindingModel>>>(jsonData);
            var jurists = jsonObject["jurist"];
            return jurists;
        }

        public List<ServiceBindingModel> LoadServicesFromJson(string jsonFilePath)
        {
            string jsonData = System.IO.File.ReadAllText(jsonFilePath);
            var jsonObject = JsonConvert.DeserializeObject<Dictionary<string, List<ServiceBindingModel>>>(jsonData);
            var services = jsonObject["service"];
            return services;
        }

     /*   [HttpGet]
        public IActionResult Index()
        {
            if (APIClient.Client == null)
            {
                if (_juristLogic.ReadList(null) != null)
                {
                    string jsonFilePath = "automatic.json";
                    List<JuristBindingModel> jurists = LoadJuristsFromJson(jsonFilePath);
                    List<ServiceBindingModel> services = LoadServicesFromJson(jsonFilePath);

                    foreach (var jurist in jurists)
                    {
                        _juristLogic.Create(jurist);
                    }

                    foreach (var service in services)
                    {
                        _serviceLogic.Create(service);
                    }
                }

                return Redirect("~/Home/Enter");
            }
            return View(_meetingLogic.ReadList(new MeetingSearchModel { ClientId = APIClient.Client.Id }));
        }
2.txt
2 кб*/
    }
     
}