using SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.SearchModels;
using SecuritySystemContracts.StoragesContracts;
using SecuritySystemContracts.ViewModels;
using SecuritySystemListImplement.Models;

namespace SecuritySystemListImplement.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(GetViewModel(order));
            }
            return result;
        }
        public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
        {
            var orders = _source.Orders
            .Select(x => x.GetViewModel)
            .ToList();
            if (model.Id.HasValue)
            {
                orders = orders.Where(x => x.Id == model.Id.Value).ToList();
            }
            if (model.Status.HasValue)
            {
                orders = orders.Where(x => x.Status == model.Status.Value).ToList();
            }
            if (model.ImplementerId.HasValue)
            {
                orders = orders.Where(x => x.ImplementerId == model.ImplementerId.Value).ToList();
            }
            if (model.DateFrom.HasValue)
            {
                orders = orders.Where(x => x.DateCreate >= model.DateFrom.Value).ToList();
            }
            if (model.DateTo.HasValue)
            {
                orders = orders.Where(x => x.DateCreate <= model.DateTo.Value).ToList();
            }
            if (model.ClientId.HasValue)
            {
                orders = orders.Where(x => x.ClientId == model.ClientId.Value).ToList();
            }
            return orders;
        }
        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 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 newOrder.GetViewModel;
        }
        public OrderViewModel? Update(OrderBindingModel model)
        {
            foreach (var order in _source.Orders)
            {
                if (order.Id == model.Id)
                {
                    order.Update(model);
                    return 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 element.GetViewModel;
                }
            }
            return null;
        }
        private OrderViewModel GetViewModel(Order order)
        {
            var viewModel = order.GetViewModel;
            foreach (var secure in _source.Secures)
            {
                if (secure.Id == order.SecureId)
                {
                    viewModel.SecureName = secure.SecureName;
                    break;
                }
            }
            return viewModel;
        }
    }
}
