//
//  EddystoneConfigParam.m
//  JoywayBeacon
//
//  Created by Bob Xia on 2018/7/3.
//  Copyright © 2018年 Bob Xia. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "EddystoneConfigParam.h"


@interface EddystoneConfigParam()
{

}

@end

@implementation EddystoneConfigParam

static NSString * FLAG_UID_NAMESPANCE = @"UIDN+";
static NSString * FLAG_UID_INSTANCE = @"UIDI+";
static NSString * FLAG_TX_POWER = @"TXPOWER+";
static NSString * FLAG_TX_INTERVAL = @"TXINTERVAL+";
static NSString * FLAG_URL_TYPE_AND_LENGTH = @"URLTYPELEN+";
static NSString * FLAG_URL = @"URL+";


- (NSInteger)numberWithHexString:(NSString *)hexString{
    
    const char *hexChar = [hexString cStringUsingEncoding:NSUTF8StringEncoding];
    
    int hexNumber;
    
    sscanf(hexChar, "%x", &hexNumber);
    
    return (NSInteger)hexNumber;
}

- (NSData*) getBytes: (NSString*) flag
{
    return [flag dataUsingEncoding:NSUTF8StringEncoding];
}

- (NSData*) getBytes_UID_Namespace
{
    NSData * flagData = [self getBytes : FLAG_UID_NAMESPANCE ];
    NSString * s = [self._uidNamespace stringByReplacingOccurrencesOfString:@"-" withString:@""];
    s = [s stringByReplacingOccurrencesOfString:@"," withString:@""];
    s = [s stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSMutableData * res = [[NSMutableData alloc] initWithData:flagData];
    
    for (int i = 0; i < s.length / 2; i++)
    {
        NSRange range = {(i*2), 2};
        NSString * itemStr = [s substringWithRange:range];
        NSInteger itemInt = [self numberWithHexString:itemStr];
        [res appendBytes:&itemInt length:1];
    }
    
    return res;
}

- (NSData*) getBytes_UID_Instance
{
    NSData * flagData = [self getBytes : FLAG_UID_INSTANCE ];
    NSString * s = [self._uidInstance stringByReplacingOccurrencesOfString:@"-" withString:@""];
    s = [s stringByReplacingOccurrencesOfString:@"," withString:@""];
    s = [s stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSMutableData * res = [[NSMutableData alloc] initWithData: flagData];
    
    
    for (int i = 0; i < self._uidInstance.length / 2; i++)
    {
        NSRange range = {(i*2), 2};
        NSString * itemStr = [self._uidInstance substringWithRange:range];
        NSInteger itemInt = [self numberWithHexString:itemStr];
        [res appendBytes:&itemInt length:1];
    }
    
    
    
    return res;
}

- (NSData*) getBytes_TxPower
{
    NSData * flagData = [self getBytes:FLAG_TX_POWER];
    NSMutableData * res = [[NSMutableData alloc] initWithData:flagData];
    Byte txPower = self._txPower;
    [res appendBytes:(&txPower) length:1];
    return res;
}

- (NSData*) getBytes_TxInterval
{
    NSData * flagData = [self getBytes:FLAG_TX_INTERVAL];
    NSMutableData * res = [[NSMutableData alloc] initWithData:flagData];
    Byte bt = (Byte)(self._txInterval/100); //unit = 100ms
    [res appendBytes:(&bt) length:1];
    return res;
}

- (NSData*) getBytes_UrlTypeAndLength
{
    NSData * flagData = [self getBytes:FLAG_URL_TYPE_AND_LENGTH];
    NSMutableData * res = [[NSMutableData alloc] initWithData:flagData];
    
    Byte type_and_length = (Byte) self._url.length;
    if (self._https_or_http)
        type_and_length |= 0x80;
    [res appendBytes:&type_and_length length:1];
    
    return res;
}

- (NSData*) getBytes_URL
{
    NSData * flagData = [self getBytes:FLAG_URL];
    NSMutableData * res = [[NSMutableData alloc] initWithData:flagData];
    [res appendData:[self getBytes:self._url]];
    
    return res;
    
    /*ArrayList<Byte> res = this.getFlagBytes(FLAG_URL);
    for (byte b : _url.getBytes()){
        res.add(Byte.valueOf(b));
    }
    return ConvertEx.ByteListTobyteArray(res);*/
}

- (NSMutableArray<NSData*>*) getBytesDataListToSend
{
    NSMutableArray<NSData*> * items = [[NSMutableArray alloc]initWithCapacity:0];
    [items addObject:[self getBytes_UID_Namespace]];
    [items addObject:[self getBytes_UID_Instance]];
    [items addObject:[self getBytes_TxPower]];
    [items addObject:[self getBytes_TxInterval]];
    [items addObject:[self getBytes_UrlTypeAndLength]];
    [items addObject:[self getBytes_URL]];
    return items;
}


@end
