//
//  EMIndicatorExampleViewController.m
//  Indicator Example
//
//  Created by Dexter Weiss on 10/15/13.
//  Copyright (c) 2013 Emmoco. All rights reserved.
//

#import "EMIndicatorExampleViewController.h"
#import "EMFramework.h"

@interface EMIndicatorExampleViewController () {
    IBOutlet UISegmentedControl *_segmentedControl;
    IBOutlet UILabel *_indicatorLabel;
    NSTimer *_fadeTimer;
}

@end

@implementation EMIndicatorExampleViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:[[[EMConnectionManager sharedManager] connectedDevice] name]];
    [[EMConnectionManager sharedManager] readResource:@"eventSelector" onSuccess:^(id readValue) {
        [self _updateSegmentedControlSelectedIndex:readValue];
    } onFail:^(NSError *error) {
        NSLog(@"%@", [error localizedDescription]);
    }];
    
    [_indicatorLabel setAlpha:0.0f];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotification:) name:kEMConnectionDidReceiveIndicatorNotificationName object:nil];
}

-(void)didReceiveNotification:(NSNotification *)notification {
    id notificationValue = [[notification userInfo] objectForKey:kEMIndicatorResourceValueKey];
    NSString *resourceName = [[notification userInfo] objectForKey:kEMIndicatorNameKey];
    NSString *indicatorString = [NSString stringWithFormat:@"%@ indicator received\nValue: %@", resourceName, [notificationValue stringValue]];
    [_indicatorLabel setAlpha:0.0f];
    [_indicatorLabel setText:indicatorString];
    [UIView animateWithDuration:0.2 animations:^{
        [_indicatorLabel setAlpha:1.0f];
    } completion:^(BOOL finished) {
        [self _startLabelTimer];
    }];
}

-(void)_startLabelTimer {
    [_fadeTimer invalidate];
    _fadeTimer = [NSTimer timerWithTimeInterval:3.0f target:self selector:@selector(fadeLabel) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:_fadeTimer forMode:NSDefaultRunLoopMode];
}

-(void)fadeLabel {
    [_fadeTimer invalidate];
    [UIView animateWithDuration:0.3 animations:^{
        [_indicatorLabel setAlpha:0.0f];
    }];
}

-(void)_updateSegmentedControlSelectedIndex:(NSString *)eventValue {
    if ([eventValue isEqualToString:@"NONE"]) {
        [_segmentedControl setSelectedSegmentIndex:0];
    }
    else if ([eventValue isEqualToString:@"VOID_EVENT"]) {
        [_segmentedControl setSelectedSegmentIndex:1];
    }
    else if ([eventValue isEqualToString:@"UINT8_EVENT"]) {
        [_segmentedControl setSelectedSegmentIndex:2];
    }
    else if ([eventValue isEqualToString:@"INT32_EVENT"]) {
        [_segmentedControl setSelectedSegmentIndex:3];
    }
}

#pragma mark - Interface actions

-(IBAction)segmentedControlDidChange:(UISegmentedControl *)sender {
    NSString *indicatorType = nil;
    switch ([sender selectedSegmentIndex]) {
        case 0:
            indicatorType = @"NONE";
            break;
        case 1:
            indicatorType = @"VOID_EVENT";
            break;
        case 2:
            indicatorType = @"UINT8_EVENT";
            break;
        case 3:
            indicatorType = @"INT32_EVENT";
            break;
    }
    
    [[EMConnectionManager sharedManager] writeValue:indicatorType toResource:@"eventSelector" onSuccess:^{
        NSLog(@"Successfully wrote event type");
    } onFail:^(NSError *error) {
        NSLog(@"%@", [error localizedDescription]);
    }];
}

@end
