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

namespace ComputersShopDataBaseImplement.Implements
{
    public class ImplementerStorage : IImplementerStorage
    {
        public ImplementerViewModel? Delete(ImplementerBindingModel model)
        {
            using var context = new ComputersShopDataBase();
            var element = context.Implementers
                .Include(x => x.Orders)
                .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Implementers.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

        public ImplementerViewModel? GetElement(ImplementerSearchModel model)
        {
            using var context = new ComputersShopDataBase();
            if (model.Id.HasValue)
            {
                return context.Implementers
                .FirstOrDefault(x => (x.Id == model.Id))?.GetViewModel;
            }
            else if (!string.IsNullOrEmpty(model.ImplementerFIO) && !string.IsNullOrEmpty(model.Password))
            {
                return context.Implementers
                .FirstOrDefault(x => (x.ImplementerFIO == model.ImplementerFIO && x.Password == model.Password))?.GetViewModel;
            }
            else if (!string.IsNullOrEmpty(model.ImplementerFIO))
            {
                return context.Implementers
                .FirstOrDefault(x => (x.ImplementerFIO == model.ImplementerFIO))?.GetViewModel;
            }
            return new();
        }

        public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
        {
            if (model == null)
            {
                return new();
            }
            if (!string.IsNullOrEmpty(model.ImplementerFIO))
            {
                using var context = new ComputersShopDataBase();
                return context.Implementers
                    .Include(x => x.Orders)
                    .Where(x => x.ImplementerFIO.Contains(model.ImplementerFIO))
                    .Select(x => x.GetViewModel)
                    .ToList();
            }
            return new();
        }

        public List<ImplementerViewModel> GetFullList()
        {
            using var context = new ComputersShopDataBase();
            return context.Implementers
                    .Include(x => x.Orders)
                    .Select(x => x.GetViewModel)
                    .ToList();
        }

        public ImplementerViewModel? Insert(ImplementerBindingModel model)
        {
            var newImplementer = Implementer.Create(model);
            if (newImplementer == null)
            {
                return null;
            }
            using var context = new ComputersShopDataBase();
            context.Implementers.Add(newImplementer);
            context.SaveChanges();
            return newImplementer.GetViewModel;
        }

        public ImplementerViewModel? Update(ImplementerBindingModel model)
        {
            using var context = new ComputersShopDataBase();
            var client = context.Implementers.FirstOrDefault(x => x.Id == model.Id);
            if (client == null)
            {
                return null;
            }
            client.Update(model);
            context.SaveChanges();
            return client.GetViewModel;
        }
    }
}
