//
//  ViewController.m
//  LED Controller
//
//  Created by Michael Lux on 5/13/15.
//  Copyright (c) 2015 Michael Lux. All rights reserved.
//

#import "ViewController.h"

@interface ViewController (){
    
}
@property (weak, nonatomic) IBOutlet UISwitch *redLight;
@property (weak, nonatomic) IBOutlet UISwitch *yellowLight;
@property (weak, nonatomic) IBOutlet UISwitch *greenLight;

@end

@implementation ViewController
- (IBAction)redLight:(id)sender {
    if(self.redLight.on){
        NSData *returnedData = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"https://smarthomeapi.herokuapp.com/data/post?address=1&category=klight&value=True"]];
    }
    else{
        
        NSData *returnedData = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"https://smarthomeapi.herokuapp.com/data/post?address=1&category=klight&value=False"]];
    }
}
- (IBAction)yellowLight:(id)sender {
    if(self.yellowLight.on){
        NSData *returnedData = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"https://smarthomeapi.herokuapp.com/data/post?address=1&category=blight&value=True"]];
    }
    else{
        NSData *returnedData = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"https://smarthomeapi.herokuapp.com/data/post?address=1&category=blight&value=False"]];
    }
}
- (IBAction)greenLight:(id)sender {
    if(self.greenLight.on){
        NSData *returnedData = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"https://smarthomeapi.herokuapp.com/data/post?address=1&category=llight&value=True"]];
    }
    else{
        NSData *returnedData = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"https://smarthomeapi.herokuapp.com/data/post?address=1&category=llight&value=False"]];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSData *lightData = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"https://smarthomeapi.herokuapp.com/data/get?address=1&category=klight"]];
    NSDictionary *lightdictionary = [NSJSONSerialization JSONObjectWithData:lightData options:0 error:nil];
    NSString *redlight=[lightdictionary valueForKey:@"data"];
    
    if([redlight isEqualToString:@"True"]){
        [self.redLight setOn:YES animated:YES];
    }
    else{
        
        [self.redLight setOn:NO animated:YES];
    }
    
    lightData = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"https://smarthomeapi.herokuapp.com/data/get?address=1&category=blight"]];
    lightdictionary = [NSJSONSerialization JSONObjectWithData:lightData options:0 error:nil];
    NSString *yellowlight=[lightdictionary valueForKey:@"data"];
    
    if([yellowlight isEqualToString:@"True"]){
        [self.yellowLight setOn:YES animated:YES];
    }
    else{
        [self.yellowLight setOn:NO animated:YES];
    }
    
    lightData = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"https://smarthomeapi.herokuapp.com/data/get?address=1&category=llight"]];
    lightdictionary = [NSJSONSerialization JSONObjectWithData:lightData options:0 error:nil];
    NSString *greenlight=[lightdictionary valueForKey:@"data"];
    
    if([greenlight isEqualToString:@"True"]){
        [self.greenLight setOn:YES animated:YES];
    }
    else{
        
        [self.greenLight setOn:NO animated:YES];
    }
}

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

@end
