using BarContracts.BindingModels;
using BarContracts.SearchModels;
using BarContracts.StoragesContracts;
using BarContracts.ViewModels;
using BarDatabaseImplement.Models;

namespace BarDatabaseImplement.Implements
{
    public class ImplementerStorage : IImplementerStorage
    {
        public List<ImplementerViewModel> GetFullList()
        {
            using var Context = new BarDatabase();
            
            return Context.Implementers.Select(x => x.GetViewModel).ToList();
        }

        public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel Model)
        {
            if (string.IsNullOrEmpty(Model.ImplementerFIO))
            {
                return new();
            }
            using var Context = new BarDatabase();
            return Context.Implementers.Where(x => x.ImplementerFIO.Contains(Model.ImplementerFIO)).Select(x => x.GetViewModel).ToList();
        }

        public ImplementerViewModel? GetElement(ImplementerSearchModel Model)
        {
            if (string.IsNullOrEmpty(Model.ImplementerFIO) && !Model.Id.HasValue)
            {
                return null;
            }
            
            using var Context = new BarDatabase();
            
            return Context.Implementers.FirstOrDefault(x =>
                (!string.IsNullOrEmpty(Model.ImplementerFIO) && x.ImplementerFIO == Model.ImplementerFIO && (!string.IsNullOrEmpty(Model.Password) ? x.Password == Model.Password : true)) ||
                (Model.Id.HasValue && x.Id == Model.Id))
                ?.GetViewModel;
        }

        public ImplementerViewModel? Insert(ImplementerBindingModel Model)
        {
            var NewImplementer = Implementer.Create(Model);
            if (NewImplementer == null)
            {
                return null;
            }
            using var Context = new BarDatabase();
            
            Context.Implementers.Add(NewImplementer);
            Context.SaveChanges();
            
            return NewImplementer.GetViewModel;
        }

        public ImplementerViewModel? Update(ImplementerBindingModel Model)
        {
            using var Context = new BarDatabase();
            
            var Implementer = Context.Implementers.FirstOrDefault(x => x.Id == Model.Id);
            if (Implementer == null)
            {
                return null;
            }
            
            Implementer.Update(Model);
            Context.SaveChanges();
            
            return Implementer.GetViewModel;
        }

        public ImplementerViewModel? Delete(ImplementerBindingModel Model)
        {
            using var Context = new BarDatabase();
            
            var Implementer = Context.Implementers.FirstOrDefault(rec => rec.Id == Model.Id);
            if (Implementer == null)
            {
                return null;
            }
            
            Context.Implementers.Remove(Implementer);
            Context.SaveChanges();
            
            return Implementer.GetViewModel;
        }
    }
}
