package com.fuyuanshen.global.mqtt.rule.bjq;

import com.fuyuanshen.common.core.utils.ImageToCArrayConverter;
import com.fuyuanshen.common.core.utils.StringUtils;
import com.fuyuanshen.common.json.utils.JsonUtils;
import com.fuyuanshen.common.redis.utils.RedisUtils;
import com.fuyuanshen.global.mqtt.base.MqttMessageRule;
import com.fuyuanshen.global.mqtt.base.MqttRuleContext;
import com.fuyuanshen.global.mqtt.config.MqttGateway;
import com.fuyuanshen.global.mqtt.constants.LightingCommandTypeConstants;
import com.fuyuanshen.global.mqtt.constants.MqttConstants;
import com.fuyuanshen.global.mqtt.listener.domain.FunctionAccessStatus;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.fuyuanshen.common.core.constant.GlobalConstants.FUNCTION_ACCESS_KEY;
import static com.fuyuanshen.common.core.constant.GlobalConstants.GLOBAL_REDIS_KEY;
import static com.fuyuanshen.common.core.utils.ImageToCArrayConverter.convertHexToDecimal;
import static com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants.DEVICE_KEY_PREFIX;

/**
 * 人员信息命令处理
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class BjqSendMessageRule implements MqttMessageRule {
    
    private final MqttGateway mqttGateway;

    @Override
    public String getCommandType() {
        return LightingCommandTypeConstants.SEND_MESSAGE;
    }

    @Override
    public void execute(MqttRuleContext context) {
        String functionAccess = FUNCTION_ACCESS_KEY + context.getDeviceImei();
        try {

//            Byte val2 = (Byte) context.getConvertArr()[1];
            String val2Str = context.getConvertArr()[1].toString();
            int val2 = Integer.parseInt(val2Str);
            System.out.println("收到设备信息命令:"+val2);
            if (val2 == 100) {
                RedisUtils.setCacheObject(functionAccess, FunctionAccessStatus.OK.getCode(), Duration.ofSeconds(20));
                return;
            }

            if(val2==200){
                String sendMessageIng = GLOBAL_REDIS_KEY+ DEVICE_KEY_PREFIX + context.getDeviceImei() + ":messageSending";
                RedisUtils.deleteObject(sendMessageIng);
                return;
            }


            String data = RedisUtils.getCacheObject(GLOBAL_REDIS_KEY+ DEVICE_KEY_PREFIX + context.getDeviceImei() + ":app_send_message_data");
            if (StringUtils.isEmpty(data)) {
                return;
            }

            byte[] arr = ImageToCArrayConverter.convertStringToByteArray(data);
            byte[] specificChunk = ImageToCArrayConverter.getChunk(arr, (val2 - 1), 512);
            log.info("第{}块数据大小: {} 字节", val2, specificChunk.length);
//            System.out.println("第" + val2 + "块数据: " + Arrays.toString(specificChunk));

            ArrayList<Integer> intData = new ArrayList<>();
            intData.add(6);
            intData.add(val2);
            ImageToCArrayConverter.buildArr(convertHexToDecimal(specificChunk), intData);
            intData.add(0);
            intData.add(0);
            intData.add(0);
            intData.add(0);
            
            Map<String, Object> map = new HashMap<>();
            map.put("instruct", intData);
            
            mqttGateway.sendMsgToMqtt(MqttConstants.GLOBAL_PUB_KEY + context.getDeviceImei(), 1, JsonUtils.toJsonString(map));
            log.info("发送设备信息数据到设备消息=>topic:{},payload:{}",
                    MqttConstants.GLOBAL_PUB_KEY + context.getDeviceImei(),
                    JsonUtils.toJsonString(map));

        } catch (Exception e) {
            log.error("处理发送设备信息时出错", e);
            RedisUtils.setCacheObject(functionAccess, FunctionAccessStatus.FAILED.getCode(), Duration.ofSeconds(20));
        }
    }
}
