//
//  ThingsAddStartViewController.m
//  kneet2
//
//  Created by Jason Lee on 11/13/15.
//  Copyright © 2015 ntels. All rights reserved.
//

#import "JDObject.h"

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

#import "ThingsAddIncludeViewController.h"

#import "ThingsAddStartViewController.h"
#import "DeviceModel.h"

@interface ThingsAddStartViewController () {

}
@end


#pragma mark - Class Definition

@implementation ThingsAddStartViewController


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


- (void)initUI {
    if (_addableDevice) {
        [_btnDoStart setTitle:@"추가 시작" forState:UIControlStateNormal];
        _lblComment.text = @"장치 근처로 이동 후\n추가시작 버튼을 누르세요";
        
    } else if (_removableDevice) {
        [_btnDoStart setTitle:@"삭제 시작" forState:UIControlStateNormal];
        _lblComment.text = @"장치 근처로 이동 후\n삭제시작 버튼을 누르세요";
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [UIView animateWithDuration:kfAnimationDur animations:^{
        _maskView.alpha = 0.7;
    } completion:^(BOOL finished) {
        
        _constraintPopViewTop.constant = 64;
        
        [UIView animateWithDuration:kfAnimationDur animations:^{
            [self.view layoutIfNeeded];
        }];
    }];
}

- (void)prepareViewDidLoad {

    if (_addableDevice) {//inclusion
        _lblSensorName.text = _addableDevice[@"nodeName"];
        _lblManufacturer.text = _addableDevice[@"manufacturer"];
        
    } else if (_removableDevice) {//exclusion
        _lblSensorName.text = _removableDevice.deviceName;
        
        if (_removableGroups) {
            NSMutableString *groups = [[NSMutableString alloc] init];
            for (DeviceModel *device in _removableGroups) {
                NSString *prefix = [groups isEmptyString] ? ksEmptyString : @",";
                [groups appendFormat:@"%@%@", prefix, device.deviceName];
            }
            
            _lblManufacturer.text = [NSString stringWithFormat:@"이 장치를 삭제하면 %@도 함께 삭제됩니다", groups];
            _lblManufacturer.backgroundColor = kUITextColor02;
        }
    }
}


#pragma mark - Main Logic



#pragma mark - UI Events
- (void)btnAddTouched:(id)sender {
    
    if ([[JDFacade facade].loginUser.homehubOnlineState isEqualToString:@"OFF"]) {
        [[JDFacade facade] toast:@"홈허브가 온라인 상태로\r연결되어 있을 때 시도해주세요"];
        return;
    }
    
    ThingsAddIncludeViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddIncludeViewController" storyboardName:@"Things"];
    vc.addableDevice = _addableDevice;
    vc.removableDevice = _removableDevice;
    
    vc.providesPresentationContextTransitionStyle = YES;
    vc.definesPresentationContext = YES;
    
    [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
    
    [self presentViewController:vc animated:nil completion:nil];
}

- (void)btnCancelTouched:(id)sender {
    [UIView animateWithDuration:kfAnimationDur animations:^{
        _maskView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
}


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

@end