using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GarmentFactoryContracts.BindingModels;
using GarmentFactoryContracts.SearchModels;
using GarmentFactoryContracts.StoragesContracts;
using GarmentFactoryContracts.ViewModels;
using GarmentFactoryListImplement.Models;

namespace GarmentFactoryListImplement.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(AddIntelligence(order.GetViewModel));
            }
            return result;
        }

        public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
        {
            var result = new List<OrderViewModel>();
            if (model.Id.HasValue)
            {
                foreach (var order in _source.Orders)
                {
                    if (order.Id == model.Id)
                    {
                        result.Add(AddIntelligence(order.GetViewModel));
                    }
                }
            }
            else if(model.DateFrom.HasValue && model.DateTo.HasValue)
            {
                foreach (var order in _source.Orders)
                {
                    if (order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo)
                    { 
                        result.Add(AddIntelligence(order.GetViewModel));
                    }
                }
            }
            else if (model.ClientId.HasValue)
            {
                foreach (var order in _source.Orders)
                {
                    if (order.ClientId == model.ClientId)
                    {
                        result.Add(AddIntelligence(order.GetViewModel));
                    }
                }
            }
            else if (model.ImplementerId.HasValue)
            {
                foreach (var order in _source.Orders)
                {
                    if (order.ImplementerId == model.ImplementerId)
                    {
                        result.Add(AddIntelligence(order.GetViewModel));
                    }
                }
            }
            else if (model.Status != null)
            {
                foreach (var order in _source.Orders)
                {
                    if (order.Status.Equals(model.Status))
                    {
                        result.Add(AddIntelligence(order.GetViewModel));
                    }
                }
            }
            return result;
        }

        public OrderViewModel? GetElement(OrderSearchModel model)
        {
            if (!model.Id.HasValue && !model.ImplementerId.HasValue)
            {
                return null;
            }
            foreach (var order in _source.Orders)
            {
                if (order.Id == model.Id)
                {
                    return AddIntelligence(order.GetViewModel);
                }
                if (model.ImplementerId.HasValue && model.Status != null && order.ImplementerId == model.ImplementerId && order.Status.Equals(model.Status))
                {
                    return AddIntelligence(order.GetViewModel);
                }
                if (model.ImplementerId.HasValue && model.Status == null && order.ImplementerId == model.ImplementerId)
                {
                    return AddIntelligence(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 AddIntelligence(newOrder.GetViewModel);
        }

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

        private OrderViewModel AddIntelligence(OrderViewModel model)
        {
            var selectedTextile = _source.Textiles.Find(textile => textile.Id == model.TextileId);
            if (selectedTextile != null)
            {
                model.TextileName = selectedTextile.TextileName;
            }
            foreach (var client in _source.Clients)
            {
                if (client.Id == model.ClientId)
                {
                    model.ClientFIO = client.ClientFIO;
                    break;
                }
            }
            return model;
        }
    }
}
