package com.wgb.system.service.controller;

import com.alibaba.fastjson.JSON;
import com.data.model.restful.response.InternalResponse;
import com.tool.data.external.domain.model.WechatGetUserInfo;
import com.tool.data.external.domain.request.ReceiveWechatPayResultRequest;
import com.tool.data.external.domain.response.WechatPayUnifiedorderResponse;
import com.wgb.system.service.domain.request.SendWechatBeforehandPayRequest;
import com.wgb.system.service.service.WechatService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/wechat")
public class WechatController {

    private final WechatService wechatService;

    public WechatController(WechatService wechatService) {
        this.wechatService = wechatService;
    }


    @ApiOperation(value = "getUserInfo", notes = "获取微信用户信息")
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public InternalResponse getUserInfo(@RequestParam String code) {
        log.info("Start to execute /wechat/getUserInfo  RequestParam ={}", code);
         WechatGetUserInfo wechatGetUserInfo= wechatService.getUserInfo(code);
        log.info("completed execute /wechat/getuserinfo responese={}",wechatGetUserInfo);
        return InternalResponse.success().withBody(wechatGetUserInfo);
    }


    @ApiOperation(value = "getAuthorizationTicket", notes = "获取微信ticket信息")
    @RequestMapping(value = "/getAuthorizationTicket", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public InternalResponse getAuthorizationTicket() {
        log.info("Start to execute /wechat/getAuthorizationTicket ");
        String ticket= wechatService.getTicket();
        log.info("Completed execute /wechat/getUserInfo responese={}", JSON.toJSON(ticket));
        return InternalResponse.success().withBody(ticket);
    }


    @ApiOperation(value = "sendWechatBeforehandPay", notes = "微信预支付")
    @RequestMapping(value = "/sendWechatBeforehandPay", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public InternalResponse  sendWechatBeforehandPay(@Valid @RequestBody SendWechatBeforehandPayRequest request)throws Exception {
        log.info("Start to execute /wechat/sendWechatBeforehandPay param={}",request);
        WechatPayUnifiedorderResponse response = wechatService.sendWechatBeforehandPay(request);
        log.info("Completed execute /wechat/sendWechatBeforehandPay responese={}",JSON.toJSON(response));
        return InternalResponse.success().withBody(response);
    }


    @ApiOperation(value = "responseToWechatPay", notes = "处理微信预支付结果")
    @RequestMapping(value = "/responseToWechatPay", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public InternalResponse  responseToWechatPay(ReceiveWechatPayResultRequest receiveWechatPayResultRequest) {
        log.info("Start to execute /wechat/responseToWechatPay");
        wechatService.responseToWechatPay(receiveWechatPayResultRequest);
        log.info("Completed execute /wechat/sendWechatBeforehandPay responese={}");
        return InternalResponse.success();
    }


    @ApiOperation(value = "sendMessage", notes = "发送微信消息")
    @RequestMapping(value = "/sendMessage", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public InternalResponse  sendMessage() {
        log.info("Start to execute /wechat/sendMessage");
        wechatService.sendMessage();
        log.info("Completed execute /wechat/sendMessage responese={}");
        return InternalResponse.success();
    }


    //todo 退款

}
