package com.jsowell.netty.handler.yunkuaichong;

import com.alibaba.fastjson2.JSON;
import com.google.common.primitives.Bytes;
import com.jsowell.common.constant.Constants;
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 com.jsowell.pile.dto.ReservationChargingStartupResult;
import com.jsowell.pile.service.PileBasicInfoService;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 预约充电启动结果上送
 */
@Slf4j
@Component
public class ReservationChargingStartupResultHandler extends AbstractHandler{

    private final String type = YKCUtils.frameType2Str(YKCFrameTypeCode.RESERVATION_CHARGING_STARTUP_RESULT_CODE.getBytes());

    @Autowired
    private PileBasicInfoService pileBasicInfoService;

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

    @Override
    public byte[] supplyProcess(YKCDataProtocol ykcDataProtocol, Channel channel) {
        log.info("[===预约充电启动结果上送===] param:{}, channel:{}", JSON.toJSONString(ykcDataProtocol), channel.toString());
        // 消息体
        byte[] msgBody = ykcDataProtocol.getMsgBody();

        int startIndex = 0;
        int length = 16;

        // 交易流水号
        byte[] transactionCodeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String transactionCode = BytesUtil.bcd2Str(transactionCodeByteArr);

        // 桩编码
        startIndex += length;
        length = 7;
        byte[] pileSnByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String pileSn = BytesUtil.bcd2Str(pileSnByteArr);

        // 保存时间
        saveLastTimeAndCheckChannel(pileSn, channel);

        // 枪号
        startIndex += length;
        length = 1;
        byte[] connectorCodeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String connectorCode = BytesUtil.bcd2Str(connectorCodeByteArr);

        // vin
        startIndex += length;
        length = 17;
        byte[] vinCodeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String vinCode = BytesUtil.bcd2Str(vinCodeByteArr);

        // 启动结果
        startIndex += length;
        length = 1;
        byte[] startupResultByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String startupResult = BytesUtil.bcd2Str(startupResultByteArr);

        // 失败原因
        startIndex += length;
        length = 1;
        byte[] failReasonByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
        String failReason = BytesUtil.bcd2Str(failReasonByteArr);

        log.info("[===预约充电启动结果上送===]交易流水号:{}, 桩编号:{}, 枪号:{}, vin:{}, 启动结果:{}, 失败原因:{}",
                transactionCode, pileSn, connectorCode, vinCode, startupResult, failReason);


        ReservationChargingStartupResult chargingStartupResult = ReservationChargingStartupResult.builder()
                .transactionCode(transactionCode)
                .pileSn(pileSn)
                .connectorCode(connectorCode)
                .vinCode(vinCode)
                .startupResult(startupResult)
                .failReason(failReason)
                .build();
        pileBasicInfoService.startupResult(chargingStartupResult);


        /*
		应答
		确认结果	0x00 成功 0x01 失败
		 */
        byte[] confirmResultBytes = Constants.zeroByteArray;
        byte[] concatMsgBody = Bytes.concat(transactionCodeByteArr, pileSnByteArr, connectorCodeByteArr, confirmResultBytes);

        return getResult(ykcDataProtocol, concatMsgBody);
    }
}
