//
//  ELMeatProbeBoxMultipleBleBBQParamModel.m
//  AILinkComponentMeatProbe
//

#import "ELMeatProbeBoxMultipleBleBBQParamModel.h"

@implementation ELMeatProbeBoxMultipleBleBBQParamModel

- (NSString *)macString {
    if (self.macData == nil || self.macData.length != 6) {
        return nil;
    }
    Byte *bytes = (Byte *)self.macData.bytes;
    NSString *str = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]];
    return str;
}

- (NSInteger)targetTemperature_C {
    NSInteger value = self.targetTemperatureRaw_C & 0x3FFF;
    if (self.targetTemperatureRaw_C & 0x4000) {
        value = -value;
    }
    return value;
}

- (NSInteger)targetTemperature_F {
    NSInteger value = self.targetTemperatureRaw_F & 0x3FFF;
    if (self.targetTemperatureRaw_F & 0x4000) {
        value = -value;
    }
    return value;
}

- (NSInteger)ambientMinTemperature_C {
    NSInteger value = self.ambientMinTemperatureRaw_C & 0x3FFF;
    if (self.ambientMinTemperatureRaw_C & 0x4000) {
        value = -value;
    }
    return value;
}

- (NSInteger)ambientMinTemperature_F {
    NSInteger value = self.ambientMinTemperatureRaw_F & 0x3FFF;
    if (self.ambientMinTemperatureRaw_F & 0x4000) {
        value = -value;
    }
    return value;
}

- (NSInteger)ambientMaxTemperature_C {
    NSInteger value = self.ambientMaxTemperatureRaw_C & 0x3FFF;
    if (self.ambientMaxTemperatureRaw_C & 0x4000) {
        value = -value;
    }
    return value;
}

- (NSInteger)ambientMaxTemperature_F {
    NSInteger value = self.ambientMaxTemperatureRaw_F & 0x3FFF;
    if (self.ambientMaxTemperatureRaw_F & 0x4000) {
        value = -value;
    }
    return value;
}

- (void)clearAll {
    self.state = 0; //State: 0x00:未开始,结束任务时,状态切为未开始,未开始状态时,不需要判定其他参数 0x01:已开始,开始任务时,状态切位已开始
    self.foodType = 0xFF; //食物类型
    self.targetTemperatureRaw_C = 0;//0xFFFF; //食物目标温度：℃//
    self.targetTemperatureRaw_F = 0;//0xFFFF; //食物目标温度：℉//
    self.ambientMaxTemperatureRaw_C = 0;//0xFFFF; //炉温目标上限：℃
    self.ambientMaxTemperatureRaw_F = 0;//0xFFFF; //炉温目标上限：℉
    self.ambientMinTemperatureRaw_C = 0;//0xFFFF; //炉温目标下限：℃
    self.ambientMinTemperatureRaw_F = 0;//0xFFFF; //炉温目标下限：℉
    self.cookingId = 0; //CookingID
    self.workTime = 0; //任务已运行时间,单位s
    self.timerCount = 0; //计时剩余时间(秒)
    self.alarmTemperaturePercent = 0.95; //提醒温度对目标温度百分比 0~1.0
    self.foodRawness = 0xFF; //食物熟度
    Byte bytes[32] = {0};
    self.remarkData = [NSData dataWithBytes:bytes length:sizeof(bytes)];
}

- (void)cookDone {
    [self clearAll];
}

- (NSData *)dataValue {
    NSMutableData *data = [NSMutableData data];
    [data appendData:self.macData];
    UInt32 alarmTemperaturePercent = 0;
    memcpy(&alarmTemperaturePercent, &(_alarmTemperaturePercent), sizeof(float));
    Byte bytes[] = { self.state, self.foodType, self.targetTemperatureRaw_C, self.targetTemperatureRaw_C >> 8, self.targetTemperatureRaw_F, self.targetTemperatureRaw_F >> 8, self.ambientMaxTemperatureRaw_C, self.ambientMaxTemperatureRaw_C >> 8, self.ambientMaxTemperatureRaw_F, self.ambientMaxTemperatureRaw_F >> 8, self.ambientMinTemperatureRaw_C, self.ambientMinTemperatureRaw_C >> 8, self.ambientMinTemperatureRaw_F, self.ambientMinTemperatureRaw_F >> 8, self.cookingId, self.cookingId >> 8, self.cookingId >> 16, self.cookingId >> 24, self.workTime, self.workTime >> 8, self.workTime >> 16, self.workTime >> 24, self.timerCount, self.timerCount >> 8, self.timerCount >> 16, self.timerCount >> 24, alarmTemperaturePercent, alarmTemperaturePercent >> 8, alarmTemperaturePercent >> 16, alarmTemperaturePercent >> 24, self.foodRawness };
    [data appendBytes:bytes length:sizeof(bytes)];
    [data appendData:self.remarkData];
    
    return data.copy;
}

- (NSString *)setRemark:(NSString *)remark {
    NSData *data = [remark dataUsingEncoding:NSUTF8StringEncoding];
    if (data.length > 32) {
        while (data.length > 29) {
            remark = [remark substringToIndex:(remark.length - 1)];
            data = [remark dataUsingEncoding:NSUTF8StringEncoding];
        }
        remark = [remark stringByAppendingString:@"..."];
        data = [remark dataUsingEncoding:NSUTF8StringEncoding];
    }
    Byte *bytes = (Byte *)data.bytes;
    NSUInteger total = data.length > 32 ? 32 : data.length;
    Byte remarkBytes[32] = {0};
    memset(remarkBytes, 0, 32);
    memcpy(remarkBytes, bytes, total);
    self.remarkData = [NSData dataWithBytes:remarkBytes length:sizeof(remarkBytes)];
    return [self remark];
}

- (NSString *)remark {
    Byte remarkBytes[64] = {0};
    [self.remarkData getBytes:remarkBytes length:self.remarkData.length];
    NSString *str = [NSString stringWithUTF8String:(const char *)remarkBytes];
    return str;
}


@end
