//
//  WSTableViewController.m
//  SDK212
//
//  Created by woosimsystems on 2016. 7. 04..
//  Copyright © 2016년 woosimsystems. All rights reserved.
//

#import "WSTableViewController.h"
#import "WSSessionController.h"
#import "RootViewController.h"


NSArray *samples;

@implementation WSTableViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    samples = [NSArray arrayWithObjects:@"Standard Mode", @"Page Mode", @"Text", @"Barcode", @"MSR",@"Image + Sign Pad", @"Label",  @"Miscellaneous", nil];
    
}


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


-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    
    // add observers
    [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidDisconnect:) name:EAAccessoryDidDisconnectNotification object:nil];
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_sessionDataReceived:) name:WSDataReceivedNotification object:nil];
    
    _sessionController = [[WSSessionController sharedController] init];
    
}


-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    UIViewController *vc = self.navigationController.topViewController;
    
    if([RootViewController class] == [vc class])
    {
        [_sessionController closeSession];
    }
    [[EAAccessoryManager sharedAccessoryManager] unregisterForLocalNotifications];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:EAAccessoryDidDisconnectNotification object:nil];
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:WSDataReceivedNotification object:nil];
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:EAAccessoryDidConnectNotification object:nil];
    
    
    if(_sessionController != nil) _sessionController = nil;
    if(_accessory != nil)_accessory = nil;
    
}


- (void)_accessoryDidDisconnect:(NSNotification *)notification
{
    [_sessionController closeSession];
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    /*
    if ([[self navigationController] topViewController] == self)
    {
        NSLog(@"_accessoryDidDisconnect2");
        EAAccessory *disconnectedAccessory = [[notification userInfo] objectForKey:EAAccessoryKey];
        if ([disconnectedAccessory connectionID] == [_accessory connectionID])
        {
            NSLog(@"_accessoryDidDisconnect3");
            [[self navigationController] popViewControllerAnimated:YES];
            
        }
    }
    */
}



- (void)_sessionDataReceived:(NSNotification *)notification
{
    
    NSInteger bytesAvailable = 0;
    NSMutableData *rcvData = [[NSMutableData alloc] init];
    
    while ((bytesAvailable = [_sessionController getAvailableReadBytes]) > 0) {
        NSData *data = [_sessionController readData:bytesAvailable];
        if (data) {
            [rcvData appendData:data];
        }
    }
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [samples count];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] ;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    cell.textLabel.text = [samples objectAtIndex:indexPath.row];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSUInteger row = [indexPath row];
    
    switch (row) {
        case 0:
            [self performSegueWithIdentifier:@"standardModeSegue" sender:tableView];
            break;
        case 1:
            [self performSegueWithIdentifier:@"pageModeSegue" sender:tableView];
            break;
        case 2:
            [self performSegueWithIdentifier:@"textSegue" sender:tableView];
            break;
        case 3:
            [self performSegueWithIdentifier:@"barcodeSegue" sender:tableView];
            break;
        case 4:
            [self performSegueWithIdentifier:@"msrSegue" sender:tableView];
            break;
        case 5:
            [self performSegueWithIdentifier:@"imageSignPadSegue" sender:tableView];
            break;
        case 6:
            [self performSegueWithIdentifier:@"labelSegue" sender:tableView];
            break;
        case 7:
            [self performSegueWithIdentifier:@"miscellaneousSegue" sender:tableView];
            break;
  
        default:
            break;
            
    }
}

@end
