//
//  ViewController.m
//  SOTI sample
//
//  Created by SOTI Inc. on 2015-06-11.
//  Copyright (c) 2015 SOTI Inc. All rights reserved.
//

#import "ViewController.h"
#import "MobiControlConnectionManager.h"


@interface ViewController ()

@property (nonatomic, assign) MobiControlConnectionManager *mcManager;

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // create manager
    self.mcManager = [MobiControlConnectionManager defaultMobiControlConnectionManager];
    
    
    // set callbacks to visualization
    self.mcManager.callbackUpdateUI = ^ void (NSString *funcName) {
        [self updateUIFrom:funcName];
    };
    
    self.mcManager.callbackUpdateStatus = ^ void (NSString *str) {
        [self updateStatus:str];
    };

    
    // update UI
    [self updateUIFrom:[NSString stringWithUTF8String:__FUNCTION__]];
    [self updateStatus:NULL];
     
}



- (IBAction)btnTapped:(UIButton *)sender {
  
    if (self.mcManager.isConnected)
        [self.mcManager disconnectFromMobiControl];
    else
        [self.mcManager connectToMobiControl];
}


- (void)updateUIFrom:(NSString *)funcName {
    NSLog(@"updateUI from %@", funcName);
    
    self.labelIsConnected.text = self.mcManager.isConnected ? @"YES" : @"NO";
    self.labelIsEnrolled.text = self.mcManager.isEnrolled ? @"YES" : @"NO";
    self.labelIsAppConfigAvailable.text = self.mcManager.isAppConfigAvailable ? @"YES" : @"NO";


    if (self.mcManager.isConnected)
        [self.btnEnroll setTitle:@"Disconnect" forState:UIControlStateNormal];
    else
        [self.btnEnroll setTitle:@"Connect" forState:UIControlStateNormal];
}



- (void)updateStatus:(NSString *)str {    
    [self.labelStatus performSelector:@selector(setText:) withObject:str afterDelay:0.5];
}


@end
