using FishFactoryContracts.BindingModels;
using FishFactoryContracts.SearchModels;
using FishFactoryContracts.StoragesContracts;
using FishFactoryContracts.ViewModels;
using FishFactoryDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FishFactoryDatabaseImplement.Implements
{
    public class CannedStorage : ICannedStorage
    {
        public List<CannedViewModel> GetFullList()
        {
            using var context = new FishFactoryDatabase();
            return context.Canneds
             .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public List<CannedViewModel> GetFilteredList(CannedSearchModel model)
        {
            if (string.IsNullOrEmpty(model.CannedName))
            {
                return new();
            }
            using var context = new FishFactoryDatabase();
            return context.Canneds.Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .Where(x => x.CannedName.Contains(model.CannedName))
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public CannedViewModel? GetElement(CannedSearchModel model)
        {
            if (string.IsNullOrEmpty(model.CannedName) &&
            !model.Id.HasValue)
            {
                return null;
            }
            using var context = new FishFactoryDatabase();
            return context.Canneds
             .Include(x => x.Components)
             .ThenInclude(x => x.Component)
             .FirstOrDefault(x => (!string.IsNullOrEmpty(model.CannedName) &&
            x.CannedName == model.CannedName) ||
            (model.Id.HasValue && x.Id ==
            model.Id))
            ?.GetViewModel;
        }
        public CannedViewModel? Insert(CannedBindingModel model)
        {
            using var context = new FishFactoryDatabase();
            var newCanned = Canned.Create(context, model);
            if (newCanned == null)
            {
                return null;
            }
            context.Canneds.Add(newCanned);
            context.SaveChanges();
            return newCanned.GetViewModel;
        }
        public CannedViewModel? Update(CannedBindingModel model)
        {
            using var context = new FishFactoryDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var canned = context.Canneds.FirstOrDefault(rec =>
                rec.Id == model.Id);
                if (canned == null)
                {
                    return null;
                }
                canned.Update(model);
                context.SaveChanges();
                canned.UpdateComponents(context, model);
                transaction.Commit();
                return canned.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
        public CannedViewModel? Delete(CannedBindingModel model)
        {
            using var context = new FishFactoryDatabase();
            var element = context.Canneds
            .Include(x => x.Components)
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Canneds.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}
