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

#import "EMCompoundExampleViewController.h"
#import "EMFramework.h"
#import "EMCompoundResourceCell.h"

@interface EMCompoundExampleViewController () <UITableViewDataSource, UITableViewDelegate, EMCompoundResourceCellDelegate>

@property (nonatomic, strong) NSArray *readValue;

@end

@implementation EMCompoundExampleViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[EMConnectionManager sharedManager] readResource:@"infoArray" onSuccess:^(id readValue) {
        if ([readValue isKindOfClass:[NSArray class]]) {
            [self setReadValue:readValue];
            [[self tableView] reloadData];
        }
    } onFail:^(NSError *error) {
        NSLog(@"%@", [error localizedDescription]);
    }];
    
    [[EMConnectionManager sharedManager] addObserver:self forKeyPath:@"connectionState" options:0 context:NULL];
}

-(void)dealloc {
    [[EMConnectionManager sharedManager] removeObserver:self forKeyPath:@"connectionState"];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (object == [EMConnectionManager sharedManager]) {
        if ([keyPath isEqualToString:@"connectionState"]) {
            if ([[EMConnectionManager sharedManager] connectionState] == EMConnectionStateDisrupted) {
                [[self navigationController] popToRootViewControllerAnimated:YES];
            }
        }
    }
}

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

#pragma mark - Table Methods

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuseIdentifier = @"ResourceCell";
    NSDictionary *value = [[self readValue] objectAtIndex:[indexPath section]];
    EMCompoundResourceCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    [cell setCompoundResourceValue:value];
    [cell setDelegate:self];
    [cell setIndex:[indexPath section]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - Cell Delegate

-(void)cellDidProcessEditing:(NSDictionary *)dictionary forIndex:(NSUInteger)index {
    NSMutableArray *mutable = [[self readValue] mutableCopy];
    [mutable replaceObjectAtIndex:index withObject:dictionary];
    [self setReadValue:mutable];
    [[EMConnectionManager sharedManager] writeValue:[self readValue] toResource:@"infoArray" onSuccess:^{
        NSLog(@"Successfully wrote resource");
    } onFail:^(NSError *error) {
        NSLog(@"Failed to write value: %@", [error localizedDescription]);
    }];
}

@end
