package com.kym.miniapp.controller;

import com.alibaba.fastjson2.JSONObject;
import com.kym.common.R;
import com.kym.common.annotation.ApiLog;
import com.kym.entity.admin.vo.StationVo;
import com.kym.entity.enplus.response.EnResponse;
import com.kym.entity.miniapp.ChargeOrder;
import com.kym.service.admin.StationService;
import com.kym.service.enplus.EnNotifyService;
import com.kym.service.enplus.EnPlusService;
import com.kym.service.miniapp.ChargeOrderService;
import com.kym.service.miniapp.ChargeService;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 充电
 *
 * @author skyline
 * @since 2023-06-27
 */
@RestController
@RequestMapping("/charge")
public class ChargerController {

    private final StationService stationService;

    private final EnPlusService enPlusService;

    private final ChargeService chargeService;

    private final ChargeOrderService chargeOrderService;

    private final EnNotifyService enNotifyService;

    public ChargerController(StationService stationService, EnPlusService enPlusService, ChargeService chargeService, ChargeOrderService chargeOrderService, EnNotifyService enNotifyService) {
        this.stationService = stationService;
        this.enPlusService = enPlusService;
        this.chargeService = chargeService;
        this.chargeOrderService = chargeOrderService;
        this.enNotifyService = enNotifyService;
    }

    /**
     * 充电站列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @SneakyThrows
    @GetMapping("/listStation")
    R listChargeStation(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        // 请求en+接口
        List<StationVo> response = stationService.queryStationInfo(pageNum, pageSize);
        return R.success(response);
    }

    /**
     * 充电站状态
     *
     * @param ids
     * @return
     */
    @GetMapping("/stationStatus")
    R stationStatus(@RequestParam("stationIds") String ids) {
        // 请求en+接口
        var response = stationService.stationStatus(ids.split(","));
        return R.success(response);
    }

    @GetMapping("/token")
    R<String> queryToken() {
        return R.success(enPlusService.queryToken());
    }

    @ApiLog("启动充电")
    @GetMapping("/startCharge/{connectorId}")
    R startCharge(@PathVariable("connectorId") String connectorId) {
        return R.success(chargeService.queryStartCharge(connectorId));
    }

    @ApiLog("停止充电")
    @GetMapping("/stopCharge/{connectorId}")
    R stopCharge(@PathVariable("connectorId") String connectorId) {
        chargeService.queryStopCharge(connectorId);
        return R.success();
    }

    /**
     * 查询当前用户充电状态
     *
     * @return
     */
    @GetMapping("/chargeStatus")
    R<ChargeOrder> chargeStatus() {
        return R.success(chargeService.queryEquipChargeStatus());
    }

    /**
     * 查询充电策略
     *
     * @param connectorId
     * @return
     */
    @GetMapping("/businessPolicy/{connectorId}")
    R businessPolicy(@PathVariable("connectorId") String connectorId) {
        return R.success(chargeService.queryEquipBusinessPolicy(connectorId));
    }

    /**
     * 充电订单详情
     *
     * @param startChargeSeq
     * @return
     */
    @GetMapping("/orderDetail/{startChargeSeq}")
    public R orderDetail(@PathVariable("startChargeSeq") String startChargeSeq) {
        return R.success(chargeOrderService.orderDetailForApp(startChargeSeq));
    }

    //====================================================以上是请求EN+接口==============================================================

    // 【我们请求en+时，数据都要加密验签；en+推送给我们的数据，需要验签解密，响应不需要加密。】

    //====================================================以下是EN+推送接口==============================================================

    /**
     * EN+场站设备状态变化推送
     *
     * @param json
     * @return 0:接收 1：丢弃/忽略，不需要重试
     */
    @PostMapping("/notification_stationStatus")
    EnResponse notificationStationStatus(@RequestBody JSONObject json) {
        return new EnResponse(enNotifyService.handleNotificationStationStatus(json));
    }


    /**
     * EN+推送启动充电结果
     *
     * @param json
     * @return StartChargeSeq SuccStat 0:成功 1：失败  FailReason 0:无 1：接收失败
     */
    @ApiLog("EN+推送启动充电结果")
    @PostMapping("/notification_start_charge_result")
    EnResponse notificationStartChargeResult(@RequestBody JSONObject json) {
        return new EnResponse(enNotifyService.handleNotificationStartChargeResult(json));
    }


    /**
     * EN+推送充电状态
     *
     * @param json
     * @return
     */
    @PostMapping("/notification_equip_charge_status")
    EnResponse notificationEquipChargeStatus(@RequestBody JSONObject json) {
        return new EnResponse(enNotifyService.handleNotificationEquipChargeStatus(json));
    }

    // TODO: 2023-08-21 推送启动充电结果


    /**
     * EN+推送停止充电结果
     *
     * @param json
     * @return
     */
    @ApiLog("推送停止充电结果")
    @PostMapping("/notification_stop_charge_result")
    EnResponse notificationStopChargeResult(@RequestBody JSONObject json) {
        return new EnResponse(enNotifyService.handleNotificationStopChargeResult(json));
    }

    /**
     * EN+推送充电订单信息
     *
     * @param json
     * @return
     */
    @ApiLog("推送充电订单信息")
    @PostMapping("/notification_charge_order_info")
    EnResponse notificationChargeOrderInfo(@RequestBody JSONObject json) {
        return new EnResponse(enNotifyService.handleNotificationChargeOrderInfo(json));
    }


    //====================================================以下是手动同步数据接口==============================================================

    /**
     * 拉取EN+充电站信息数据并更新本地服务器数据
     *
     * @return
     */
    @GetMapping("/pullEnStations")
    R pullEnStationInfos() {
        stationService.pullEnStationInfos();
        return R.success();
    }


}
