using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooContracts.BindingModels;
using ZooContracts.SearchModel;
using ZooContracts.StoragesContracts;
using ZooContracts.ViewModels;
using ZooDatabaseImplements;
using ZooDatabaseImplements.Models;

namespace ZooDatabaseImplements.Implements
{
    public class RouteCostStorage : IRouteCostStorage
    {
        public RouteCostViewModel? Delete(RouteCostBindingModel model)
        {
            using var context = new ZooDatabase();
            var RouteCost = context.RouteCosts.FirstOrDefault(rec => rec.Id == model.Id);
            if (RouteCost != null)
            {
                context.RouteCosts.Remove(RouteCost);
                context.SaveChanges();
                return RouteCost.GetViewModel;
            }
            return null;
        }

        public RouteCostViewModel GetElement(RouteCostSearchModel model)
        {
            if (model == null)
            {
                return null;
            }
            using var context = new ZooDatabase();
            if (model.Id.HasValue)
            {
                return context.RouteCosts.Include(x => x.Route)
                    .Include(x => x.Cost)
                    .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
            }
            return null;
        }

        public List<ReportCostsInRoutesViewModel> GetCostsInRoutes(RouteCostSearchModel model)
        {
            if (model.DateFrom == null || model.DateTo == null)
            {
                return new();
            }
            using var context = new ZooDatabase();
            return context.RouteCosts.Include(x => x.Route)
                    .Include(x => x.Cost)
                    .Select(x => new ReportCostsInRoutesViewModel()
                    {
                        RouteId = x.Route.Id,
                        CostName = x.Cost.CostName,
                        Count = x.Cost.Count,
                        Sum = x.Cost.Sum
                    })
                    .ToList();
        }
        public List<RouteCostViewModel> GetFilteredList(RouteCostSearchModel model)
        {
            using var context = new ZooDatabase();
            return context.RouteCosts
                .Where(x => x.Id == model.Id)
                .Include(x => x.Route)
                .Include(x => x.Cost)
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public List<RouteCostViewModel> GetFullList()
        {
            using var context = new ZooDatabase();
            return context.RouteCosts
                .Include(x => x.Route)
                .Include(x => x.Cost)
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public RouteCostViewModel? Insert(RouteCostBindingModel model)
        {
            using var context = new ZooDatabase();
            var newRouteCost = RouteCost.Create(context, model);
            if (newRouteCost == null)
            {
                return null;
            }
            context.RouteCosts.Add(newRouteCost);
            context.SaveChanges();
            return newRouteCost.GetViewModel;
        }

        public RouteCostViewModel? Update(RouteCostBindingModel model)
        {
            using var context = new ZooDatabase();
            var RouteCost = context.RouteCosts.FirstOrDefault(x => x.Id == model.Id);
            if (RouteCost == null)
            {
                return null;
            }
            RouteCost.Update(model);
            context.SaveChanges();
            return RouteCost.GetViewModel;
        }
    }
}
