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

#import "GroupObject.h"
#import "Utilities.h"

@implementation GroupObject

-(id) initWithShortData : (NSDictionary *) dict {
    if([dict objectForKey:@"id"] != nil) {
        self.groupID = [dict[@"id"] intValue];
    }
    if([dict objectForKey:@"group_name"] != nil) {
        self.name = dict[@"group_name"];
    }
    return self;
}

-(id) initWithData : (NSDictionary *) dict {
    if([dict objectForKey:@"group_id"] != nil) {
        self.groupID = [dict[@"group_id"] intValue];
    }
    if([dict objectForKey:@"group_avatar"] != nil) {
        self.avatar = dict[@"group_avatar"];
    }
    if([dict objectForKey:@"created_at"] != nil && ![[dict objectForKey:@"created_at"] isKindOfClass:[NSNull class]]) {
        NSString *dateString = dict[@"created_at"];
        self.createDate = [Utilities dateFromString:dateString withFormat:@"yyyy-MM-dd hh:mm:ss"];
    }
    else {
        self.createDate = [NSDate date];
    }
    if([dict objectForKey:@"num_search"] != nil) {
        self.numSearch = [dict[@"num_search"] intValue];
    }
    [self parseData:dict];
    return self;
}

-(id) initWithData : (NSDictionary *) dict andGroupID : (int) groupID {
    self.groupID = groupID;
    if([dict objectForKey:@"join_group"] != nil && ![[dict objectForKey:@"join_group"] isKindOfClass:[NSNull class]]) {
        self.isJoin = [dict[@"join_group"] boolValue];
    }
    if([dict objectForKey:@"goal"] != nil && ![[dict objectForKey:@"goal"] isKindOfClass:[NSNull class]]) {
        self.goal = dict[@"goal"];
    }
    else {
        self.goal = @"";
    }
    if([dict objectForKey:@"walk_mode_goal"] != nil && ![[dict objectForKey:@"walk_mode_goal"] isKindOfClass:[NSNull class]]) {
        self.walkGoal = [dict[@"walk_mode_goal"] intValue];
    }
    if([dict objectForKey:@"run_mode_goal"] != nil && ![[dict objectForKey:@"run_mode_goal"] isKindOfClass:[NSNull class]]) {
        self.runGoal = [dict[@"run_mode_goal"] intValue];
    }
    if([dict objectForKey:@"bike_mode_goal"] != nil && ![[dict objectForKey:@"bike_mode_goal"] isKindOfClass:[NSNull class]]) {
        self.bikeGoal = [dict[@"bike_mode_goal"] intValue];
    }
    if([dict objectForKey:@"step_mode_goal"] != nil && ![[dict objectForKey:@"step_mode_goal"] isKindOfClass:[NSNull class]]) {
        self.stepGoal = [dict[@"step_mode_goal"] intValue];
    }
    if([dict objectForKey:@"gym_mode_goal"] != nil && ![[dict objectForKey:@"gym_mode_goal"] isKindOfClass:[NSNull class]]) {
        self.gymGoal = [dict[@"gym_mode_goal"] intValue];
    }
    if([dict objectForKey:@"beginer_mode_goal"] != nil && ![[dict objectForKey:@"beginer_mode_goal"] isKindOfClass:[NSNull class]]) {
        self.beginGoal = [dict[@"beginer_mode_goal"] intValue];
    }
    [self parseData:dict];
    return self;
}

-(void) parseData : (NSDictionary *) dict {
    if([dict objectForKey:@"group_name"] != nil) {
        self.name = dict[@"group_name"];
    }
    if([dict objectForKey:@"walk_mode_active"] != nil && ![[dict objectForKey:@"walk_mode_active"] isKindOfClass:[NSNull class]]) {
        self.walkMode = [dict[@"walk_mode_active"] boolValue];
    }
    if([dict objectForKey:@"run_mode_active"] != nil && ![[dict objectForKey:@"run_mode_active"] isKindOfClass:[NSNull class]]) {
        self.runMode = [dict[@"run_mode_active"] boolValue];
    }
    if([dict objectForKey:@"bike_mode_active"] != nil && ![[dict objectForKey:@"bike_mode_active"] isKindOfClass:[NSNull class]]) {
        self.bikeMode = [dict[@"bike_mode_active"] boolValue];
    }
    if([dict objectForKey:@"step_mode_active"] != nil && ![[dict objectForKey:@"step_mode_active"] isKindOfClass:[NSNull class]]) {
        self.stepMode = [dict[@"step_mode_active"] boolValue];
    }
    if([dict objectForKey:@"gym_mode_active"] != nil && ![[dict objectForKey:@"gym_mode_active"] isKindOfClass:[NSNull class]]) {
        self.gymMode = [dict[@"gym_mode_active"] boolValue];
    }
    if([dict objectForKey:@"beginer_mode_active"] != nil && ![[dict objectForKey:@"beginer_mode_active"] isKindOfClass:[NSNull class]]) {
        self.beginMode = [dict[@"beginer_mode_active"] boolValue];
    }
}

-(void) updateDate : (GroupObject *) obj {
    self.name = obj.name;
    self.walkMode   = obj.walkMode;
    self.runMode    = obj.runMode;
    self.bikeMode   = obj.bikeMode;
    self.stepMode   = obj.stepMode;
    self.gymMode    = obj.gymMode;
    self.beginMode  = obj.beginMode;
    self.isJoin     = obj.isJoin;
    self.goal       = obj.goal;
    self.walkGoal   = obj.walkGoal;
    self.runGoal    = obj.runGoal;
    self.bikeGoal   = obj.bikeGoal;
    self.stepGoal   = obj.stepGoal;
    self.gymGoal    = obj.gymGoal;
    self.beginGoal  = obj.beginGoal;
}

@end
