//
//  ELMeatProbeBoxMultipleBleParser.m
//  Pods
//

#import "ELMeatProbeBoxMultipleBleParser.h"
#import "ELMeatProbeBoxMultipleBleBBQParamModel.h"


@interface ELMeatProbeBoxMultipleBleParser()

//@property (nonatomic, strong) ELMeatProbeBoxMultipleStatusModel *boxStatus;

@end

@implementation ELMeatProbeBoxMultipleBleParser

- (void)parseA7Payload:(nonnull NSData *)payload {
    NSLog(@"#### parseA7Payload payload:%@", payload.debugDescription);
    
    Byte *bytes = (Byte *)payload.bytes;
    Byte cmd = bytes[0];
    
    switch (cmd) {
        case ELMeatProbeBoxMultipleCMDMcuUploadStatus_Type02:
            [self parseStatusData:payload];
            break;
        case ELMeatProbeBoxMultipleCCMDAppSwitchUnit_Type03:
        {
            UInt8 unit = bytes[1];
            if (self.delegate && [self.delegate respondsToSelector:@selector(meatProbeBoxMultipleBleParser:unit:)]) {
                [self.delegate meatProbeBoxMultipleBleParser:self unit:unit];
            }
        }
            break;
//        case ELMeatProbeBoxMultipleCCMDAppSwitchUnitResponse_Type04:
//            break;
        case ELMeatProbeBoxMultipleCMDStartAlarmState_Type05: {
            Byte i = 1;
            NSData *macData = [payload subdataWithRange:NSMakeRange(i, 6)];
            i += 6;
            Byte state = bytes[i];
            if (self.delegate && [self.delegate respondsToSelector:@selector(meatProbeBoxMultipleBleParser:startAlarmWithState:macData:mac:)]) {
                [self.delegate meatProbeBoxMultipleBleParser:self startAlarmWithState:state macData:macData mac:[self macDataToString:macData]];
            }
        }
            break;
//        case ELMeatProbeBoxMultipleCMDStartAlarmStateResponse_Type06:
//            break;
        case ELMeatProbeBoxMultipleCMDStoplAlarm_Type07: {
            Byte i = 1;
            NSData *macData = [payload subdataWithRange:NSMakeRange(i, 6)];
            if (self.delegate && [self.delegate respondsToSelector:@selector(meatProbeBoxMultipleBleParser:stopAlarmWithMacData:mac:)]) {
                [self.delegate meatProbeBoxMultipleBleParser:self stopAlarmWithMacData:macData mac:[self macDataToString:macData]];
            }
        }
            break;
//        case ELMeatProbeBoxMultipleCMDStoplAlarmResponse_Type08:
//            break;
        case ELMeatProbeBoxMultipleCMDSetInfo_Type09:
        case ELMeatProbeBoxMultipleCMDSetInfoResponse_Type0A: {
            [self parseSetInfoParamData:payload];
        }
            break;
            
        case ELMeatProbeBoxMultipleCMDColorInfoResponse_Type0C: {
            NSMutableArray *arr = [NSMutableArray array];
            if (payload.length < 4) break;
            
            for (int i=2; i<payload.length; i+=2) {
                UInt8 probeIndex = bytes[i];
                UInt8 colorIndex = bytes[i+1];
                ELMeatProbeBoxMultipleColorInfoModel *color = [[ELMeatProbeBoxMultipleColorInfoModel alloc] init];
                color.probeIndex = probeIndex;
                color.colorIndex = colorIndex;
                [arr addObject:color];
            }
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(meatProbeBoxMultipleBleParser:colorInfos:)]) {
                [self.delegate meatProbeBoxMultipleBleParser:self colorInfos:arr.copy];
            }
        }
            
        default:
            break;
    }
}

- (NSString *)macDataToString:(NSData *)macData {
    if (macData == nil || macData.length != 6) {
        return nil;
    }
    Byte *bytes = (Byte *)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;
}

