using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushibarListImplement.Models;

namespace SushibarListImplement.Implements
{
    public class OrderStorage : IOrderStorage
    {
        private readonly DataListSingleton _source;

        public OrderStorage()
        {
            _source = DataListSingleton.GetInstance();
        }
        public OrderViewModel? Delete(OrderBindingModel model)
        {
            for (int i = 0; i < _source.Sushi.Count; i++)
            {
                if (_source.Orders[i].Id == model.Id)
                {
                    Order order = _source.Orders[i];
                    _source.Orders.RemoveAt(i);
                    return order.GetViewModel;
                }
            }
            return null;
        }

        public OrderViewModel? GetElement(OrderSearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return null;
            }
            foreach (Order order in _source.Orders)
            {
                if (order.Id == model.Id)
                {
                    return order.GetViewModel;
                }
            }
            return null;
        }

        public List<OrderViewModel> GetFilteredList(OrderSearchModel? model)
        {
            List<OrderViewModel> list = new();
            if (!model.Id.HasValue || model.Id == 0)
            {
                foreach (Order order in _source.Orders)
                {
                    if (order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo)
                    {
                        list.Add(order.GetViewModel);
                    }
                }
            }
            else
            {
                foreach (Order order in _source.Orders)
                {
                    if (order.Id == model.Id)
                    {
                        list.Add(order.GetViewModel);
                    }
                }
            }
            return list;
        }

        public List<OrderViewModel> GetFullList()
        {
            List<OrderViewModel> list = new();
            foreach (Order order in _source.Orders)
            {
                list.Add(order.GetViewModel);
            }
            return list;
        }

        public OrderViewModel? Insert(OrderBindingModel model)
        {
            model.Id = 1;
            foreach (Order order in _source.Orders)
            {
                if (model.Id <= order.Id)
                {
                    model.Id = order.Id;
                }
            }
            Order? newOrder = Order.Create(model);
            if (newOrder == null)
            {
                return null;
            }
            _source.Orders.Add(newOrder);
            return newOrder.GetViewModel;
        }

        public OrderViewModel? Update(OrderBindingModel model)
        {
            foreach (Order order in _source.Orders)
            {
                if (order.Id == model.Id)
                {
                    order.Update(model);
                    return order.GetViewModel;
                }
            }
            return null;
        }
    }
}
