//
//  Sensors.m
//  DUREX Vendor Control
//
//  Created by Imanol Barba on 9/5/14.
//  Copyright (c) 2014 Emmoco. All rights reserved.
//

#import "Sensors.h"

@interface Sensors ()

@end

@implementation Sensors

- (id) init
{
    [self setChannelProductAvailability:[[NSMutableArray alloc]init]];
    [self setChannelStatus:[[NSMutableArray alloc]init]];
    [self setMoneyCollected:[[NSMutableArray alloc]init]];
    [self setMoneyReturned:[[NSMutableArray alloc]init]];
    [self setProductsSold:[[NSMutableArray alloc]init]];
    [self setChangeAvailable:[[NSMutableArray alloc] init]];
    
    return self;
}

- (void) setResponseValue:(NSString *)response
{
    [self setResponse:response];
    [self parseResponse];
}

- (void) parseResponse
{
    uint8_t channelCount = 0;
    uint16_t channelStatus = 0;
    uint16_t channelAvailability = 0;
    uint16_t channelMotors = 0;
    
    NSRange substrRange;
    substrRange.length = 5;
    substrRange.location = 2;
    
    channelStatus = [[[self response] substringWithRange:substrRange] intValue];
    substrRange.location += 5;
    
    channelAvailability = [[[self response] substringWithRange:substrRange] intValue];
    substrRange.location += 5;
    
    channelMotors = [[[self response] substringWithRange:substrRange] intValue];
    substrRange.location += 5;

    for(int i = 0; i < MAX_CHANNELS; i++)
    {
        if(channelStatus & 0x01)
        {
            channelCount++;
        }
        channelStatus >>= 1;
    }
    [self setNumChannels:channelCount];
    
    for(int i = 0; i < MAX_CHANNELS; i++)
    {
        if(channelAvailability & 0x01)
        {
            [[self channelProductAvailability] insertObject:[NSNumber numberWithBool:TRUE] atIndex:i];
        }
        else
        {
            [[self channelProductAvailability] insertObject:[NSNumber numberWithBool:FALSE] atIndex:i];
        }
        channelAvailability >>= 1;
    }

    for(int i = 0; i < MAX_CHANNELS; i++)
    {
        if(channelMotors & 0x01)
        {
            [[self channelStatus] insertObject:[NSNumber numberWithBool:TRUE] atIndex:i];
        }
        else
        {
            [[self channelStatus] insertObject:[NSNumber numberWithBool:FALSE] atIndex:i];
        }
        channelMotors >>= 1;
    }
    
    if([[self response] characterAtIndex:17] == '1')
    {
        [self setDoorOpen:TRUE];
    }
    else
    {
        [self setDoorOpen:FALSE];
    }
    
    if([[self response] characterAtIndex:18] == '1')
    {
        [self setCoilOpen:TRUE];
    }
    else
    {
        [self setCoilOpen:FALSE];
    }
    substrRange.length = 3;
    substrRange.location = 19;
    
    for(int i = 0; i < MONEY_IN_NUM_UNITS; i++)
    {
        [[self moneyCollected] insertObject:[NSNumber numberWithInt:[[[self response] substringWithRange:substrRange] intValue]] atIndex:i];
        substrRange.location += 3;
    }
    for(int i = 0; i < MONEY_OUT_NUM_UNITS; i++)
    {
        [[self moneyReturned] insertObject:[NSNumber numberWithInt:[[[self response] substringWithRange:substrRange] intValue]] atIndex:i];
        substrRange.location += 3;
    }
    
    substrRange.length = 2;
    for(int i = 0; i < MAX_PRODUCTS; i++)
    {
        [[self productsSold] insertObject:[NSNumber numberWithInt:[[[self response] substringWithRange:substrRange] intValue]] atIndex:i];
        substrRange.location += 2;
    }
    
    if([[self response] characterAtIndex:78] == '1')
    {
        [[self changeAvailable]insertObject:[NSNumber numberWithBool:TRUE] atIndex:0];
    }
    else
    {
        [[self changeAvailable]insertObject:[NSNumber numberWithBool:FALSE] atIndex:0];
    }
    
    if([[self response] characterAtIndex:79] == '1')
    {
        [[self changeAvailable]insertObject:[NSNumber numberWithBool:TRUE] atIndex:1];
    }
    else
    {
        [[self changeAvailable]insertObject:[NSNumber numberWithBool:FALSE] atIndex:1];
    }
}

@end
