//
//  WSTableViewController.m
//  WifiPrint
//
//  Created by woosimsystems on 2015. 5. 19..
//  Copyright (c) 2015년 woosimsystems. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "WSTableViewController.h"
#import "WSSessionController.h"
#import "RootViewController.h"

@implementation WSTableViewController

NSArray *samples;

- (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 + Signature Pad", @"Label", @"Miscellaneous", nil];
    [self setTitle:@"Samples"];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // add observers
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_sessionDataReceived:) name:WSDataReceivedNotification object:nil];
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_wifiDidConnect:) name:socketDidConnectNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_wifiDidDisConnect:) name:socketDidDisconnectNotification 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];
    }
    
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:WSDataReceivedNotification object:nil];
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:socketDidConnectNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:socketDidDisconnectNotification object:nil];
    
    if(_sessionController != nil) _sessionController = nil;
   
}

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

- (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];
        }
    }
}

/*
- (void)_wifiDidConnect:(NSNotification *)notification{

    // Pushes a view controller onto the receiver’s stack and updates the display
    
}
*/
- (void)_wifiDidDisConnect:(NSNotification *)notification{

    [self.navigationController popToRootViewControllerAnimated:YES];
    if(_sessionController != nil) _sessionController = nil;
    
}



- (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