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

#import "HistoryObject.h"
#import "Utilities.h"

@implementation HistoryObject

-(id) initWithData : (NSDictionary *) dict {
    if([dict objectForKey:@"steps"] != nil) {
        self.step = [dict[@"steps"] intValue];
    }
    if([dict objectForKey:@"step_diff"] != nil) {
        self.step_diff = [dict[@"step_diff"] intValue];
    }
    if([dict objectForKey:@"target"] != nil) {
        self.target = [dict[@"target"] intValue];
    }
    if([dict objectForKey:@"step_remain"] != nil) {
        self.missing = [dict[@"step_remain"] intValue];
    }
    if([dict objectForKey:@"complete_percent"] != nil) {
        self.percent = [dict[@"complete_percent"] floatValue];
    }
    if([dict objectForKey:@"distance"] != nil) {
        if([dict[@"distance"] isKindOfClass:[NSString class]]) {
            NSString *distance = dict[@"distance"];
            self.distance = [distance floatValue];
        }
        else {
            self.distance = [dict[@"distance"] floatValue];
        }
    }
    if([dict objectForKey:@"kcal"] != nil) {
        self.calories = [dict[@"kcal"] floatValue];
    }
    if([dict objectForKey:@"time"] != nil) {
        self.time = [dict[@"time"] intValue];
    }
    if([dict objectForKey:@"date"] != nil) {
        NSString *dateString = dict[@"date"];
        self.date  = [Utilities dateFromString:dateString withFormat:@"yyyy-MM-dd"];
    }
    else {
        self.date = [NSDate date];
    }
    if([dict objectForKey:@"data_chart"] != nil) {
        self.dataGraph = [[NSMutableArray alloc] init];
        NSDictionary * graph = [dict objectForKey:@"data_chart"];
        if([graph count] == 24) {
            for(int i = 0; i < 24; i++) {
                if(![graph isKindOfClass:[NSArray class]])
                    [self.dataGraph addObject:[graph objectForKey:[NSString stringWithFormat:@"%d", i]]];
            }
        }
    }
    return self;
}
@end
