//
//  Invitation+DictRepresentation.m
//  rainbow
//
//  Created by Doha Tubaileh on 10/28/19.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import <Rainbow/Rainbow.h>
#import "Invitation+DictRepresentation.h"
#import "Contact+DictRepresentation.h"
#import "NSData+Base64String.h"
#import "NSString+PictureData.h"

@implementation Invitation (DictRepresentation)

-(NSDictionary*)invitationDictionaryRepresentation {
    NSLog(@"[Invitation+DictRepresentation]");
    NSMutableDictionary *invitationDic = [[NSMutableDictionary alloc] init];
    Contact *invitedContact;
    NSString *invitationType;
    if (self.direction == InvitationDirectionReceived) {
        //Me getting an invitation
        invitedContact = self.contact;
        invitationType = @"received";
    }
    else {
        invitedContact = self.contact;
        invitationType = @"sent";
    }
    if (invitedContact != nil) {
        NSMutableDictionary *contactDic = invitedContact.contactDictionaryRepresentation.mutableCopy;
        //check if you send invitation by email. without contact.
        //then make this contact name his mail
        if (contactDic[@"contactName"] == nil) {
            NSString *email = [invitedContact.emailAddresses.firstObject address];
            contactDic[@"name"] = email;
            contactDic[@"imageURL"] = [email getImageData].encodeToBase64String;
        }
        NSString *invitationEmail = self.email != nil ? self.email : @"";

        [invitationDic setValue:contactDic forKey:@"contact"];
        [invitationDic setValue:self.invitationID forKey:@"invitationId"];
        [invitationDic setValue:invitationType forKey:@"invitationType"];
        [invitationDic setValue: invitationEmail forKey:@"invitedUserEmail"];
    }
    return invitationDic;
}

@end
