package com.wteam.controller.payment;


import com.egzosn.pay.common.bean.RefundOrder;
import com.egzosn.pay.common.bean.RefundResult;
import com.wteam.framework.common.exception.ServiceException;
import com.wteam.framework.common.vo.ResultMessage;
import com.wteam.framework.modules.book.service.OrderInfoService;
import com.wteam.framework.modules.pay.entity.enums.PaymentClientEnum;
import com.wteam.framework.modules.pay.entity.enums.PaymentMethodEnum;
import com.wteam.framework.modules.pay.serviceimpl.CashierSupport;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 买家端,收银台接口
 *
 * @author Chopper
 * @since 2020-12-18 16:59
 */
@Slf4j
@RestController
@Tag(name = "支付相关接口管理")
@RequestMapping("/v1/payment")
public class CashierController {

    @Autowired
    private CashierSupport cashierSupport;


    @Autowired
    private OrderInfoService orderInfoService;


//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "client", value = "客户端类型", paramType = "path", allowableValues = "PC,H5,WECHAT_MP,APP")
//    })
//    @GetMapping(value = "/tradeDetail")
//    @ApiOperation(value = "获取支付详情")
//    public ResultMessage paymentParams(@Validated PayParam payParam) {
//        CashierParam cashierParam = cashierSupport.cashierParam(payParam);
//        return ResultUtil.data(cashierParam);
//    }

    //TODO：退款接口待办 实现的有点问题
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paymentMethod", value = "支付方式", paramType = "path", allowableValues = "WECHAT"),
    })
    @ApiOperation(value = "退款")
    @RequestMapping(value = "/refund/{paymentMethod}/{sn}", method = {RequestMethod.POST})
    public RefundResult refund(HttpServletRequest request, @PathVariable String paymentMethod, @PathVariable String sn) {
        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.valueOf(paymentMethod);
        try {
            return cashierSupport.refund(paymentMethodEnum, request, sn);
        } catch (ServiceException se) {
            log.info("支付异常", se);
            throw se;
        } catch (Exception e) {
            log.error("收银台支付错误", e);
        }
        return null;
    }


    @ApiImplicitParams({@ApiImplicitParam(name = "paymentMethod", value = "支付方式", paramType = "path", allowableValues = "WECHAT,ALIPAY"),})
    @GetMapping(value = "/pay/{paymentMethod}/{paymentClient}")
    @ApiOperation(value = "支付")
    public ResultMessage payment(HttpServletRequest request, HttpServletResponse response, @PathVariable String paymentMethod, @PathVariable String paymentClient, @Validated String sn) {
        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.valueOf(paymentMethod);
        PaymentClientEnum paymentClientEnum = PaymentClientEnum.valueOf(paymentClient);

        try {
            return cashierSupport.payment(paymentMethodEnum, paymentClientEnum, request, response, sn);
        } catch (ServiceException se) {
            log.info("支付异常", se);
            throw se;
        } catch (Exception e) {
            log.error("收银台支付错误", e);
        }
        return null;
    }

    @ApiOperation(value = "支付回调")
    @RequestMapping(value = "/callback/{paymentMethod}", method = {RequestMethod.GET, RequestMethod.POST})
    public String callback(HttpServletRequest request, @PathVariable String paymentMethod) {

        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.valueOf(paymentMethod);


        String callback = cashierSupport.callback(paymentMethodEnum, request);

        log.info("回调响应消息" + callback);

        return callback;
    }


//    @ApiOperation(value = "支付异步通知")
//    @RequestMapping(value = "/notify/{paymentMethod}", method = {RequestMethod.GET, RequestMethod.POST})
//    public void notify(HttpServletRequest request, @PathVariable String paymentMethod) {
//
//        PaymentMethodEnum paymentMethodEnum = PaymentMethodEnum.valueOf(paymentMethod);
//
//        cashierSupport.notify(paymentMethodEnum, request);
//
//    }

//    @ApiOperation(value = "查询支付结果")
//    @GetMapping(value = "/result")
//    public ResultMessage<Boolean> paymentResult(PayParam payParam) {
//        return ResultUtil.data(cashierSupport.paymentResult(payParam));
//    }

//    @ApiOperation(value = "查询支付结果")
//    @GetMapping(value = "/result")
//    public ResultMessage<Boolean> paymentResult(PayParam payParam) {
//        return ResultUtil.data(cashierSupport.paymentResult(payParam));
//    }


}
