using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StoragesContracts;
using PizzeriaContracts.ViewModels;
using PizzeriaListImplement.Models;

namespace PizzeriaListImplement.Implements
{
    public class OrderStorage : IOrderStorage
    {
        private readonly DataListSingleton _source;
        private IPizzaStorage _pizzaStorage;
        public OrderStorage(IPizzaStorage pizzaStorage)
        {
            _pizzaStorage = pizzaStorage;
            _source = DataListSingleton.GetInstance();
        }
        public List<OrderViewModel> GetFullList()
        {
            var result = new List<OrderViewModel>();

            foreach (var order in _source.Orders)
            {
                OrderViewModel viewModel = order.GetViewModel;
                viewModel.PizzaName = _pizzaStorage.GetElement(new PizzaSearchModel { Id = order.PizzaId }).PizzaName;
                result.Add(viewModel);
            }

            return result;
        }
        public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
        {
            var result = new List<OrderViewModel>();

            if (!model.Id.HasValue)
            {
                return result;
            }

            foreach (var order in _source.Orders)
            {
                if (model.Id.HasValue && order.Id == model.Id)
                {
                    OrderViewModel viewModel = order.GetViewModel;
                    viewModel.PizzaName = _pizzaStorage.GetElement(new PizzaSearchModel { Id = order.PizzaId }).PizzaName;
                    result.Add(viewModel);
                }
            }

            return result;
        }
        public OrderViewModel? GetElement(OrderSearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return null;
            }

            foreach (var order in _source.Orders)
            {
                if (model.Id.HasValue && order.Id == model.Id)
                {
                    OrderViewModel viewModel = order.GetViewModel;
                    viewModel.PizzaName = _pizzaStorage.GetElement(new PizzaSearchModel { Id = order.PizzaId }).PizzaName;
                    return viewModel;
                }
            }

            return null;
        }
        public OrderViewModel? Insert(OrderBindingModel model)
        {
            model.Id = 1;

            foreach (var order in _source.Orders)
            {
                if (model.Id <= order.Id)
                {
                    model.Id = order.Id + 1;
                }
            }

            var newOrder = Order.Create(model);

            if (newOrder == null)
            {
                return null;
            }

            _source.Orders.Add(newOrder);

            return newOrder.GetViewModel;
        }
        public OrderViewModel? Update(OrderBindingModel model)
        {
            foreach (var order in _source.Orders)
            {
                if (order.Id == model.Id)
                {
                    order.Update(model);
                    OrderViewModel viewModel = order.GetViewModel;
                    viewModel.PizzaName = _pizzaStorage.GetElement(new PizzaSearchModel { Id = order.PizzaId }).PizzaName;
                    return viewModel;
                }
            }

            return null;
        }
        public OrderViewModel? Delete(OrderBindingModel model)
        {
            for (int i = 0; i < _source.Orders.Count; ++i)
            {
                if (_source.Orders[i].Id == model.Id)
                {
                    var element = _source.Orders[i];
                    _source.Orders.RemoveAt(i);
                    OrderViewModel viewModel = element.GetViewModel;
                    viewModel.PizzaName = _pizzaStorage.GetElement(new PizzaSearchModel { Id = element.PizzaId }).PizzaName;
                    return viewModel;
                }
            }

            return null;
        }
    }
}
