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

namespace SecuritySystemListImplement.Implements
{
    public class MessageInfoStorage : IMessageInfoStorage
    {
        private readonly DataListSingleton _source;
        public MessageInfoStorage(DataListSingleton source)
        {
            _source = source;
        }

        public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
        {
            return GetFilteredList(model).FirstOrDefault();
        }

        public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
        {
            var messageInfos = GetFullList();
            if (!string.IsNullOrEmpty(model.MessageId))
            {
                messageInfos = messageInfos.Where(x => x.MessageId == model.MessageId).ToList();
            }
            if (model.ClientId != null)
            {
                messageInfos = messageInfos.Where(x => x.ClientId == model.ClientId).ToList();
            }
            return messageInfos;
        }

        public List<MessageInfoViewModel> GetFullList()
        {
            return _source.MessageInfos.Select(x => x.GetViewModel).ToList();
        }

        public MessageInfoViewModel? Insert(MessageInfoBindingModel model)
        {
            var messageInfo = MessageInfo.Create(model);
            if (messageInfo == null)
            {
                return null;
            }
            _source.MessageInfos.Add(messageInfo);
            return messageInfo.GetViewModel;
        }
    }
}
