/*
 *    	Copyright 2015-2016 GetSocial B.V.
 *
 *	Licensed under the Apache License, Version 2.0 (the "License");
 *	you may not use this file except in compliance with the License.
 *	You may obtain a copy of the License at
 *
 *    	http://www.apache.org/licenses/LICENSE-2.0
 *
 *	Unless required by applicable law or agreed to in writing, software
 *	distributed under the License is distributed on an "AS IS" BASIS,
 *	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *	See the License for the specific language governing permissions and
 *	limitations under the License.
 */

#import "GetSocialUser+Serialize.h"

@implementation GetSocialUser (Serialize)

-(NSString*) serialize {
    NSMutableDictionary* dictionary = [[NSMutableDictionary alloc] init];
    [dictionary setObject:[self guid] forKey:@"guid"];
    [dictionary setObject:[self displayName] forKey:@"display_name"];
    [dictionary setObject:[self avatarUrl] forKey:@"avatar"];
    
    NSMutableDictionary *identities = [[NSMutableDictionary alloc] init];
    [self.identities enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
       [identities setObject:@"" forKey:[NSString stringWithFormat:@"%@", obj]];
    }];
    [dictionary setObject:identities forKey:@"identities"];
    
    NSDictionary* dict = [NSDictionary dictionaryWithDictionary:dictionary];
    
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    
    if (jsonData) {
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    } else {
        return @"";
    }
}

@end
