using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DressAtelierContracts.BindingModels;
using DressAtelierContracts.SearchModels;
using DressAtelierContracts.StorageContracts;
using DressAtelierContracts.ViewModels;
using DressAtelierListImplement.Models;

namespace DressAtelierListImplement.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(ReceiveDressName(order.GetViewModel));
            }
            return result;
        }
        public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
        {
            var result = new List<OrderViewModel>();
            if (!model.ID.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientID.HasValue && !model.Status.HasValue)
            {
                return result;
            }

            if (model.ClientID.HasValue)
            {
                foreach (var order in _source.Orders)
                {
                    if (order.ClientID == model.ClientID)
                    {
                        result.Add(ReceiveDressName(order.GetViewModel));
                    }
                }
                return result;
            }

            if (model.ID.HasValue)
            {
                foreach (var order in _source.Orders)
                {
                    if (order.ID == model.ID)
                    {
                        result.Add(ReceiveDressName(order.GetViewModel));
                    }
                }
                return result;
            }

            if (model.DateFrom.HasValue && model.DateTo.HasValue)
            {
                foreach (var order in _source.Orders)
                {
                    if (order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo)
                    {
                        result.Add(ReceiveDressName(order.GetViewModel));
                    }
                }
                return result;
            }

            foreach (var order in _source.Orders)
            {
                if (order.Status == model.Status)
                {
                    result.Add(ReceiveDressName(order.GetViewModel));
                }
            }
            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)
                {
                    return ReceiveDressName(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 newComponent = Order.Create(model);
            if (newComponent == null)
            {
                return null;
            }
            _source.Orders.Add(newComponent);
            return ReceiveDressName(newComponent.GetViewModel);
        }

        public OrderViewModel? Update(OrderBindingModel model)
        {
            foreach (var order in _source.Orders)
            {
                if (order.ID == model.ID)
                {
                    order.Update(model);
                    return ReceiveDressName(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 ReceiveDressName(element.GetViewModel);
                }
            }
            return null;
        }

        private OrderViewModel? ReceiveDressName(OrderViewModel model)
        {
            var dress = _source.Dresses.Find(un => un.ID == model.ID);
            model.DressName = dress == null ? "" : dress.DressName;
            return model;
        }
    }
}
