using SushiBarContracts.BindingModel;
using SushiBarContracts.SearchModel;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarListImplement;
using SushiBarListImplements.Models;

namespace SushiBarListImplements.Implements
{
    public class MessageInfoStorage : IMessageInfoStorage
    {
        private readonly DataListSingleton source;

        public MessageInfoStorage(DataListSingleton source)
        {
            this.source = source;
        }

        public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
        {
            if(string.IsNullOrEmpty(model.MessageId) || !model.ClientId.HasValue)
            {
                return null;
            }

            foreach (var message in source.Messages)
            {
                if (message.MessageId.Equals(model.MessageId))
                {
                    return message.GetViewModel;
                }
            }

            return null;
        }

        public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
        {
            var list = new List<MessageInfoViewModel>();
            foreach (var msg in source.Messages)
            {
                if (msg.ClientId.HasValue && model.ClientId == msg.ClientId)
                {
                    list.Add(msg.GetViewModel);
                }
            }
            return list;
        }

        public List<MessageInfoViewModel> GetFullList()
        {
            var list = new List<MessageInfoViewModel>();
            foreach (var msg in source.Messages)
            {
                list.Add(msg.GetViewModel);
            }
            return list;
        }

        public MessageInfoViewModel? Insert(MessageInfoBindingModel model)
        {
            var newMessage = MessageInfo.Create(model);
            if (newMessage == null)
            {
                return null;
            }
            source.Messages.Add(newMessage);
            return newMessage.GetViewModel;
        }
    }
}
