package com.cyl.wechat.coupon;

import cn.felord.payment.wechat.v3.WechatApiProvider;
import cn.felord.payment.wechat.v3.WechatMarketingBusiFavorApi;
import cn.felord.payment.wechat.v3.WechatResponseEntity;
import cn.felord.payment.wechat.v3.model.busifavor.BusiFavorCreateParams;
import cn.felord.payment.wechat.v3.model.ResponseSignVerifyParams;
import cn.felord.payment.wechat.v3.model.busifavor.*;
import com.cyl.manager.marketing.service.IWechatMemberCouponService;
import com.cyl.wechat.WechatPayData;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/no-auth/wechat/coupon")
@Slf4j
public class WechatCouponController {

    @Autowired
    WechatApiProvider wechatApiProvider;

    @Autowired
    private IWechatMemberCouponService wechatMemberCouponService;

    @PostMapping("/createStock")
    public ResponseEntity<WechatResponseEntity> createStock(@RequestBody BusiFavorCreateParams params){
        WechatMarketingBusiFavorApi wechatMarketingBusiFavorApi = wechatApiProvider.busiFavorApi("miniapp");

        WechatResponseEntity<ObjectNode> wechatResponseEntity = wechatMarketingBusiFavorApi.createStock(params);

        return ResponseEntity.ok(wechatResponseEntity);
    }

    @GetMapping("/queryStock/{stockId}")
    public ResponseEntity<WechatResponseEntity> queryStockDetail(@PathVariable String stockId){
        WechatMarketingBusiFavorApi wechatMarketingBusiFavorApi = wechatApiProvider.busiFavorApi("miniapp");

        WechatResponseEntity<ObjectNode> wechatResponseEntity = wechatMarketingBusiFavorApi.queryStockDetail(stockId);

        return ResponseEntity.ok(wechatResponseEntity);
    }

    @PostMapping("/updateStock")
    public ResponseEntity<WechatResponseEntity> updateStock(@RequestBody BusiFavorUpdateParams params){
        WechatMarketingBusiFavorApi wechatMarketingBusiFavorApi = wechatApiProvider.busiFavorApi("miniapp");

        WechatResponseEntity<ObjectNode> wechatResponseEntity = wechatMarketingBusiFavorApi.updateStock(params);

        return ResponseEntity.ok(wechatResponseEntity);
    }

    @PostMapping("/queryUserCoupon")
    public ResponseEntity<WechatResponseEntity> queryUserCoupon(@RequestBody UserBusiCouponQueryParams params){
        WechatMarketingBusiFavorApi wechatMarketingBusiFavorApi = wechatApiProvider.busiFavorApi("miniapp");

        WechatResponseEntity<ObjectNode> wechatResponseEntity = wechatMarketingBusiFavorApi.queryUserCoupon(params);

        return ResponseEntity.ok(wechatResponseEntity);
    }

    @PostMapping("/queryUserStocks")
    public ResponseEntity<WechatResponseEntity> queryUserCoupon(@RequestBody UserBusiFavorQueryParams params){
        WechatMarketingBusiFavorApi wechatMarketingBusiFavorApi = wechatApiProvider.busiFavorApi("miniapp");

        WechatResponseEntity<ObjectNode> wechatResponseEntity = wechatMarketingBusiFavorApi.queryUserStocks(params);

        return ResponseEntity.ok(wechatResponseEntity);
    }

    @PostMapping("/use")
    public ResponseEntity<WechatResponseEntity> use(@RequestBody BusiFavorUseParams params){
        WechatMarketingBusiFavorApi wechatMarketingBusiFavorApi = wechatApiProvider.busiFavorApi("miniapp");

        WechatResponseEntity<ObjectNode> wechatResponseEntity = wechatMarketingBusiFavorApi.use(params);

        return ResponseEntity.ok(wechatResponseEntity);
    }

    @PostMapping("/refund")
    public ResponseEntity<WechatResponseEntity> refund(@RequestBody BusiFavorRefundParams params){
        WechatMarketingBusiFavorApi wechatMarketingBusiFavorApi = wechatApiProvider.busiFavorApi("miniapp");

        WechatResponseEntity<ObjectNode> wechatResponseEntity = wechatMarketingBusiFavorApi.refund(params);

        return ResponseEntity.ok(wechatResponseEntity);
    }

    @PostMapping("/deactivate")
    public ResponseEntity<WechatResponseEntity> deactivate(@RequestBody BusiFavorDeactivateParams params){
        WechatMarketingBusiFavorApi wechatMarketingBusiFavorApi = wechatApiProvider.busiFavorApi("miniapp");

        WechatResponseEntity<ObjectNode> wechatResponseEntity = wechatMarketingBusiFavorApi.deactivate(params);

        return ResponseEntity.ok(wechatResponseEntity);
    }

    @PostMapping("/setCallbacks")
    public ResponseEntity<WechatResponseEntity> setCallbacks(@RequestBody BusiFavorCallbackSettingParams params){
        WechatMarketingBusiFavorApi wechatMarketingBusiFavorApi = wechatApiProvider.busiFavorApi("miniapp");

        params.setMchid(WechatPayData.merchantId);

        WechatResponseEntity<ObjectNode> wechatResponseEntity = wechatMarketingBusiFavorApi.setCallbacks(params);

        return ResponseEntity.ok(wechatResponseEntity);
    }

    @GetMapping("/getCallbacks")
    public ResponseEntity<WechatResponseEntity> getCallbacks(){
        WechatMarketingBusiFavorApi wechatMarketingBusiFavorApi = wechatApiProvider.busiFavorApi("miniapp");

        WechatResponseEntity<ObjectNode> wechatResponseEntity = wechatMarketingBusiFavorApi.getCallbacks(WechatPayData.merchantId);

        return ResponseEntity.ok(wechatResponseEntity);
    }

    @PostMapping("/callback")
    public Map<String, String> callback(@RequestHeader("Wechatpay-Serial") String wechatpaySerial,
                                                        @RequestHeader("Wechatpay-Signature") String wechatpaySignature,
                                                        @RequestHeader("Wechatpay-Timestamp") String wechatpayTimestamp,
                                                        @RequestHeader("Wechatpay-Nonce") String wechatpayNonce,
                                                        HttpServletRequest request) throws IOException {
        String body = request.getReader().lines().collect(Collectors.joining());
        log.info("【微信商家券回调】接收到数据：" + body);

        ResponseSignVerifyParams params = new ResponseSignVerifyParams();
        params.setWechatpayNonce(wechatpayNonce);
        params.setBody(body);
        params.setWechatpaySerial(wechatpaySerial);
        params.setWechatpayTimestamp(wechatpayTimestamp);
        params.setWechatpaySignature(wechatpaySignature);
        log.info("【微信商家券回调】接收到组装的数据：" + params);

        return wechatApiProvider.callback("miniapp").busiFavorReceiveCallback(params, data -> {
            log.info("【微信商家券回调】接收到回调数据：" + data);

            wechatMemberCouponService.send(data);
        });
    }

}
