using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StorageContracts;
using PizzeriaContracts.ViewModels;
using PizzeriaListImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PizzeriaListImplement.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(AttachOthersNames(order.GetViewModel));
            }
            return result;
        }
        public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
        {
            var result = new List<OrderViewModel>();
            if ((!model.DateFrom.HasValue || !model.DateTo.HasValue) && !model.ClientId.HasValue && !model.Status.HasValue)
            {
                return result;
            }
            foreach (var order in _source.Orders)
            {
                if ((model.DateFrom.HasValue && model.DateTo.HasValue && order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo) ||
                     (model.ClientId.HasValue && order.ClientId == model.ClientId) ||
                    (model.Status.HasValue && order.Status == model.Status))
                {
                    result.Add(AttachOthersNames(order.GetViewModel));
                }
            }
            return result;

        }
        public OrderViewModel? GetElement(OrderSearchModel model)
        {
            if (!model.Id.HasValue && (!model.ImplementerId.HasValue || !model.Status.HasValue))
            {
                return null;
            }
            foreach (var order in _source.Orders)
            {
                if ((model.Id.HasValue && order.Id == model.Id) ||
                  (model.ImplementerId.HasValue && order.Id == model.ImplementerId && order.Status == model.Status))
                {
                    return AttachOthersNames(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 AttachOthersNames(newOrder.GetViewModel);
        }
        public OrderViewModel? Update(OrderBindingModel model)
        {
            foreach (var order in _source.Orders)
            {
                if (order.Id == model.Id)
                {
                    order.Update(model);
                    return AttachOthersNames(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 AttachOthersNames(element.GetViewModel);
                }
            }
            return null;
        }
        private OrderViewModel AttachOthersNames(OrderViewModel model)
        {
            foreach (var implementer in _source.Implementers)
            {
                if (model.ImplementerId == implementer.Id)
                {
                    model.ImplementerFIO = implementer.ImplementerFIO;
                    break;
                }
            }
            foreach (var client in _source.Clients)
            {
                if (model.ClientId == client.Id)
                {
                    model.ClientFIO = client.ClientFIO;
                    break;
                }
            }
            foreach (var pizza in _source.Pizzas)
            {
                if (pizza.Id == model.PizzaId)
                {
                    model.PizzaName = pizza.PizzaName;
                    return model;
                }
            }
            return model;
        }
    }
}
