using DinerContracts.BindingModels;
using DinerContracts.SearchModels;
using DinerContracts.StoragesContracts;
using DinerContracts.ViewModels;
using DinerListImplement.Models;
using DinerListImplement;

namespace DinerListImplement.Implements
{
    public class OrderStorage : IOrderStorage
    {
        private readonly DataListSingleton _source;
        public OrderStorage()
        {
            _source = DataListSingleton.GetInstance();
        }
        public List<OrderViewModel> GetFullList()
        {
            var result = new List<OrderViewModel>();
            foreach (var order in _source.Orders)
            {
                result.Add(AttachDinerName(order.GetViewModel));
            }
            return result;
        }
        public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
        {
            var result = new List<OrderViewModel>();
            if (model == null || !model.Id.HasValue)
            {
                return result;
            }
            if (!model.Id.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue)
            {
                foreach (var order in _source.Orders)
                {
                    if (order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo)
                    {
                        result.Add(order.GetViewModel);
                    }
                }
                return result;
            }
            else if (model.Id.HasValue)
                foreach (var order in _source.Orders)
                {
                    if (order.Id == model.Id)
                    {
                        if (order.Id == model.Id)
                        {
                            return new() { AttachSnackName(order.GetViewModel) };
                        }
                    }
                }

            else if (model.ClientId.HasValue)
            {
             
                foreach (var order in _source.Orders)
                {
                    if (order.ClientId == model.ClientId)
                    {
                        result.Add(AttachSnackName(order.GetViewModel));
                    }
                }

                return result;

            }
            else if (model.ImplementerId.HasValue)
            {
                foreach (var order in _source.Orders)
                {
                    if (order.ImplementerId == model.ImplementerId)
                    {
                        result.Add(AttachSnackName(order.GetViewModel));
                    }
                }

                return result;
            }
            return new();
        }


        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)
                {
                    return AttachDinerName(order.GetViewModel);
                }
            }
            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 AttachDinerName(newOrder.GetViewModel);
        }
        public OrderViewModel? Update(OrderBindingModel model)
        {
            foreach (var order in _source.Orders)
            {
                if (order.Id == model.Id)
                {
                    order.Update(model);
                    return AttachDinerName(order.GetViewModel);
                }
            }
            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);
                    return AttachDinerName(element.GetViewModel);
                }
            }
            return null;
        }
        private OrderViewModel AttachDinerName(OrderViewModel model)
        {
            foreach (var snack in _source.Products)
            {
                if (snack.Id == model.SnackId)
                {
                    model.SnackName = snack.SnackName;
                    return model;
                }
            }
            return model;
        }

        private OrderViewModel? AttachSnackName(OrderViewModel? model)
        {
            if (model == null)
            {
                return null;
            }
            var snack = _source.Products.FirstOrDefault(x => x.Id == model.SnackId);
            var client = _source.Clients.FirstOrDefault(x => x.Id == model.ClientId);
            var implementer = _source.Implementers.FirstOrDefault(x => x.Id == model.ImplementerId);
            if (snack != null)
            {
                model.SnackName = snack.SnackName;
            }
            if (client != null)
            {
                model.ClientFIO = client.ClientFIO;
            }
            if (implementer != null)
            {
                model.ImplementerFIO = implementer.ImplementerFIO;
            }
            return model;
        }
    }
}
