using TypographyContracts.BindingModels;
using TypographyContracts.SearchModels;
using TypographyContracts.StoragesContracts;
using TypographyContracts.ViewModels;
using TypographyDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using TypographyDatabaseImplements.Models;

namespace TypographyDatabaseImplements.Implements
{
    public class PrintedStorage : IPrintedStorage
    {
        public List<PrintedViewModel> GetFullList()
        {
            using var context = new TypographyDatabase();
            return context.Printeds
                    .Include(x => x.Components)
                    .ThenInclude(x => x.Component)
                    .ToList()
                    .Select(x => x.GetViewModel)
                    .ToList();
        }

        public List<PrintedViewModel> GetFilteredList(PrintedSearchModel model)
        {
            if (string.IsNullOrEmpty(model.PrintedName))
            {
                return new();
            }
            using var context = new TypographyDatabase();
            return context.Printeds
                    .Include(x => x.Components)
                    .ThenInclude(x => x.Component)
                    .Where(x => x.PrintedName.Contains(model.PrintedName))
                    .ToList()
                    .Select(x => x.GetViewModel)
                    .ToList();
        }

        public PrintedViewModel? GetElement(PrintedSearchModel model)
        {
            if (string.IsNullOrEmpty(model.PrintedName) && !model.Id.HasValue)
            {
                return null;
            }
            using var context = new TypographyDatabase();
            return context.Printeds
                .Include(x => x.Components)
                .ThenInclude(x => x.Component)
                .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PrintedName) && x.PrintedName == model.PrintedName) ||
                                (model.Id.HasValue && x.Id == model.Id))
                ?.GetViewModel;
        }

        public PrintedViewModel? Insert(PrintedBindingModel model)
        {
            using var context = new TypographyDatabase();
            var newPrinted = Printed.Create(context, model);
            if (newPrinted == null)
            {
                return null;
            }
            context.Printeds.Add(newPrinted);
            context.SaveChanges();
            return newPrinted.GetViewModel;
        }

        public PrintedViewModel? Update(PrintedBindingModel model)
        {
            using var context = new TypographyDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var printed = context.Printeds.FirstOrDefault(rec => rec.Id == model.Id);
                if (printed == null)
                {
                    return null;
                }
                printed.Update(model);
                context.SaveChanges();
                printed.UpdateComponents(context, model);
                transaction.Commit();
                return printed.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }

        public PrintedViewModel? Delete(PrintedBindingModel model)
        {
            using var context = new TypographyDatabase();
            var element = context.Printeds
                .Include(x => x.Components)
                .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Printeds.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}