- (void)parseSetInfoParamData:(NSData *)payload {
    NSLog(@"##### parseSetInfoParamData payload:%@", payload.debugDescription);
    
    if (payload.length != 72 && payload.length != 140) {
        return;
    }
    
    Byte *bytes = (Byte *)payload.bytes;
    
    Byte i = 1;
    Byte option = bytes[i];
    i = 2;
    Byte supportProbe = bytes[i];
    
    i = 3;
//    NSMutableArray *arr = [NSMutableArray array];
//    
//    while (i < payload.length) {
        //探针 1
        ELMeatProbeBoxMultipleBleBBQParamModel *param = [[ELMeatProbeBoxMultipleBleBBQParamModel alloc] init];
        param.timestamp = [NSDate date].timeIntervalSince1970;
        
        param.macData = [payload subdataWithRange:NSMakeRange(i, 6)];
        i += 6;
        param.state = bytes[i];
        i += 1;
        param.foodType = bytes[i];//if (param.foodType == 0xFF) param.foodType = 0xFE;
        i += 1;
        param.targetTemperatureRaw_C = (bytes[i] << 0) | (bytes[i+1] << 8);
        i += 2;
        param.targetTemperatureRaw_F = (bytes[i] << 0) | (bytes[i+1] << 8);
        i += 2;
        param.ambientMaxTemperatureRaw_C = (bytes[i] << 0) | (bytes[i+1] << 8);
        i += 2;
        param.ambientMaxTemperatureRaw_F = (bytes[i] << 0) | (bytes[i+1] << 8);
        i += 2;
        param.ambientMinTemperatureRaw_C = (bytes[i] << 0) | (bytes[i+1] << 8);
        i += 2;
        param.ambientMinTemperatureRaw_F = (bytes[i] << 0) | (bytes[i+1] << 8);
        i += 2;
        param.cookingId = (bytes[i] << 0) | (bytes[i+1] << 8) | (bytes[i+2] << 16) | (bytes[i+3] << 24);
        i += 4;
        param.workTime = (bytes[i] << 0) | (bytes[i+1] << 8) | (bytes[i+2] << 16) | (bytes[i+3] << 24);
        i += 4;
        param.timerCount = (bytes[i] << 0) | (bytes[i+1] << 8) | (bytes[i+2] << 16) | (bytes[i+3] << 24);
        i += 4;
        UInt32 alarmTemperaturePercentU32 = (bytes[i] << 0) | (bytes[i+1] << 8) | (bytes[i+2] << 16) | (bytes[i+3] << 24);
        float alarmTemperaturePercent = 0;
        memcpy(&alarmTemperaturePercent, &alarmTemperaturePercentU32, sizeof(UInt32));
        param.alarmTemperaturePercent = alarmTemperaturePercent;
        i += 4;
        param.foodRawness = bytes[i];
        i += 1;
        param.remarkData = [payload subdataWithRange:NSMakeRange(i, 32)];
        i += 32;
        
        if (param.state == 0) {
            [param clearAll];
        }
        
//        [arr addObject:param];
//    }
    
//    if (self.delegate && [self.delegate respondsToSelector:@selector(meatProbeBoxMultipleBleParser:option:supportProbe:params:)]) {
//        [self.delegate meatProbeBoxMultipleBleParser:self option:option supportProbe:supportProbe params:arr.copy];
//    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(meatProbeBoxMultipleBleParser:option:supportProbe:param:)]) {
        [self.delegate meatProbeBoxMultipleBleParser:self option:option supportProbe:supportProbe param:param];
    }
}

