package com.jsowell.netty.handler;

import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.core.domain.ykc.YKCDataProtocol;
import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.YKCUtils;
import com.jsowell.netty.factory.YKCOperateFactory;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 0x29 平台设置工作参数回复
 *
 * @author JS-ZZA
 * @date 2023/4/4 13:43
 */
@Component
@Slf4j
public class SettingPileWorkParamsHandler extends AbstractHandler{
    private final String type = YKCUtils.frameType2Str(YKCFrameTypeCode.SETTING_PILE_WORK_PARAMS_ANSWER_CODE.getBytes());

    @Override
    public void afterPropertiesSet() throws Exception {
        YKCOperateFactory.register(type, this);
    }

    @Override
    public byte[] supplyProcess(YKCDataProtocol ykcDataProtocol, Channel channel) {
        log.info("[====平台设置工作参数回复====] param:{}", JSONObject.toJSONString(ykcDataProtocol));
        // 获取消息体
        byte[] msgBody = ykcDataProtocol.getMsgBody();

        int startIndex = 0;
        int length = 7;

        // 桩编码
        byte[] pileSnByte = BytesUtil.copyBytes(msgBody, startIndex, length);
        String pileSn = BytesUtil.binary(pileSnByte, 16);

        // 保存时间
        saveLastTime(pileSn);

        // 设置状态 0x00-成功  0x01-失败
        startIndex += length;
        length = 1;
        byte[] settingStatusByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String settingStatus = BytesUtil.bcd2Str(settingStatusByteArr);

        // 充电桩类型  0x00:直流 0x01:交流
        startIndex += length;
        byte[] pileTypeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String pileType = BytesUtil.bcd2Str(pileTypeByteArr);

        // 最高充电电压  精确到小数点后一位；待机置零
        startIndex += length;
        length = 2;
        byte[] maxChargingVoltageByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String maxChargingVoltage = YKCUtils.convertVoltageCurrent(maxChargingVoltageByteArr);

        // 最高充电电流
        startIndex += length;
        byte[] maxChargingCurrentByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String maxChargingCurrent = YKCUtils.convertVoltageCurrent(maxChargingCurrentByteArr);

        // 最大充电功率
        startIndex += length;
        byte[] maxChargingPowerByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String maxChargingPower = YKCUtils.convertVoltageCurrent(maxChargingPowerByteArr);

        // 当前充电电压
        startIndex += length;
        byte[] instantChargingVoltageByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String instantChargingVoltage = YKCUtils.convertVoltageCurrent(instantChargingVoltageByteArr);

        // 当前充电电流
        startIndex += length;
        byte[] instantChargingCurrentByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String instantChargingCurrent = YKCUtils.convertVoltageCurrent(instantChargingCurrentByteArr);

        // 当前充电功率
        startIndex += length;
        byte[] instantChargingPowerByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String instantChargingPower = YKCUtils.convertVoltageCurrent(instantChargingPowerByteArr);

        log.info("[====平台设置工作参数回复====] 桩编号:{}, 设置状态:{}, 充电桩类型:{}, 最大充电电压:{}, 最高充电电流:{}, " +
                        "最大充电功率:{}, 当前充电电压:{}, 当前充电电流:{}, 当前充电功率:{}",
                pileSn, settingStatus, pileType, maxChargingVoltage, maxChargingCurrent, maxChargingPower, instantChargingVoltage,
                instantChargingCurrent, instantChargingPower);

        return null;
    }
}
