using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TravelCompanyContracts.BindingModels.TouristBindingModels;
using TravelCompanyContracts.SearchModels.TouristSearchModels;
using TravelCompanyContracts.StoragesContracts.TouristStoragesContracts;
using TravelCompanyContracts.ViewModels.TouristViewModels;
using TravelCompanyDatabaseImplement.Models.TouristModel;

namespace TravelCompanyDatabaseImplement.Implements
{
    public class PlaceStorage : IPlaceStorage
    {
        public List<PlaceViewModel> GetFullList()
        {
            using var context = new TravelCompanyDatabase();
            return context.Places
                 .Include(x => x.Excursion)
            .Select(x => x.GetViewModel)
           .ToList();
        }
        public List<PlaceViewModel> GetFilteredList(PlaceSearchModel model)
        {
            if (model.Id.HasValue)
            {
                return new();
            }
            using var context = new TravelCompanyDatabase();
            return context.Places
            .Where(x => x.TouristId == model.TouristId)
            .Include (x => x.Excursion)
               .Select(x => x.GetViewModel)
               .ToList();
        }
        public PlaceViewModel? GetElement(PlaceSearchModel model)
        {
            if (string.IsNullOrEmpty(model.PlaceName) && !model.Id.HasValue)
            {
                return null;
            }
            using var context = new TravelCompanyDatabase();
            return context.Places.Include(x => x.Excursion)
            .FirstOrDefault(x =>
          ( !string.IsNullOrEmpty(model.PlaceName) && x.PlaceName ==
           model.PlaceName )||
           ( model.Id.HasValue && x.Id == model.Id))
            ?.GetViewModel;
        }
        public PlaceViewModel? Insert(PlaceBindingModel model)
        {
            using var context = new TravelCompanyDatabase();
            var newComponent = Place.Create(model);
            if (newComponent == null)
            {
                return null;
            }
            
            context.Places.Add(newComponent);
            context.SaveChanges();
            return newComponent.GetViewModel;
        }
        public PlaceViewModel? Update(PlaceBindingModel model)
        {
            using var context = new TravelCompanyDatabase();
            var component = context.Places
                .Include(x => x.Excursion)
                .FirstOrDefault(x => x.Id == model.Id);
            if (component == null)
            {
                return null;
            }
            component.Update(model);
            context.SaveChanges();
            return component.GetViewModel;
        }
        public PlaceViewModel? Delete(PlaceBindingModel model)
        {
            using var context = new TravelCompanyDatabase();
            var element = context.Places.FirstOrDefault(rec => rec.Id ==
           model.Id);
            if (element != null)
            {
                context.Places.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}
