//
//  IMMessageItem+Encoded.m
//  kordophoned
//
//  Created by James Magahern on 11/16/18.
//  Copyright © 2018 James Magahern. All rights reserved.
//

#import "IMMessageItem+Encoded.h"
#import "MBIMHTTPUtilities.h"

static NSString* IMAssociatedMessageTypeValue(IMAssociatedMessageType type) {
    switch (type) {
        case IMAssociatedMessageTypeAcknowledgmentHeart:
            return @"heart";
        case IMAssociatedMessageTypeAcknowledgmentThumbsUp:
            return @"thumbsUp";
        case IMAssociatedMessageTypeAcknowledgmentThumbsDown:
            return @"thumbsDown";
        case IMAssociatedMessageTypeAcknowledgmentHa:
            return @"ha";
        case IMAssociatedMessageTypeAcknowledgmentExclamation:
            return @"exclamation";
        case IMAssociatedMessageTypeAcknowledgmentQuestionMark:
            return @"questionMark";
            
        case IMAssociatedMessageTypeAcknowledgmentHeartRemoved:
            return @"removed heart";
        case IMAssociatedMessageTypeAcknowledgmentThumbsUpRemoved:
            return @"removed thumbsUp";
        case IMAssociatedMessageTypeAcknowledgmentThumbsDownRemoved:
            return @"removed thumbsDown";
        case IMAssociatedMessageTypeAcknowledgmentHaRemoved:
            return @"removed ha";
        case IMAssociatedMessageTypeAcknowledgmentExclamationRemoved:
            return @"removed exclamation";
        case IMAssociatedMessageTypeAcknowledgmentQuestionMarkRemoved:
            return @"removed questionMark";
            
        case IMAssociatedMessageTypeUnspecified:
        default:
            return @"unknown";
    }
}

@implementation IMMessage (Encoded)

- (NSDictionary *)mbim_dictionaryRepresentation
{
    NSMutableDictionary *messageDict = [NSMutableDictionary dictionary];
    messageDict[@"text"] = [[self text] string];
    messageDict[@"date"] = MBIMWebServerFormatISO8601([self time]);
    messageDict[@"sender"] = ([self isFromMe] ? nil : [[self sender] displayID]); // TODO: nil sender is still a weird way to represent this...
    messageDict[@"guid"] = [self guid];
    
    if ([self associatedMessageGUID]) {
        NSString *encodedAssociatedMessageGUID = [self associatedMessageGUID];
        messageDict[@"associatedMessageGUID"] = encodedAssociatedMessageGUID;
        messageDict[@"associatedMessageType"] = IMAssociatedMessageTypeValue([self associatedMessageType]);
        
        NSString *decodedAssociatedChatItemGUID = IMAssociatedMessageDecodeGUID(encodedAssociatedMessageGUID);
        if (decodedAssociatedChatItemGUID) messageDict[@"associatedChatItemGUID"] = decodedAssociatedChatItemGUID;
    }
    
    if ([self fileTransferGUIDs]) {
        // Support only images right now
        NSMutableDictionary *attachmentMetadatas = [NSMutableDictionary dictionary];
        NSMutableArray *filteredFileTransferGUIDs = [NSMutableArray array];
        for (NSString *guid in self.fileTransferGUIDs) {
            NSMutableDictionary *metadata = [NSMutableDictionary dictionary];
            IMFileTransfer *transfer = [[IMFileTransferCenter sharedInstance] transferForGUID:guid];
            if ([[transfer mimeType] containsString:@"image"]) {
                [filteredFileTransferGUIDs addObject:guid];
                
                if ([transfer attributionInfo] != nil) {
                    metadata[@"attributionInfo"] = [transfer attributionInfo];
                }
            }
            
            if (metadata.count) {
                attachmentMetadatas[guid] = metadata;
            }
        }
        
        if ([filteredFileTransferGUIDs count]) {
            messageDict[@"fileTransferGUIDs"] = filteredFileTransferGUIDs;
            messageDict[@"attachmentMetadata"] = attachmentMetadatas;
        }
    }
    
    return messageDict;
}

@end

