//
//  ELMeatProbeCustomDataModel.m
//  AILinkBleSDK
//

#import "ELMeatProbeBoxCustomDataModel.h"
#import <AILinkBleSDK/NSData+AILinkBle.h>
#import <objc/runtime.h>


@implementation ELMeatProbeBoxCustomDataModel

+ (Byte)version {
    return 0x02;
}

+ (Byte)modelSize {
    return 128;//sizeof(ELMeatProbeCustomDataStruct);
}

+ (instancetype)modelWithData:(NSData *)data {
    if (data.length != self.modelSize) {
        NSLog(@"数据长度错误 data:%@", data);
        return nil;
    }
    
    ELMeatProbeBoxCustomDataModel *model = [[ELMeatProbeBoxCustomDataModel alloc] initWithData:data];
    if (model.customStruct.ver != self.version) {
        NSLog(@"数据版本错误 data:%@", data);
        return nil;
    }
    
    return model;
}

- (instancetype)init {
    if (self = [super init]) {
        memset(&_customStruct, 0, sizeof(_customStruct));
        _customStruct.ver = self.class.version;
    }
    return self;
}

- (instancetype)initWithData:(NSData *)data {
    if (self = [self init]) {
        memcpy(&_customStruct, data.bytes, sizeof(_customStruct));
    }
    return self;
}

- (NSData *)dataValue {
    Byte bytes[128] = {0x00};
    memcpy(bytes, &_customStruct, sizeof(_customStruct));
    return [NSData dataWithBytes:bytes length:sizeof(bytes)];
}

- (NSString *)remark {
    char *remarkBytes[64] = {0};
    memcpy(remarkBytes, _customStruct.remarkBytes, sizeof(_customStruct.remarkBytes));
    NSString *str = [NSString stringWithUTF8String:(const char *)remarkBytes];
    return str;
}

- (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;
    memset(_customStruct.remarkBytes, 0, 32);
    memcpy(_customStruct.remarkBytes, bytes, total);
    return [self remark];
}

#pragma mark setter getter

- (NSInteger)ver {
    return self.customStruct.ver;
}

- (NSInteger)cookingId {
    return self.customStruct.cookingId;
}

- (NSInteger)foodType {
    return self.customStruct.foodType;
}

- (NSInteger)foodRawness {
    return self.customStruct.foodRawness;
}

- (double)alarmTemperaturePercent {
    return self.customStruct.alarmTemperaturePercent;
}

- (NSInteger)alarmTemperature_C {
    return self.customStruct.alarmTemperature_C;
}

- (NSInteger)alarmTemperature_F {
    return self.customStruct.alarmTemperature_F;
}

- (void)setAlarmTemperaturePercent:(double)alarmTemperaturePercent {
    _customStruct.alarmTemperaturePercent = alarmTemperaturePercent;
    _customStruct.alarmTemperature_C = _customStruct.targetTemperature_C * alarmTemperaturePercent;
    _customStruct.alarmTemperature_F = _customStruct.targetTemperature_F * alarmTemperaturePercent;
}

- (ELDeviceTemperatureUnit)currentUnit {
    return self.customStruct.currentUnit;
}


#pragma mark ---- description

- (NSString *)description {
    NSMutableString *desc = [NSStringFromClass([self class]) stringByAppendingString:@": {\n"].mutableCopy;
    
    [desc appendString:[NSString stringWithFormat:@"ver: %@,\n", @(self.customStruct.ver)]];
    [desc appendString:[NSString stringWithFormat:@"cookingId: %@,\n", @(self.customStruct.cookingId)]];
    [desc appendString:[NSString stringWithFormat:@"foodType: %@,\n", @(self.customStruct.foodType)]];
    [desc appendString:[NSString stringWithFormat:@"foodRawness: %@,\n", @(self.customStruct.foodRawness)]];
    [desc appendString:[NSString stringWithFormat:@"targetTemperature_C: %@,\n", @(self.customStruct.targetTemperature_C)]];
    [desc appendString:[NSString stringWithFormat:@"targetTemperature_F: %@,\n", @(self.customStruct.targetTemperature_F)]];
    [desc appendString:[NSString stringWithFormat:@"ambientMinTemperature_C: %@,\n", @(self.customStruct.ambientMinTemperature_C)]];
    [desc appendString:[NSString stringWithFormat:@"ambientMinTemperature_F: %@,\n", @(self.customStruct.ambientMinTemperature_F)]];
    [desc appendString:[NSString stringWithFormat:@"ambientMaxTemperature_C: %@,\n", @(self.customStruct.ambientMaxTemperature_C)]];
    [desc appendString:[NSString stringWithFormat:@"ambientMaxTemperature_F: %@,\n", @(self.customStruct.ambientMaxTemperature_F)]];
    [desc appendString:[NSString stringWithFormat:@"alarmTemperaturePercent: %@,\n", @(self.alarmTemperaturePercent)]];
    [desc appendString:[NSString stringWithFormat:@"timerStart: %@,\n", @(self.customStruct.timerStart)]];
    [desc appendString:[NSString stringWithFormat:@"timerEnd: %@,\n", @(self.customStruct.timerEnd)]];
    [desc appendString:[NSString stringWithFormat:@"alarmTemperature_C: %@,\n", @(self.customStruct.alarmTemperature_C)]];
    [desc appendString:[NSString stringWithFormat:@"alarmTemperature_F: %@,\n", @(self.customStruct.alarmTemperature_F)]];
    [desc appendString:[NSString stringWithFormat:@"currentUnit: %@,\n", @(self.customStruct.currentUnit)]];
    [desc appendString:[NSString stringWithFormat:@"remark: %@,\n", self.remark]];
    
    [desc appendString:@"}"];
    return desc;
}

- (NSString *)debugDescription {
    return [self description];
}


@end
