/*
 CamerasListViewController.m
 
 View controller for cameras list interface
 */

#import "CamerasListViewController.h"

XPSDKViewID kAllCamerasFolderID = (XPSDKViewID)@"1926418B-893E-4AD6-A258-FB4A9AB57453";

@import XPMobileSDK;

@interface CamerasListViewController () <XPMobileSDKConnectionDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation CamerasListViewController
{
    NSMutableArray *camerasList;
}

#pragma mark: Life cycle methods

- (void)viewDidLoad {
    [super viewDidLoad];
    [XPMobileSDK addDelegate:self];
    camerasList = [[NSMutableArray alloc] init];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.title = @"Cameras List";
    self.navigationItem.backBarButtonItem.title = @"Back";
    [self loadCameras];
}

#pragma mark: Fetch and parse cameras list methods

- (void) loadCameras {
    [XPMobileSDK getAllViewsAndCamerasWithSuccessHandler:^(XPSDKResponse * response) {
        [self parseItem:(ItemType *)response.responseItems[0][@"Items"]];
        [self.tableView reloadData];
        
    } failureHandler:^(NSError * error) {
        NSLog(@"Error fetching all views and cameras");
    }];
}

/*
 Recursive function for parsing the response items
 */
- (void) parseItem:(ItemType *) item {
    NSArray *items;
    
    if([item[@"Item"] isKindOfClass:[NSDictionary class]]){
        items = @[item[@"Item"]];
    } else if([item[@"Item"] isKindOfClass:[NSArray class]]){
        items = item[@"Item"];
    }
    
    for (ItemType *item in items) {
        if ([item[@"Type"] isEqualToString: @"Camera"]) {
            //When an item of type 'Camera' is found, it is appended to the list of cameras to be displayed
            [camerasList addObject:item];
        } else if (([item[@"Type"] isEqualToString: @"Folder"] && [item[@"Id"] caseInsensitiveCompare:kAllCamerasFolderID] == NSOrderedSame) || [item[@"Type"] isEqualToString: @"View"]) {
            //When the item is of type 'Folder' with the View ID of 'All Cameras' or of type 'View',
            //Parse its child items
            [self parseItem:item[@"Items"]];
        }
        
    }
    
}

#pragma mark - TableViewDataSource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return camerasList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"CameraTableItem";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    
    ItemType* cameraItem = [camerasList objectAtIndex:indexPath.row];
    cell.textLabel.text = cameraItem[@"Name"];
    return cell;
}

#pragma mark - TableViewDelegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ItemType *cameraItem = camerasList[indexPath.row];
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    CameraLiveViewController *cameraLiveViewController = [sb instantiateViewControllerWithIdentifier:@"CameraLiveViewController"];
    cameraLiveViewController.cameraID = cameraItem[@"Id"];
    cameraLiveViewController.cameraName = cameraItem[@"Name"];
    [self.navigationController pushViewController:cameraLiveViewController animated:YES];
    
}

@end
