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

namespace ShipyardDataBaseImplement.Implements
{
    public class ShipStorage:IShipStorage
    {
        public List<ShipViewModel> GetFullList()
        {
            using var context = new ShipyardDataBase();
            return context.Ships.Include(x => x.Details)
            .ThenInclude(x => x.Detail)
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public List<ShipViewModel> GetFilteredList(ShipSearchModel model)
        {
            if (string.IsNullOrEmpty(model.ShipName))
            {
                return new();
            }
            using var context = new ShipyardDataBase();
            return context.Ships
             .Include(x => x.Details)
            .ThenInclude(x => x.Detail)
            .Where(x => x.ShipName.Contains(model.ShipName))
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public ShipViewModel? GetElement(ShipSearchModel model)
        {
            if (string.IsNullOrEmpty(model.ShipName) &&
            !model.Id.HasValue)
            {
                return null;
            }
            using var context = new ShipyardDataBase();
            return context.Ships
             .Include(x => x.Details)
             .ThenInclude(x => x.Detail)
             .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShipName) &&
            x.ShipName == model.ShipName) ||
            (model.Id.HasValue && x.Id ==
            model.Id))
            ?.GetViewModel;
        }
        public ShipViewModel? Insert(ShipBindingModel model)
        {
            using var context = new ShipyardDataBase();
            var newProduct = Ship.Create(context, model);
            if (newProduct == null)
            {
                return null;
            }
            context.Ships.Add(newProduct);
            context.SaveChanges();
            return newProduct.GetViewModel;
        }
        public ShipViewModel? Update(ShipBindingModel model)
        {
            using var context = new ShipyardDataBase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var product = context.Ships.FirstOrDefault(rec =>
                rec.Id == model.Id);
                if (product == null)
                {
                    return null;
                }
                product.Update(model);
                context.SaveChanges();
                product.UpdateComponents(context, model);
                transaction.Commit();
                return product.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
        public ShipViewModel? Delete(ShipBindingModel model)
        {
            using var context = new ShipyardDataBase();
            var element = context.Ships
            .Include(x => x.Details)
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Ships.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

    }
}
