package com.jsowell.pile.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode;
import com.jsowell.common.core.page.PageResponse;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.StringUtils;
import com.jsowell.common.util.YKCUtils;
import com.jsowell.pile.domain.PileMsgRecord;
import com.jsowell.pile.dto.QueryPileDTO;
import com.jsowell.pile.mapper.PileMsgRecordMapper;
import com.jsowell.pile.service.IPileMsgRecordService;
import com.jsowell.pile.vo.web.PileCommunicationLogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PileMsgRecordServiceImpl implements IPileMsgRecordService {

	@Autowired
	private PileMsgRecordMapper pileMsgRecordMapper;

	@Override
	public void save(String pileSn, String connectorCode, String frameType, String jsonMsg, String originalMsg) {
		PileMsgRecord pileMsgRecord = PileMsgRecord.builder()
				.pileSn(pileSn)
				.connectorCode(connectorCode)
				.jsonMsg(jsonMsg)
				.frameType(frameType)
				.originalMsg(originalMsg)
				.build();
		pileMsgRecordMapper.insertSelective(pileMsgRecord);
	}

	// @Override
	// public List<PileMsgRecord> getByConnectorCodeList(List<String> connectorCodeList) {
	// 	if (CollectionUtils.isEmpty(connectorCodeList)) {
	// 		return Lists.newArrayList();
	// 	}
	// 	return pileMsgRecordMapper.getByConnectorCodeList(connectorCodeList);
	// }

	public static void main(String[] args) {
		String type = BytesUtil.bcd2Str(YKCFrameTypeCode.TRANSACTION_RECORDS_CODE.getBytes());
		System.out.println(type);
		String binary = BytesUtil.binary(YKCFrameTypeCode.TRANSACTION_RECORDS_CODE.getBytes(), 16);
		System.out.println(binary);
		String s = YKCUtils.frameType2Str(YKCFrameTypeCode.TRANSACTION_RECORDS_CODE.getBytes());
		System.out.println(s);

	}

	@Override
	public PageResponse getPileFeedList(QueryPileDTO dto) {
		// 分页
		PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
		List<PileMsgRecord> pileFeedList = pileMsgRecordMapper.getPileFeedList(dto.getPileSn());
		PageInfo<PileMsgRecord> pageInfo = new PageInfo<>(pileFeedList);

		List<PileCommunicationLogVO> list = new ArrayList<>();
		for (PileMsgRecord pileMsgRecord : pageInfo.getList()) {
			PileCommunicationLogVO vo = new PileCommunicationLogVO();
			String frameType = pileMsgRecord.getFrameType();
			vo.setFrameType(frameType);
			vo.setFrameTypeStr(YKCFrameTypeCode.getFrameTypeStr(frameType));
			vo.setDescription(generateDescription(pileMsgRecord));
			vo.setOriginalMsg(pileMsgRecord.getOriginalMsg());
			vo.setPileSn(pileMsgRecord.getPileSn());
			vo.setCreateTime(pileMsgRecord.getCreateTime());
			list.add(vo);
		}

		PageResponse pageResponse = PageResponse.builder()
				.pageNum(pageInfo.getPageNum())
				.pageSize(pageInfo.getPageSize())
				.list(list)
				.total(pageInfo.getTotal())
				.pages(pageInfo.getPages())
				.build();
		return pageResponse;
	}

	/**
	 * 生成展示在后管的描述
	 * @param pileMsgRecord
	 * @return
	 */
	@Override
	public String generateDescription(PileMsgRecord pileMsgRecord) {
		String result = null;
		String frameType = pileMsgRecord.getFrameType();
		String jsonMsg = pileMsgRecord.getJsonMsg();
		switch (frameType) {
			case "0x01" :
				result = loginMsg(jsonMsg);
				break;
			case "0x31" :
				result = requestStartChargingMsg(jsonMsg);
				break;
			case "9999" :
				result = jsonMsg;
				break;
			default:

		}

		return result;
	}

	private String loginMsg(String jsonMsg) {
		JSONObject jsonObject = JSONObject.parseObject(jsonMsg);

		// iccid
		String iccid = jsonObject.getString("iccid");

		// 设备程序版本
		String programVersion = jsonObject.getString("programVersion");

		// 枪口数量
		Integer connectorNum = jsonObject.getInteger("connectorNum");

		// 网络链接类型
		String internetConnection = jsonObject.getString("internetConnection");
		if (StringUtils.equals(internetConnection, "00")) {
			internetConnection = "SIM卡";
		} else if (StringUtils.equals(internetConnection, "01")) {
			internetConnection = "LAN";
		} else if (StringUtils.equals(internetConnection, "02")) {
			internetConnection = "WAN";
		} else {
			internetConnection = "其他";
		}

		// 桩类型
		String pileType = jsonObject.getString("pileType");
		if (StringUtils.equals(pileType, "01")) {
			pileType = "运营桩";
		} else {
			pileType = "个人桩";
		}

		return "SIM卡号:" + iccid + ", " +
				"设备程序版本:" + programVersion + ", " +
				"桩类型:" + pileType + ", " +
				"枪数量:" + connectorNum + ", " +
				"网络链接类型:" + internetConnection;
	}

	private String requestStartChargingMsg(String jsonMsg) {
		JSONObject jsonObject = JSONObject.parseObject(jsonMsg);
		// 启动方式
		String startMode = jsonObject.getString("startMode");
		if (StringUtils.equals(startMode, "0x01")) {
			startMode = "离线卡启动";
		} else if (StringUtils.equals(startMode, "0x02")) {
			startMode = "帐号启动";
		} else if (StringUtils.equals(startMode, "0x03")) {
			startMode = "VIN启动";
		} else {
			startMode = "未知";
		}
		String physicsCard = jsonObject.getString("physicsCard");
		String vinCode = jsonObject.getString("vinCode");
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("启动方式:").append(startMode).append(", ");
		stringBuilder.append("物理卡号:").append(physicsCard).append(", ");
		stringBuilder.append("vinCode:").append(vinCode).append(", ");
		return stringBuilder.toString();
	}
}
