using ShipyardContracts.BindingModels;
using ShipyardContracts.SearchModels;
using ShipyardContracts.StoragesContracts;
using ShipyardContracts.ViewModels;
using ShipyardDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShipyardDataBaseImplement.Implements
{
    public class DetailStorage:IDetailStorage
    {
        public List<DetailViewModel> GetFullList()
        {
            using var context = new ShipyardDataBase();
            return context.Details
            .Select(x => x.GetViewModel)
           .ToList();
        }
        public List<DetailViewModel> GetFilteredList(DetailSearchModel model)
        {
            if (string.IsNullOrEmpty(model.DetailName))
            {
                return new();
            }
            using var context = new ShipyardDataBase();
            return context.Details
            .Where(x => x.DetailName.Contains(model.DetailName))
           .Select(x => x.GetViewModel)
           .ToList();
        }
        public DetailViewModel? GetElement(DetailSearchModel model)
        {
            if (string.IsNullOrEmpty(model.DetailName) && !model.Id.HasValue)
            {
                return null;
            }
            using var context = new ShipyardDataBase();
            return context.Details
            .FirstOrDefault(x =>
           (!string.IsNullOrEmpty(model.DetailName) && x.DetailName ==
           model.DetailName) ||
            (model.Id.HasValue && x.Id == model.Id))
            ?.GetViewModel;
        }
        public DetailViewModel? Insert(DetailBindingModel model)
        {
            var newDetail = Detail.Create(model);
            if (newDetail == null)
            {
                return null;
            }
            using var context = new ShipyardDataBase();
            context.Details.Add(newDetail);
            context.SaveChanges();
            return newDetail.GetViewModel;
        }
        public DetailViewModel? Update(DetailBindingModel model)
        {
            using var context = new ShipyardDataBase();
            var detail = context.Details.FirstOrDefault(x => x.Id ==
           model.Id);
            if (detail == null)
            {
                return null;
            }
            detail.Update(model);
            context.SaveChanges();
            return detail.GetViewModel;
        }
        public DetailViewModel? Delete(DetailBindingModel model)
        {
            using var context = new ShipyardDataBase();
            var element = context.Details.FirstOrDefault(rec => rec.Id ==
           model.Id);
            if (element != null)
            {
                context.Details.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

    }
}
