using DocumentFormat.OpenXml.Bibliography;
using DocumentFormat.OpenXml.Drawing.Charts;
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Wordprocessing;
using System.Linq;
using TravelCompanyBusinessLogic.OfficePackage;
using TravelCompanyBusinessLogic.OfficePackage.HelperModels;
using TravelCompanyContracts.BindingModels;
using TravelCompanyContracts.BusinessLogicsContracts;
using TravelCompanyContracts.BusinessLogicsContracts.OperatorBusinessLogicsContracts;
using TravelCompanyContracts.SearchModels.OperatorSearchModels;
using TravelCompanyContracts.SearchModels.TouristSearchModels;
using TravelCompanyContracts.StoragesContracts.OperatorStoragesContracts;
using TravelCompanyContracts.StoragesContracts.TouristStoragesContracts;
using TravelCompanyContracts.ViewModels;
using TravelCompanyContracts.ViewModels.OperatorViewModels;
using TravelCompanyContracts.ViewModels.TouristViewModels;

namespace TravelCompanyBusinessLogic.BusinessLogics
{
    public class ReportLogic : IReportLogic
    {
        private readonly ITravelStorage travelStorage;
        private readonly IExcursionStorage excursionStorage;
        private readonly IGuideStorage guideStorage;
        private readonly AbstractSaveToPdf saveToPdf;
        private readonly AbstractSaveToWord saveToWord;
        private readonly AbstractSaveToExcel saveToExcel;
        public ReportLogic(ITravelStorage travelStorage, IExcursionStorage excursionStorage, 
            IGuideStorage guideStorage,
            AbstractSaveToPdf saveToPdf, AbstractSaveToWord saveToWord, AbstractSaveToExcel saveToExcel)
        {
            this.travelStorage = travelStorage;
            this.guideStorage = guideStorage;
            this.excursionStorage = excursionStorage;
            this.saveToPdf = saveToPdf;
            this.saveToWord = saveToWord;
            this.saveToExcel = saveToExcel;
        }
        public List<ReportTravelsPCView> GetTravelsPCView(ReportBindingModel model)
        {
            var list = new List<ReportTravelsPCView>();

            var travels = travelStorage.GetFilteredList(new TravelSearchModel
            {
                DateFrom =model.DateAfter,
                DateTo = model.DateBefore,
                TouristId = model.TouristId
            });

            foreach (var travel in travels)
            {
                var record = new ReportTravelsPCView
                {
                    DateCreate = travel.DateStart,
                    Excursions = new List<ExcursionViewModel>(),
                    Guids = new List<GuideViewModel>()
                };

                var excursions = travel.ExcursionTravels.Keys.ToList();
                foreach (var excrsionId in excursions)
                {
                    var excursion = excursionStorage.GetElement(new ExcursionSearchModel { Id = excrsionId });
                    record.Excursions.Add(excursion);
                    var excursionguide = excursion.GuideExcursions.Keys.ToList();
                    foreach (var guideId in excursionguide)
                    {
                        var guide = guideStorage.GetElement(new GuideSearchModel { Id = guideId });
                        record.Guids.Add(guide);
                    }
                }
                list.Add(record);
            }

            return list;
        }
        public List<ReportGuidesPCView> GetGuidesPCView(ReportBindingModel model)
        {
            var list = new List<ReportGuidesPCView>();

            var guides = guideStorage.GetFilteredList(null);

            var travels = travelStorage.GetFilteredList(new TravelSearchModel
            {
                DateFrom = model.DateAfter,
                DateTo = model.DateBefore,
            });

            foreach (var guide in guides)
            {
                var record = new ReportGuidesPCView
                {
                    GuideId = guide.Id,
                    FIO = guide.Surname+" "+guide.Name,
                    Travels = new List<TravelViewModel>()
                };

                foreach (var travel in travels)
                {
                    foreach(var excursion in travel.ExcursionTravels)
                    {
                        var findExcursion = excursionStorage.GetElement(new ExcursionSearchModel { Id = excursion.Key });
                        if (findExcursion.GuideExcursions.ContainsKey(guide.Id))
                        {
                            record.Travels.Add(travel);
                            break;
                        }
                    }
                }

                if (record.Travels.Count > 0) list.Add(record);
            }

            return list;
        }
        public List<ReportGuideView> GetGuidesByTravels(ReportBindingModel model)
        {
            var list = new List<ReportGuideView>();

            var traveles = travelStorage.GetFilteredList(new TravelSearchModel
            {
                TouristId = model.TouristId
            }).Where(x => model.TravelId.Contains(x.Id)).ToList();


            foreach (var travel in traveles)
            {
                var record = new ReportGuideView
                {
                    Travel = travel,
                    Guides = new List<GuideViewModel>()
                };
                var travelExcursions = travel.ExcursionTravels.Keys.ToList();
                foreach (var excursionId in travelExcursions)
                {
                    var excursion = excursionStorage.GetElement(new ExcursionSearchModel { Id = excursionId });
                    var excursionGuides = excursion.GuideExcursions.Keys.ToList();

                    foreach (var guideId in excursionGuides)
                    {
                        if (record.Guides.Where(guide => guide.Id == guideId).ToList().Count == 0)
                        {
                            var guide = guideStorage.GetElement(new GuideSearchModel { Id = guideId });
                            record.Guides.Add(guide);
                        }
                    }
                }

                list.Add(record);
            }
            return list;
        }
      /*  private List<ReportExcursionView> GetExcursionsByTours(ReportBindingModel model)
        {
            var list = new List<ReportExcursionView>();

            var tours = tourStorage.GetFilteredList(null).Where(x => model.TourId.Contains(x.Id));

            foreach (var tour in tours)
            {
                var record = new ReportExcursionView
                {
                    Tour = tour,
                    Excursions = new List<ExcursionViewModel>()
                };
                var tourGuide = tour.Gui.Keys.ToList();
                foreach (var guideId in tourGuide)
                {
                    var excursions = excursionStorage.GetExcursionGuidesList(new ExcursionSearchModel { GuideId = guideId });
                    excursions.ForEach(x =>
                    {
                        if (record.Excursions.Find(y => y.Id == x.Id) == null) record.Excursions.Add(x);
                    });
                }
                list.Add(record);
            }
            return list;
        }*/
        public void saveTravelesToPdfFile(ReportBindingModel model)
        {
            saveToPdf.CreateTravelDoc(new PdfInfo
            {
                Title = "Список обедов",
                DateAfter = model.DateAfter.Value,
                DateBefore = model.DateBefore.Value,
                Traveles = GetTravelsPCView(model)
        });
        }
        public void saveGuidesToPdfFile(ReportBindingModel model)
        {
            saveToPdf.CreateGuideDoc(new PdfInfo
            {
                Title = "Список поваров",
                DateAfter = model.DateAfter.Value,
                DateBefore = model.DateBefore.Value,
                FileName = (model.FileName != null) ? model.FileName : $"C:\\PdfReports\\{DateTime.Now.ToString("HH.mm.ss_dd.MM.yyyy")}.pdf",
                Guides = GetGuidesPCView(model)
            });
        }
        public void saveGuidesToExcel(ReportBindingModel model)
        {
            saveToExcel.CreateGuidesReport(new ExcelInfo()
            {
                Title = "Список поваров:",
                Guides = GetGuidesByTravels(model)
            });
        }
      /*  public void saveExcursionsToExcel(ReportBindingModel model)
        {
            saveToExcel.CreateExcursionsReport(new ExcelInfo()
            {
                Title = "Список заказов:",
                Excursions = GetExcursionsByTours(model)
            });
        }*/
        public void saveGuidesToWord(ReportBindingModel model)
        {
            saveToWord.CreateGuidesDoc(new WordInfo()
            {
                Title = "Список поваров",
                Guides = GetGuidesByTravels(model)
            });
        }
       /* public void saveExcursionsToWord(ReportBindingModel model)
        {
            saveToWord.CreateExcursionsDoc(new WordInfo()
            {
                Title = "Список заказов",
                Excursions = GetExcursionsByTours(model)
            });
        }*/
    }
}
