//
//  HomeModeUpdateViewController.m
//  
//
//  Created by Jason Lee on 10/30/15.
//
//
#import "JDObject.h"

#import "CustomButton.h"
#import "CustomLabel.h"
#import "CustomImageView.h"

#import "RequestHandler.h"
#import "JDJSONModel.h"

#import "HomeModeUpdateViewController.h"
#import "ModeModel.h"
#import "DeviceSelectPopupView.h"
#import "HomeModeSettingsViewController.h"


@interface HomeModeUpdateViewController () {

}
@end


#pragma mark - Class Definition

@implementation HomeModeUpdateViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initUI];
    [self prepareViewDidLoad];
}


- (void)initUI {
    
    NSString *message = nil, *highlightMessage = nil;
    NSString *btnTitle = nil;
    if ([_mode.basicModeCode isEqualToString:HOME_MODE_HOME]) {
        btnTitle = @"귀가";
        highlightMessage = @"잘 돌아왔어요!";
        message = @"쾌적한 귀가모드를 \r실행할까요?";
        
    } else if ([_mode.basicModeCode isEqualToString:HOME_MODE_AWAY]) {
        btnTitle = @"외출";
        highlightMessage = @"안녕히 다녀오세요!";
        message = @"안전한 외출모드를 \r실행할까요?";
        
    } else if ([_mode.basicModeCode isEqualToString:HOME_MODE_MORNING]) {
        btnTitle = @"기상";
        highlightMessage = @"좋은 아침!";
        message = @"상쾌한 기상모드를\r실행할까요?";
        
    } else if ([_mode.basicModeCode isEqualToString:HOME_MODE_NIGHT]) {
        btnTitle = @"취침";
        highlightMessage = @"잘 자요!";
        message = @"편안한 취침모드를 \r실행할까요?";
        
    }
    
    [_btnExecute setTitle:btnTitle forState:UIControlStateNormal];
    _lblMessage.text = [NSString stringWithFormat:@"%@\n\n%@", highlightMessage, message];
    [_lblMessage setColor:kUITextColor02 text:highlightMessage];
    
    _btnSetting.hidden = [JDFacade facade].loginUser.level < 90;
    
    //TODO : set icon;
}

- (void)prepareViewDidLoad {
    
}

#pragma mark - Main Logic

- (void)requestChangeHomeMode {

    NSString *modeTitle = [NSString stringWithFormat:@"%@ 모드", _mode.modeName];
    _lblMessage.text = [NSString stringWithFormat:@"%@\r\r실행 중입니다.", modeTitle];
    [_lblMessage setColor:kUITextColor02 text:modeTitle];
    
    NSString *path = [NSString stringWithFormat:API_POST_DASHBOARD_MODE_CHANGE, _mode.modeId];
    
    [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:nil modelClass:[JDJSONModel class] completion:^(id responseObject) {
        
        if ([[JDFacade facade].currentViewController isEqual:self]) {
            [self setContentsForComplete];
        }
        
        [[JDFacade facade] toast:[NSString stringWithFormat:@"%@ 상태가 되었습니다", _mode.modeName]];
        
    } failure:^(id errorObject) {
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
    }];
}

- (void)setContentsForComplete {
    
    NSString *highlightMessage = nil;

    highlightMessage = [NSString stringWithFormat:@"%@ 상태가 되었습니다.", _mode.modeName];
    //TODO : chagne button image
}


#pragma mark - UI Events

- (IBAction)btnExecuteTouched:(id)sender {
    [self requestChangeHomeMode];
}

- (IBAction)btnSettingTouched:(id)sender {
    HomeModeSettingsViewController *vc = (HomeModeSettingsViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeModeSettingsViewController" storyboardName:@"Main"];
    vc.mode = _mode;
    
    [self presentViewController:vc animated:YES completion:nil];
}

- (IBAction)btnCloseTouched:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


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



@end