/*
 * This file is part of Bisq.
 *
 * Bisq is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bisq is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bisq. If not, see <http://www.gnu.org/licenses/>.
 */

package bisq.network.p2p.mailbox;

import bisq.network.p2p.DecryptedMessageWithPubKey;
import bisq.network.p2p.storage.payload.ProtectedMailboxStorageEntry;

import bisq.common.proto.ProtobufferException;
import bisq.common.proto.network.NetworkProtoResolver;
import bisq.common.proto.persistable.PersistablePayload;

import lombok.Value;

@Value
public class MailboxItem implements PersistablePayload {
    private final ProtectedMailboxStorageEntry protectedMailboxStorageEntry;
    private final DecryptedMessageWithPubKey decryptedMessageWithPubKey;

    public MailboxItem(ProtectedMailboxStorageEntry protectedMailboxStorageEntry,
                       DecryptedMessageWithPubKey decryptedMessageWithPubKey) {
        this.protectedMailboxStorageEntry = protectedMailboxStorageEntry;
        this.decryptedMessageWithPubKey = decryptedMessageWithPubKey;
    }

    @Override
    public protobuf.MailboxItem toProtoMessage() {
        return protobuf.MailboxItem.newBuilder()
                .setProtectedMailboxStorageEntry(protectedMailboxStorageEntry.toProtoMessage())
                .setDecryptedMessageWithPubKey(decryptedMessageWithPubKey.toProtoMessage())
                .build();
    }

    public static MailboxItem fromProto(protobuf.MailboxItem proto, NetworkProtoResolver networkProtoResolver)
            throws ProtobufferException {
        return new MailboxItem(ProtectedMailboxStorageEntry.fromProto(proto.getProtectedMailboxStorageEntry(),
                networkProtoResolver),
                DecryptedMessageWithPubKey.fromProto(proto.getDecryptedMessageWithPubKey(), networkProtoResolver));
    }
}

@@ -1004,20 +1004,174 @@
 ort 
-lombok.Value
+java.time.Clock;%0A%0Aimport java.util.Optional;%0A%0Aimport lombok.Value;%0A%0Aimport javax.annotation.Nullable;%0A%0Aimport static com.google.common.base.Preconditions.checkNotNull
 ;%0A%0A@
@@ -1306,24 +1306,38 @@
 orageEntry;%0A
+    @Nullable%0A
     private 
@@ -1495,32 +1495,42 @@
                 
+@Nullable 
 DecryptedMessage
@@ -1794,22 +1794,54 @@
         
-return
+protobuf.MailboxItem.Builder builder =
  protobu
@@ -1954,32 +1954,34 @@
 oProtoMessage())
+;%0A
 %0A               
@@ -1969,32 +1969,135 @@
 );%0A%0A        
-        
+Optional.ofNullable(decryptedMessageWithPubKey).ifPresent(decryptedMessageWithPubKey -%3E%0A                builder
 .setDecrypte
@@ -2159,16 +2159,42 @@
 ssage())
+);%0A%0A        return builder
 %0A       
@@ -2372,16 +2372,260 @@
 ption %7B%0A
+%0A        DecryptedMessageWithPubKey decryptedMessageWithPubKey = proto.hasDecryptedMessageWithPubKey() ?%0A                DecryptedMessageWithPubKey.fromProto(proto.getDecryptedMessageWithPubKey(), networkProtoResolver) :%0A                null;%0A%0A
         
@@ -2774,33 +2774,33 @@
                 
-D
+d
 ecryptedMessageW
@@ -2812,79 +2812,1020 @@
 bKey
-.fromProto(proto.getDecryptedMessageWithPubKey(), networkProtoResolver)
+);%0A    %7D%0A%0A    public boolean isMine() %7B%0A        return decryptedMessageWithPubKey != null;%0A    %7D%0A%0A    public String getUid() %7B%0A        if (decryptedMessageWithPubKey != null) %7B%0A            // We use uid from mailboxMessage in case its ours as we have the at removeMailboxMsg only the%0A            // decryptedMessageWithPubKey available which contains the mailboxMessage.%0A            MailboxMessage mailboxMessage = (MailboxMessage) checkNotNull(decryptedMessageWithPubKey).getNetworkEnvelope();%0A            return mailboxMessage.getUid();%0A        %7D else %7B%0A            // If its not our mailbox msg we take the uid from the prefixedSealedAndSignedMessage instead.%0A            // Those will never be removed via removeMailboxMsg but we clean up expired entries at startup.%0A            return protectedMailboxStorageEntry.getMailboxStoragePayload().getPrefixedSealedAndSignedMessage().getUid();%0A        %7D%0A    %7D%0A%0A    public boolean isExpired(Clock clock) %7B%0A        return protectedMailboxStorageEntry.isExpired(clock
 );%0A 

Add new methods, make decryptedMessageWithPubKey nullable