using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations.Schema;
using TravelCompanyDataModels.Models.TouristModels;
using TravelCompanyDataModels.Models.OperatorModels;
using TravelCompanyContracts.BindingModels.TouristBindingModels;
using TravelCompanyContracts.ViewModels.TouristViewModels;
using TravelCompanyDatabaseImplement.Models.OperatorModels;

namespace TravelCompanyDatabaseImplement.Models.TouristModel
{
    public class Travel : ITravelModel
    {
        public int Id { get; set; }
        [Required]
        public string TravelName { get; set; } = string.Empty;
        [Required]
        public DateTime DateStart { get; set; } = DateTime.Now;
        [Required]
        public string TravelDescription { get; private set; } = string.Empty;
        [Required]
        public int TouristId { get; private set; }
        public virtual Tourist Tourist { get; set; }
        public Dictionary<int, ITourModel>? _tourTravels = null;
        [NotMapped]
        public Dictionary<int, ITourModel> TourTravels
        {
            get
            {
                if (_tourTravels == null)
                {
                    _tourTravels = Tours.ToDictionary(rec => rec.TourId, rec => rec.Tour as ITourModel);
                }
                return _tourTravels;
            }
        }
        public Dictionary<int, IExcursionModel>? _excursionTravels = null;
        [NotMapped]
        public Dictionary<int, IExcursionModel> ExcursionTravels
        {
            get
            {
                if (_excursionTravels == null)
                {
                    _excursionTravels = Excursions.ToDictionary(rec => rec.ExcursionId,
                        rec => rec.Excursion as IExcursionModel);
                }
                return _excursionTravels;
            }
        }

        [ForeignKey("TravelId")]
        public virtual List<ExcursionTravel> Excursions { get; set; } = new();
        [ForeignKey("TravelId")]
        public virtual List<TourTravel> Tours { get; set; } = new();

        public static Travel? Create(TravelCompanyDatabase context, TravelBindingModel model)
        {
            if (model == null)
            {
                return null;
            }

            return new Travel()
            {
                Id = model.Id,
                TravelName = model.TravelName,
                DateStart = model.DateStart,
                TouristId = model.TouristId,
                TravelDescription = model.TravelDescription,
                Tours = model.TourTravels.Select(x => new TourTravel
                {
                    Tour = context.Tours.First(y => y.Id == x.Key)
                }).ToList()
            };
        }
        public void Update(TravelCompanyDatabase context, TravelBindingModel model)
        {
            if (model == null)
            {
                return;
            }
            TravelName = model.TravelName;
            DateStart = model.DateStart;
            TravelDescription = model.TravelDescription;

            var TourTravels = context.TourTravels.Where(rec => rec.TravelId == model.Id).ToList();
            if (TourTravels!= null && TourTravels.Count > 0)
            {
                // удалили те, которых нет в модели
                context.TourTravels.RemoveRange(TourTravels.Where(rec => model.Id == rec.Travel.Id));
                context.SaveChanges();
            }
            var ExcursionTravels = context.ExcursionTravels.Where(rec =>
               rec.TravelId == model.Id).ToList();
            if (ExcursionTravels != null && ExcursionTravels.Count > 0)
            {
                // удалили те, которых нет в модели
                context.ExcursionTravels.RemoveRange(ExcursionTravels.Where(rec => model.Id == rec.Travel.Id));
                context.SaveChanges();
            }
            var Travel = context.Travels.First(x => x.Id == Id);
            foreach (var rc in model.TourTravels)
            {
                context.TourTravels.Add(new TourTravel
                {
                    Travel = Travel,
                    Tour = context.Tours.First(x => x.Id == rc.Key),
                });
                context.SaveChanges();
            }
            _tourTravels = null;
            try
            {
                foreach (var rc in model.ExcursionTravels)
                {
                    context.ExcursionTravels.Add(new ExcursionTravel
                    {
                        Travel = Travel,
                        Excursion = context.Excursions.First(x => x.Id == rc.Key),
                    });
                    context.SaveChanges();
                }
                _tourTravels = null;
            }catch (Exception ex) { }
        }
        public TravelViewModel GetViewModel => new()
        {
            Id = Id,
            TravelName = TravelName,
            DateStart = DateStart,
            TouristId = TouristId,
            TravelDescription = TravelDescription,
            TourTravels = TourTravels,
            ExcursionTravels = ExcursionTravels
        };
    }
}
