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

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

@interface RootViewController ()
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _sessionController = [[WSSessionController sharedController] init];
    [self setTitle:@"WOOSIM"];
    
}



-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // Do any additional setup after loading the view, typically from a nib.
    [[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];

}

-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:socketDidConnectNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:socketDidDisconnectNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:WSDataReceivedNotification 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
    [self performSegueWithIdentifier:@"sampleSegue" sender:self];
}

- (void)_wifiDidDisConnect:(NSNotification *)notification{

    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Error"
                          message:@"Network connection is failed."
                          delegate:self
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];
    
    [alert show];
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == _hostIPField) {
        [textField resignFirstResponder];
    }
    if (textField == _portField) {
        [textField resignFirstResponder];
    }
    return YES;
}


-(IBAction)backgroundTouched:(id)sender
{
    [_hostIPField resignFirstResponder];
    [_portField resignFirstResponder];
}


-(IBAction)connectHost:(id)sender
{
    NSString *IPString = _hostIPField.text;
    NSString *portString = _portField.text;
    
    [[WSSessionController sharedController] setupControllerForNetwork:IPString withPort:portString];
    
    if ([[WSSessionController sharedController] openSession] == NO)
    {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Error"
                              message:@"Network connection is failed."
                              delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }


}

@end