- (void)parseStatusData:(NSData *)payload {
    if (payload.length != 25 && payload.length != 41) {
        return;
    }
    
    Byte *bytes = (Byte *)payload.bytes;
    
    ELMeatProbeBoxMultipleStatusModel *boxStatus = [[ELMeatProbeBoxMultipleStatusModel alloc] init];
    boxStatus.timestamp = [NSDate date].timeIntervalSince1970;
    boxStatus.boxMacString = self.mac;
    
    Byte i = 1;
    boxStatus.version = bytes[i];
    i = 2;
    boxStatus.probeMaxCount = bytes[i];
    i = 3;
    boxStatus.probeCount = bytes[i];
    i = 4;
    boxStatus.batteryStatus = bytes[i] >> 7;
    boxStatus.batteryPercent = bytes[i] & 0x7F;
    i = 5;
    boxStatus.unit = bytes[i];
    i = 9;
    NSMutableArray *arr = [NSMutableArray array];
    
    while (i < payload.length) {
        ELMeatProbeBoxMultipleProbeStatusModel *probeStatus = [[ELMeatProbeBoxMultipleProbeStatusModel alloc] init];
        probeStatus.timestamp = boxStatus.timestamp;
        probeStatus.index = bytes[i];
        i += 1;
        NSData *macData = [payload subdataWithRange:NSMakeRange(i, 6)];
        probeStatus.macData = macData;
        i += 6;
        probeStatus.internalTemperatureRaw = (bytes[i] << 0) | (bytes[i+1] << 8);
        // 凸(艹皿艹 ) //0x639F (25503)
        if (probeStatus.internalTemperatureRaw == 25503) {
            probeStatus.internalTemperatureRaw = 0xFFFF;
        }
        i += 2;
        probeStatus.ambientTemperatureRaw = (bytes[i] << 0) | (bytes[i+1] << 8);
        // 凸(艹皿艹 ) //0x639F (25503)
        if (probeStatus.ambientTemperatureRaw == 25503) {
            probeStatus.ambientTemperatureRaw = 0xFFFF;
        }
        i += 2;
        probeStatus.batteryStatus = bytes[i] >> 7;
        probeStatus.batteryPercent = bytes[i] & 0x7F;
        probeStatus.batteryRaw = bytes[i];
        i += 1;
        probeStatus.insertStatusRaw = bytes[i];
        i += 1;
        probeStatus.connectState = bytes[i];
        i += 3;
        [arr addObject:probeStatus];
    }
    boxStatus.probeStatusArray = arr.copy;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(meatProbeBoxMultipleBleParser:boxStatus:)]) {
        [self.delegate meatProbeBoxMultipleBleParser:self boxStatus:boxStatus];
    }
}

@end

@implementation ELMeatProbeBoxMultipleStatusModel

@end

@implementation ELMeatProbeBoxMultipleProbeStatusModel

- (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)internalTemperature_C {
    if (self.internalTemperatureRaw == 0xFFFF) {
        return 0xFFFF;
    }
    if (self.internalTemperatureUnit == ELDeviceTemperatureUnit_C) {
        return self.internalTemperature;
    }
    return [self convertF2C:self.internalTemperature];
}

- (NSInteger)internalTemperature_F {
    if (self.internalTemperatureRaw == 0xFFFF) {
        return 0xFFFF;
    }
    if (self.internalTemperatureUnit == ELDeviceTemperatureUnit_F) {
        return self.internalTemperature;
    }
    return [self convertC2F:self.internalTemperature];
}

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

- (ELDeviceTemperatureUnit)internalTemperatureUnit {
    return self.internalTemperatureRaw >> 15;
}

- (NSInteger)ambientTemperature_C {
    if (self.ambientTemperatureRaw == 0xFFFF) {
        return 0xFFFF;
    }
    if (self.ambientTemperatureUnit == ELDeviceTemperatureUnit_C) {
        return self.ambientTemperature;
    }
    return [self convertF2C:self.ambientTemperature];
}

- (NSInteger)ambientTemperature_F {
    if (self.ambientTemperatureRaw == 0xFFFF) {
        return 0xFFFF;
    }
    if (self.ambientTemperatureUnit == ELDeviceTemperatureUnit_F) {
        return self.ambientTemperature;
    }
    return [self convertC2F:self.ambientTemperature];
}

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

- (ELDeviceTemperatureUnit)ambientTemperatureUnit {
    return self.ambientTemperatureRaw >> 15;
}

//温度换算
- (float)convertF2C:(float)fahrenheit {
    return round((fahrenheit - 32) / 1.8);
}

- (float)convertC2F:(float)celsius {
    return round(celsius * 1.8 + 32);
}

@end

@implementation ELMeatProbeBoxMultipleColorInfoModel

@end
