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

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


WSEncoder *_wsPI;

@implementation WSMiscellaneousViewController


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


- (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];
    _wsPI = [[WSEncoder alloc]init];
    
}



-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [[EAAccessoryManager sharedAccessoryManager] unregisterForLocalNotifications];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:EAAccessoryDidConnectNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:WSDataReceivedNotification object:nil];
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:EAAccessoryDidDisconnectNotification object:nil];
    
    
    if(_sessionController != nil) _sessionController = nil;
    if (_wsPI != nil) _wsPI = nil;
    
    
}

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

- (void)_accessoryDidDisconnect:(NSNotification *)notification{
    
    [_sessionController closeSession];
    [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];
        }
    }
    
    
    WSParser *parser = [[WSParser alloc] init];
    WSDATATYPE wsType = [parser decode:rcvData];
    
    switch (wsType) {

        case WSDATA_SPECIAL:
            if(parser.specialDataType == PRINTER_VER)
                _Lb_PrinterFW.text = parser.printerVersion;
            else if(parser.specialDataType == PRINTER_NAME)
                _Lb_PrinterName.text = parser.printerName;
            else if (parser.specialDataType == PRINTER_BATTERY)
            {
                if (parser.batteryStatus == 1)
                    _Lb_PrinterBatteryCharge.text = @"0 ~ 25%";
                else if (parser.batteryStatus == 2)
                    _Lb_PrinterBatteryCharge.text = @"25 ~ 50%";
                else if (parser.batteryStatus == 3)
                    _Lb_PrinterBatteryCharge.text = @"50 ~ 75%";
                else
                    _Lb_PrinterBatteryCharge.text = @"75 ~ 100%";
            }
            break;
            
        case WSDATA_UNKNOWN:
            _Lb_PrinterFW.text = @"unknown data";
            break;
            
        default:
            break;
    }
}



-(IBAction)Btn_CheckFirmwareVersion:(id)sender
{
    _Lb_PrinterFW.text = @"";
    _wsPI = [[WSEncoder alloc]init];
    [_sessionController writeData:[_wsPI verifyPrinterVer]];
}



-(IBAction)Btn_CheckPrinterName:(id)sender
{
    _Lb_PrinterName.text = @"";
    _wsPI = [[WSEncoder alloc]init];
    [_sessionController writeData:[_wsPI requestPrinterName]];
}

-(IBAction)Btn_CheckPrinterBatteryCharge:(id)sender
{
    _Lb_PrinterBatteryCharge.text = @"";
    _wsPI = [[WSEncoder alloc]init];
    [_sessionController writeData:[_wsPI requestPrinterBatteryStatus]];
}



@end
