//
//  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];
    
    [[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];
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidConnect:) name:EAAccessoryDidConnectNotification object:nil];
    
    _sessionController = [[WSSessionController sharedController]init];
    _wsMSR = [[WSEncoder alloc]init];
    
}



-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [[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 (_wsMSR != nil) _wsMSR = nil;
    if (_printDataMSR != nil) _printDataMSR = nil;
    
}


/*
- (void)_accessoryDidConnect:(NSNotification *)notification{
    
    
}
*/

- (void)_accessoryDidDisconnect:(NSNotification *)notification{
    
    [_sessionController closeSession];
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

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

    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];
    [_sessionController writeData:[_wsMSR enterMSRDoubleTrackMode]];

    
}

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