using System;
using System.Collections.Generic;
using CommunityToolkit.Mvvm.Messaging;
using Wino.Core.Domain.Entities.Mail;
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Requests;
using Wino.Messaging.UI;

namespace Wino.Core.Requests.Folder;

public record MarkFolderAsReadRequest(MailItemFolder Folder, List<MailCopy> MailsToMarkRead) : FolderRequestBase(Folder, FolderSynchronizerOperation.MarkFolderRead), ICustomFolderSynchronizationRequest
{
    public List<Guid> SynchronizationFolderIds => [Folder.Id];

    public bool ExcludeMustHaveFolders => true;

    public override void ApplyUIChanges()
    {
        if (MailsToMarkRead == null || MailsToMarkRead.Count == 0) return;

        foreach (var item in MailsToMarkRead)
        {
            // Skip if already read
            if (item.IsRead) continue;

            item.IsRead = true;

            WeakReferenceMessenger.Default.Send(new MailUpdatedMessage(item, EntityUpdateSource.ClientUpdated, MailCopyChangeFlags.IsRead));
        }
    }

    public override void RevertUIChanges()
    {
        if (MailsToMarkRead == null || MailsToMarkRead.Count == 0) return;

        foreach (var item in MailsToMarkRead)
        {
            // Skip if already unread (wasn't changed by ApplyUIChanges)
            if (!item.IsRead) continue;

            item.IsRead = false;

            WeakReferenceMessenger.Default.Send(new MailUpdatedMessage(item, EntityUpdateSource.ClientReverted, MailCopyChangeFlags.IsRead));
        }
    }
}
