package com.jsowell.web.controller.pile;

import com.alibaba.fastjson2.JSON;
import com.google.common.collect.ImmutableMap;
import com.jsowell.common.core.controller.BaseController;
import com.jsowell.common.core.domain.AjaxResult;
import com.jsowell.common.enums.ykc.ReturnCodeEnum;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.util.StringUtils;
import com.jsowell.common.util.id.IdUtils;
import com.jsowell.pile.domain.ykcCommond.RemoteControlGroundLockCommand;
import com.jsowell.pile.dto.GenerateOrderDTO;
import com.jsowell.pile.dto.QueryPileDTO;
import com.jsowell.pile.dto.UpdateFirmwareDTO;
import com.jsowell.pile.service.PileRemoteService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * 远程控制controller
 */
@RestController
@RequestMapping("/pile/remote")
public class PileRemoteController extends BaseController {

	@Autowired
	private PileRemoteService pileRemoteService;

	/**
	 * 获取实时上传数据
	 * http://localhost:8080/pile/remote/getRealTimeMonitorData
	 * @return
	 */
	@PostMapping("/getRealTimeMonitorData")
	public AjaxResult getRealTimeMonitorData(@RequestBody QueryPileDTO queryPileDTO) {
		pileRemoteService.getRealTimeMonitorData(queryPileDTO.getPileSn(), queryPileDTO.getConnectorCode());
		return AjaxResult.success();
	}

	/**
	 * 远程重启
	 * http://localhost:8080/pile/remote/reboot
	 * @return
	 */
	@PostMapping("/reboot")
	public AjaxResult reboot(@RequestBody QueryPileDTO queryPileDTO) {
		pileRemoteService.reboot(queryPileDTO.getPileSn());
		return AjaxResult.success();
	}

	/**
	 * 后管-远程启动充电
	 * http://localhost:8080/pile/remote/remoteStartChargingForWeb
	 */
	@PostMapping("/remoteStartChargingForWeb")
	public AjaxResult remoteStartCharging(@RequestBody GenerateOrderDTO dto) {

		// 生成订单并远程启动充电
		// dto.setStartMode(Constants.ZERO);
		// String orderCode = null;
		String transactionCode = null;
		try {
			transactionCode = IdUtils.generateTransactionCode(dto.getPileSn(), dto.getConnectorCode());
			pileRemoteService.remoteStartCharging(dto.getPileSn(), dto.getConnectorCode(), transactionCode, new BigDecimal("500"));
		// 	// 生成订单
		// 	orderCode = orderService.generateOrder(dto);
		// 	// 支付订单
		// 	PayOrderDTO payOrderDTO = new PayOrderDTO();
		// 	payOrderDTO.setPayMode(OrderPayModeEnum.PAYMENT_OF_WHITELIST.getValue());
		// 	payOrderDTO.setOrderCode(orderCode);
		// 	payOrderDTO.setStartMode(Constants.ZERO);
		//
		// 	orderService.payOrder(payOrderDTO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return AjaxResult.success(ImmutableMap.of("orderCode", transactionCode));
	}

	/**
	 * 远程停止充电
	 * http://localhost:8080/pile/remote/remoteStopCharging
	 * @param dto
	 * @return
	 */
	@PostMapping("/remoteStopCharging")
	public AjaxResult remoteStopCharging(@RequestBody QueryPileDTO dto) {
		pileRemoteService.remoteStopCharging(dto.getPileSn(), dto.getConnectorCode());
		return AjaxResult.success();
	}

	/**
	 * 远程下发二维码
	 * http://localhost:8080/pile/remote/issueQRCode
	 */
	@PostMapping("/issueQRCode")
	public AjaxResult issueQRCode(@RequestBody QueryPileDTO queryPileDTO) {
		pileRemoteService.issueQRCode(queryPileDTO.getPileSn());
		return AjaxResult.success();
	}

	/**
	 * 对时
	 * http://localhost:8080/pile/remote/proofreadTime
	 */
	@PostMapping("/proofreadTime")
	public AjaxResult proofreadTime(@RequestBody QueryPileDTO queryPileDTO) {
		pileRemoteService.proofreadTime(queryPileDTO.getPileSn());
		return AjaxResult.success();
	}

	/**
	 * 远程升级
	 * http://localhost:8080/pile/remote/updateFirmware
	 * @return
	 */
	@PostMapping("/updateFirmware")
	public AjaxResult updateFirmware(@RequestBody UpdateFirmwareDTO dto) {
		AjaxResult result;
		try {
			if (CollectionUtils.isEmpty(dto.getPileSns())) {
				throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR);
			}
			if (StringUtils.isBlank(dto.getFirmwareId())) {
				throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR);
			}
			pileRemoteService.updateFirmware(dto);
			result = AjaxResult.success();
		} catch (BusinessException e) {
			result = AjaxResult.error(e.getMessage());
		}
		return result;
	}

	/**
	 * 遥控地锁
	 * http://localhost:8080/pile/remote/remoteControlGroundLock
	 */
	@PostMapping("remoteControlGroundLock")
	public AjaxResult remoteControlGroundLock(@RequestBody RemoteControlGroundLockCommand command) {
		logger.info("遥控地锁接口param:{}", JSON.toJSONString(command));
		AjaxResult result;
		try {
			pileRemoteService.remoteControlGroundLock(command);
			result = AjaxResult.success();
		} catch (BusinessException e) {
			logger.error("遥控地锁接口error", e);
			result = AjaxResult.error(e.getMessage());
		} catch (Exception e) {
			logger.error("遥控地锁接口error", e);
			result = AjaxResult.error(e.getMessage());
		}
		return result;
	}

}
