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

#import <Foundation/Foundation.h>
#import "MSRViewController.h"
#import "WSSessionController.h"
#import "WSParser.h"
#import "WSEncoder.h"


WSEncoder *_wsMSR;
NSMutableData *_printDataMSR;

@implementation MSRViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitle:@"MSR"];
}


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

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_wifiDidDisConnect:) name:socketDidDisconnectNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_sessionDataReceived:) name:WSDataReceivedNotification object:nil];
    
    _sessionController = [[WSSessionController sharedController]init];
    _wsMSR = [[WSEncoder alloc]init];
    
}



-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:socketDidDisconnectNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:WSDataReceivedNotification object:nil];
    
    if(_sessionController != nil) _sessionController = nil;
    if (_wsMSR != nil) _wsMSR = nil;
    if (_printDataMSR != nil) _printDataMSR = nil;
    
}

- (void)_wifiDidDisConnect:(NSNotification *)notification{
    
    [self.navigationController popToRootViewControllerAnimated: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];
        }
    }
    
    Byte *pSrc = (Byte *)[rcvData bytes];
    NSInteger length = [rcvData length];
    for ( int i = 0; i<length; i++) {
        NSLog(@"%02x", *(pSrc + i));
    }
    
    WSParser *parser = [[WSParser alloc] init];
    WSDATATYPE wsType = [parser decode:rcvData];
    
    switch (wsType) {
        case WSDATA_MSR:
        {
            [self displayMSRInfo:parser];
            break;
        }
        case WSDATA_MSRFAIL:
            
            break;
            
        case WSDATA_SPECIAL:
            
            break;
            
        case WSDATA_UNKNOWN:
            break;
            
        default:
            break;
    }
}

-(IBAction)Btn_DoubleTrack:(id)sender{
    
    [self clearMSRInfo];
    _wsMSR = [[WSEncoder alloc]init];
    
    Byte test[] = {0x1b, 0x7e, 0x49};
    [_sessionController writeData:[NSData dataWithBytes:test length:sizeof(test)]];
    
    
}

-(IBAction)Btn_TripleTrack:(id)sender{
    
    
    [self clearMSRInfo];
    _wsMSR = [[WSEncoder alloc]init];
    [_sessionController writeData:[_wsMSR enterMSRTripleTrackMode]];
    
    
}

-(IBAction)Btn_SwipeCancel:(id)sender
{
    [self clearMSRInfo];
    _wsMSR = [[WSEncoder alloc]init];
    [_sessionController writeData:[_wsMSR exitMSRMode]];
    
}

-(IBAction)Btn_ClearInfo:(id)sender
{
    [self clearMSRInfo];
    
}


-(void)displayMSRInfo:(WSParser *)MSRparser
{
    
    if(MSRparser.track1 != nil)
        _Lb_Track1.text = [[NSString alloc]initWithData:MSRparser.track1 encoding:NSASCIIStringEncoding];
    
    if(MSRparser.track2 != nil)
        _Lb_Track2.text = [[NSString alloc]initWithData:MSRparser.track2 encoding:NSASCIIStringEncoding];
    
    if(MSRparser.track3 != nil)
        _Lb_Track3.text = [[NSString alloc]initWithData:MSRparser.track3 encoding:NSASCIIStringEncoding];
    
}

-(void)clearMSRInfo
{
    _Lb_Track1.text = @"";
    _Lb_Track2.text = @"";
    _Lb_Track3.text = @"";
}


@end
