//
//  File+DictRepresentation.m
//  react-native-rainbow-module
//
//  Created by Doha Tubaileh on 29/06/2022.
//

#import "File+DictRepresentation.h"
#import "Contact+DictRepresentation.h"
#import "NSDate+ReactDateFormatter.h"
#import "NSDate+ReactDateFormatter.h"
#import "NSData+Base64String.h"
#import "Room+DictRepresentation.h"

@implementation File (DictRepresentation)

- (NSDictionary*) fileDictionaryRepresentation {
    NSLog(@"[File+DictRepresentation] %@",NSStringFromSelector(_cmd));
    NSMutableDictionary *fileDict = [[NSMutableDictionary alloc] init];
    [fileDict setValue:self.rainbowID forKey:@"id"];
    [fileDict setValue:self.fileName forKey:@"name"];
    [fileDict setValue:[self fileTypeToString] forKey:@"type"];
    [fileDict setValue:self.url forKey:@"url"];
    [fileDict setValue:[NSNumber numberWithUnsignedInteger:self.size] forKey:@"size"];
    [fileDict setValue:[self.owner contactDictionaryRepresentation] forKey:@"owner"];
    [fileDict setValue:self.dateToSort.changeDateFormatToReact forKey:@"date"];
    [fileDict setValue:self.thumbnailData.encodeToBase64String forKey:@"ImagePreview"];
    [fileDict setValue:[self getViewersAsDictionaryArray] forKey:@"viewers"];
    return fileDict;
}

- (NSArray<NSDictionary*>*) getViewersAsDictionaryArray {
    NSLog(@"[File+DictRepresentation] %@",NSStringFromSelector(_cmd));
    NSMutableArray *viewersArray = [[NSMutableArray alloc] init];
    for (Peer *item in self.viewers) {
        //Check if this peer is a contact peer
        if (item.class == Contact.class) {
            Contact *contactPeer = (Contact*)item;
            [viewersArray addObject: contactPeer.contactDictionaryRepresentation];
        }
        else if (item.class == Room.class){
            Room *roomPeer = (Room*)item;
            [viewersArray addObject: roomPeer.roomDictionaryRepresentation];
        }
    }
    
    return viewersArray;
}


- (NSString*) fileTypeToString {
    switch(self.type){
        case FileTypeImage:
            return @"FileTypeImage";
            break;
        case FileTypeDoc:
            return @"FileTypeDoc";
            break;
        case FileTypePDF:
            return @"FileTypePDF";
            break;
        case FileTypePPT:
            return @"FileTypePPT";
            break;
        case FileTypeXLS:
            return @"FileTypeXLS";
            break;
        case FileTypeAudio:
            return @"FileTypeAudio";
            break;
        case FileTypeVideo:
            return @"FileTypeVideo";
            break;
            
        default:
            return @"FileTypeOther";
    }
}


@end

