//
//  TweetObject.m
//  LifeLog
//
//  Created by nvtu on 8/11/17.
//  Copyright © 2017 PhongNV. All rights reserved.
//

#import "TweetObject.h"
#import "Utilities.h"

@implementation TweetObject

-(id) initWithData : (NSDictionary *) dict {
    if([dict objectForKey:@"id"] != nil) {
        self.tweetID = [dict[@"id"] intValue];
    }
    if([dict objectForKey:@"group_id"] != nil) {
        self.groupID = [dict[@"group_id"] intValue];
    }
    if([dict objectForKey:@"tweet_content"] != nil) {
        self.content = dict[@"tweet_content"];
    }
    if([dict objectForKey:@"mode"] != nil) {
        int modeInt = [dict[@"mode"] intValue];
        switch (modeInt) {
            case 2:
                self.mode = @"running";
                break;
            case 3:
                self.mode = @"bike";
                break;
            default:
                self.mode = @"walking";
                break;
        }
    }
    if([dict objectForKey:@"distance"] != nil) {
        self.distance = [dict[@"distance"] floatValue];
    }
    if([dict objectForKey:@"time"] != nil) {
        self.time = dict[@"time"];
    }
    if([dict objectForKey:@"user_id"] != nil) {
        self.userID = [dict[@"user_id"] intValue];
    }
    if([dict objectForKey:@"user_created"] != nil) {
        self.userID = [dict[@"user_created"] intValue];
    }
    if([dict objectForKey:@"username"] != nil) {
        self.userName = dict[@"username"];
    }
    if([dict objectForKey:@"full_name"] != nil) {
        self.userName = dict[@"full_name"];
    }
    if([dict objectForKey:@"profile_image"] != nil) {
        self.avatar = dict[@"profile_image"];
    }
    if([dict objectForKey:@"created_at"] != nil) {
        NSString *dateString = dict[@"created_at"];
        self.createDate = [Utilities dateFromString:dateString withFormat:@"yyyy-MM-dd hh:mm:ss"];
    }
    else if([dict objectForKey:@"date_created"] != nil && [dict objectForKey:@"time_created"] != nil) {
        NSString *dateString = dict[@"date_created"];
        [dateString stringByAppendingString:@" "];
        [dateString stringByAppendingString:dict[@"time_created"]];
        self.createDate = [Utilities dateFromString:dateString withFormat:@"yyyy-MM-dd hh:mm:ss"];
    }
    else {
        self.createDate = [NSDate date];
    }
    return self;
}

@end
