package com.xhpc.pp.logic;

import com.xhpc.common.api.PowerPileService;
import com.xhpc.common.enums.StationDeviceEnum;
import com.xhpc.pp.controller.ChargingController;
import com.xhpc.pp.domain.XhpcDeviceMessage;
import com.xhpc.pp.mapper.XhpcDeviceMessageMapper;
import com.xhpc.pp.tx.ServiceParameter;
import com.xhpc.pp.tx.ServiceResult;
import com.xhpc.pp.tx.logic.ServiceLogic;
import com.xhpc.pp.utils.HexUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Lazy
@Component("BmsErrorDataLogic")
public class BmsErrorDataLogic implements ServiceLogic {

    private static final Logger log = LoggerFactory.getLogger(BmsErrorDataLogic.class);

    @Resource
    XhpcDeviceMessageMapper deviceMessageMapper;
    @Resource
    private PowerPileService powerPileService;

    @Override
    public ServiceResult service(ServiceParameter sp) throws Exception {

        String remark = "充电桩与 BMS 充电错误报文";
        Map<String, Object> req = sp.getParameters();
// todo test only
//        String hex = (String) req.get("hex");
//        int idxStart = 60;
//        String flags1 = HexUtils.toBinaryString(hex.substring(idxStart, 2 + idxStart));//每字节若干检测标识位
//        idxStart = 2 + idxStart;
//        String flags2 = HexUtils.toBinaryString(hex.substring(idxStart, 2 + idxStart));
//        idxStart = 2 + idxStart;
//        String flags3 = HexUtils.toBinaryString(hex.substring(idxStart, 2 + idxStart));
//        idxStart = 2 + idxStart;
//        String flags4 = HexUtils.toBinaryString(hex.substring(idxStart, 2 + idxStart));
//        idxStart = 2 + idxStart;
//        String flags5 = HexUtils.toBinaryString(hex.substring(idxStart, 2 + idxStart));
//        idxStart = 2 + idxStart;
//        String flags6 = HexUtils.toBinaryString(hex.substring(idxStart, 2 + idxStart));
//        idxStart = 2 + idxStart;
//        String flags7 = HexUtils.toBinaryString(hex.substring(idxStart, 2 + idxStart));
//        idxStart = 2 + idxStart;
//        String flags8 = HexUtils.toBinaryString(hex.substring(idxStart, 2 + idxStart));
//        idxStart = 0;//1
//        String flag = flags1.substring(idxStart, 2 + idxStart);
//        String spn2560x00 = flag.equals("00") ? "正常" : (flag.equals("01") ? "超时" : "不可信状态");//接收 SPN2560=0x00 的充电机辨识报文超时
//        idxStart = 2 + idxStart;
//        flag = flags1.substring(idxStart, 2 + idxStart);
//        String spn2560xAA = flag.equals("00") ? "正常" : (flag.equals("01") ? "超时" : "不可信状态");//接收 SPN2560=0xAA 的充电机辨识报文超时
//        idxStart = 2 + idxStart;
//        flag = flags1.substring(idxStart, 4 + idxStart);
//        String reserved1 = flag;//预留位
//        idxStart = 0;//2
//        String rectsyncmax = flag.equals("00") ? "正常" : (flag.equals("01") ? "超时" : "不可信状态");//接收充电机的时间同步和充电机最大输出能力报文超时
//        idxStart = 2 + idxStart;
//        flag = flags2.substring(idxStart, 2 + idxStart);
//        String recchgprepcompl = flag.equals("00") ? "正常" : (flag.equals("01") ? "超时" : "不可信状态");//接收充电机完成充电准备报文超时
//        idxStart = 2 + idxStart;
//        flag = flags2.substring(idxStart, 4 + idxStart);
//        String reserved2 = flag;//预留位
//        idxStart = 0;//3
//        flag = flags3.substring(idxStart, 2 + idxStart);
//        String recchgst = flag.equals("00") ? "正常" : (flag.equals("01") ? "超时" : "不可信状态");//接收充电机充电状态报文超时
//        idxStart = 2 + idxStart;
//        flag = flags3.substring(idxStart, 2 + idxStart);
//        String recchgstop = flag.equals("00") ? "正常" : (flag.equals("01") ? "超时" : "不可信状态");//接收充电机中止充电报文超时
//        idxStart = 2 + idxStart;
//        flag = flags3.substring(idxStart, 4 + idxStart);
//        String reserved3 = flag;//预留位
//        idxStart = 0; // 4
//        flag = flags4.substring(idxStart, 2 + idxStart);
//        String recchgsum = flag.equals("00") ? "正常" : (flag.equals("01") ? "超时" : "不可信状态");//接收充电机充电统计报文超时
//        idxStart = 2 + idxStart;
//        flag = flags4.substring(idxStart, 6 + idxStart);
//        String reserved4 = flag;//BMS 其他
//        idxStart = 0; // 5
//        flag = flags5.substring(idxStart, 2 + idxStart);
//        String recbmsvid = flag.equals("00") ? "正常" : (flag.equals("01") ? "超时" : "不可信状态");//接收 BMS 和车辆的辨识报文超时
//        idxStart = 2 + idxStart;
//        flag = flags5.substring(idxStart, 6 + idxStart);
//        String reserved5 = flag;//BMS 其他
//        idxStart = 0;//6
//        flag = flags6.substring(idxStart, 2 + idxStart);
//        String recbatarg = flag.equals("00") ? "正常" : (flag.equals("01") ? "超时" : "不可信状态");//接收电池充电参数报文超时
//        idxStart = 2 + idxStart;
//        flag = flags6.substring(idxStart, 2 + idxStart);
//        String recbmsprecompl = flag.equals("00") ? "正常" : (flag.equals("01") ? "超时" : "不可信状态");//接收 BMS 完成充电准备报文超时
//        idxStart = 2 + idxStart;
//        flag = flags3.substring(idxStart, 4 + idxStart);
//        String reserved6 = flag;//预留位
//        idxStart = 0;//7
//        flag = flags7.substring(idxStart, 2 + idxStart);
//        String recbatsumsts = flag.equals("00") ? "正常" : (flag.equals("01") ? "超时" : "不可信状态");//接收电池充电总状态报文超时
//        idxStart = 2 + idxStart;
//        flag = flags7.substring(idxStart, 2 + idxStart);
//        String recbmschgreq = flag.equals("00") ? "正常" : (flag.equals("01") ? "超时" : "不可信状态");//接收电池充电要求报文超时
//        idxStart = 2 + idxStart;
//        flag = flags7.substring(idxStart, 2 + idxStart);
//        String recbmsstopchg = flag.equals("00") ? "正常" : (flag.equals("01") ? "超时" : "不可信状态");//接收 BMS 中止充电报文超时
//        idxStart = 2 + idxStart;
//        flag = flags3.substring(idxStart, 2 + idxStart);
//        String reserved7 = flag;//预留位
//        idxStart = 0;//8
//        flag = flags8.substring(idxStart, 2 + idxStart);
//        String recbumssum = flag.equals("00") ? "正常" : (flag.equals("01") ? "超时" : "不可信状态");//接收 BMS 充电统计报文超时
//        idxStart = 2 + idxStart;
//        flag = flags3.substring(idxStart, 6 + idxStart);
//        String reserved8 = flag;//预留位

        XhpcDeviceMessage deviceMessage = new XhpcDeviceMessage();
        deviceMessage.setType(StationDeviceEnum.PILE.getCode());
        deviceMessage.setSerialNumber(sp.getPileNo());
        deviceMessage.setRemark(remark);
        deviceMessage.setStatus(0);
        deviceMessage.setContent((String) req.get("hex"));
        deviceMessage.setChargeOrderNo((String) req.get("orderNo"));

        String gunId=(String) req.get("pileNo")+(String) req.get("gunId");
        //下发停止充电
        powerPileService.stopCharging((String) req.get("orderNo"), sp.getPileNo(),gunId, "0C");

        deviceMessageMapper.insertByDomain(deviceMessage);
        return new ServiceResult(false);
    }

}
