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

#import "CreateGroupViewController.h"
#import "ServerAPI.h"

@interface CreateGroupViewController ()

@end

@implementation CreateGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.viewHeader attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark IBAction

- (IBAction)clickBack:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:true];
}

- (IBAction)clickCreateGroup:(AutoTransButton *)sender {
    NSString *name = self.textName.text;
    NSString *goal = self.textGoal.text;
    if(name.length == 0) {
        [Utilities showErrorMessage:@"Fill name of group!" withViewController:self];
        return;
    }
    else if(name.length >= 255) {
        [Utilities showErrorMessage:@"Name of group is too long!" withViewController:self];
        return;
    }
    else if(goal.length == 0) {
        [Utilities showErrorMessage:@"Fill information for goal of group" withViewController:self];
        return;
    }
    if(self.switchWalk.on && [self.textGoalWalk.text intValue] == 0) {
        [Utilities showErrorMessage:@"Add goal for walk mode" withViewController:self];
        return;
    }
    if(self.switchBike.on && [self.textGoalBike.text intValue] == 0) {
        [Utilities showErrorMessage:@"Add goal for bike mode" withViewController:self];
        return;
    }
    if(self.switchRun.on && [self.textGoalRun.text intValue] == 0) {
        [Utilities showErrorMessage:@"Add goal for run mode" withViewController:self];
        return;
    }
    GroupObject *object = [[GroupObject alloc] init];
    object.name = name;
    object.goal = goal;
    object.walkMode = self.switchWalk.on;
    object.runMode = self.switchRun.on;
    object.bikeMode = self.switchBike.on;
    object.stepMode = self.switchStep.on;
    object.gymMode = self.switchGym.on;
    object.beginMode = self.switchBegin.on;
    object.walkGoal = [self.textGoalWalk.text intValue];
    object.bikeGoal = [self.textGoalBike.text intValue];
    object.runGoal = [self.textGoalRun.text intValue];
    
    NSLog(@"%@", object.name);
    NSLog(@"%@", object.goal);
    NSLog(@"%d", object.walkGoal);
    NSLog(@"%d", object.bikeGoal);
    NSLog(@"%d", object.runGoal);
    NSLog(@"%d", object.walkMode);
    NSLog(@"%d", object.runMode);
    NSLog(@"%d", object.bikeMode);
    NSLog(@"%d", object.stepMode);
    NSLog(@"%d", object.gymMode);
    NSLog(@"%d", object.beginMode);
    
    NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
    MBProgressHUD *hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
    [[ServerAPI server] requestCreateGroup:token withGroup:object CompletionHandler:^(GroupObject * group, NSError * error) {
        CreateGroupViewController __weak *weakSelf = self;
        if(error == nil) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if(weakSelf.createGroupSuccess != nil) {
                    [hudView hideAnimated:true];
                    [weakSelf clickBack:nil];
                    weakSelf.createGroupSuccess(object);
                }
            });
        }
        else {
            dispatch_async(dispatch_get_main_queue(), ^{
                [hudView hideAnimated:true];
                NSString *message = [error.userInfo objectForKey:@"message"];
                [Utilities showErrorMessage:message withViewController:weakSelf];
            });
        }
    }];
}

#pragma mark UITextView Delegate

-(BOOL) textFieldShouldReturn:(UITextField *)textField {
    [self.view endEditing:true];
    return true;
}

@end
