using DressAtelierContracts.BindingModels;
using DressAtelierContracts.SearchModels;
using DressAtelierContracts.StorageContracts;
using DressAtelierContracts.ViewModels;
using DressAtelierDatabaseImplementation.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace DressAtelierDatabaseImplementation.Implements
{
    public class DressStorage : IDressStorage
    {
        public List<DressViewModel> GetFullList()
        {
            using var context = new DressAtelierDatabase();
            return context.Dresses.Include(x => x.Materials).ThenInclude(x => x.Material).ToList().Select(x => x.GetViewModel).ToList();
        }

        public List<DressViewModel> GetFilteredList(DressSearchModel model)
        {
            using var context = new DressAtelierDatabase();
            return context.Dresses.Include(x => x.Materials).ThenInclude(x => x.Material).Where(x => x.ID == model.ID).ToList().Select(x => x.GetViewModel).ToList();
        }

        public DressViewModel? GetElement(DressSearchModel model)
        {
            if (string.IsNullOrEmpty(model.DressName) && !model.ID.HasValue)
            {
                return null;
            }
            using var context = new DressAtelierDatabase();
            return context.Dresses.Include(x => x.Materials).ThenInclude(x => x.Material).FirstOrDefault(x => (!string.IsNullOrEmpty(model.DressName) && x.DressName == model.DressName) || (model.ID.HasValue && x.ID == model.ID))?.GetViewModel;
        }

        public DressViewModel? Insert(DressBindingModel model)
        {
            using var context = new DressAtelierDatabase();
            var newDress = Dress.Create(context,model);
            if (newDress == null)
            {
                return null;
            }
            context.Dresses.Add(newDress);
            context.SaveChanges();
            return newDress.GetViewModel;
        }

        public DressViewModel? Update(DressBindingModel model)
        {
            using var context = new DressAtelierDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var dress = context.Dresses.FirstOrDefault(x => x.ID == model.ID);
                if(dress == null)
                {
                    return null;
                }
                dress.Update(model);
                context.SaveChanges();
                dress.UpdateComponents(context, model);
                transaction.Commit();
                return dress.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
         
        }

        public DressViewModel? Delete(DressBindingModel model)
        {
            using var context = new DressAtelierDatabase();
            var dress = context.Dresses.Include(x => x.Materials).FirstOrDefault(x => x.ID == model.ID);
            if(dress == null)
            {
                return null;
            }
            context.Dresses.Remove(dress);
            context.SaveChanges();
            return dress.GetViewModel;
        }
    }
}
