using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConfectioneryFileImplement.Implements
{
    public class MessageInfoStorage
    {
        private readonly DataFileSingleton _source;
        public MessageInfoStorage()
        {
            _source = DataFileSingleton.GetInstance();
        }
        public List<MessageInfoViewModel> GetFullList()
        {
            return _source.MessagesInfo
                .Select(x => x.GetViewModel)
                .ToList();
        }
        public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
        {
            if (!model.ClientId.HasValue)
            {
                return new();
            }

            return _source.MessagesInfo
                .Where(x => x.ClientId == model.ClientId)
                .Select(x => x.GetViewModel)
                .ToList();
        }
        public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
        {
            if (string.IsNullOrEmpty(model.MessageId))
            {
                return null;
            }

            return _source.MessagesInfo
                .FirstOrDefault(x => x.MessageId == model.MessageId)
                ?.GetViewModel;
        }
        public MessageInfoViewModel? Insert(MessageInfoBindingModel model)
        {
            var newMessage = MessageInfo.Create(model);
            if (newMessage == null)
            {
                return null;
            }

            _source.MessagesInfo.Add(newMessage);
            _source.SaveMessagesInfo();
            return newMessage.GetViewModel;
        }
    }
}
