package com.gree.mall.manager.controller.common;

import com.alibaba.excel.util.StringUtils;
import com.gree.mall.manager.annotation.ApiNotAuth;
import com.gree.mall.manager.bean.PayDetail;
import com.gree.mall.manager.bean.common.WechatOpenBean;
import com.gree.mall.manager.bean.common.WxJsApiSignBean;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.logic.common.WechatLogic;
import com.gree.mall.manager.plus.entity.AdminCompanyWechat;
import com.gree.mall.manager.plus.service.AdminCompanyWechatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.math.BigDecimal;

@RequiredArgsConstructor
@Slf4j
@RestController
@Api(value = "微信api", tags ={"微信api"} )
@RequestMapping("wechat")
public class WechatController {

    private final WechatLogic wechatLogic;
    private final CommonLogic commonLogic;
    private final AdminCompanyWechatService adminCompanyWechatService;

    @ApiNotAuth
    @PostMapping("/auth")
    @ApiOperation(value = "授权")
    public ResponseHelper<String> getQrcode(
            @RequestParam String code,
            @RequestParam(required = false) String phoneCode,
            @RequestParam String companyWechatId
    ) {
        WechatOpenBean wechatOpenBean = wechatLogic.authToken(code, companyWechatId);
        if(StringUtils.isNotBlank(phoneCode)) {
            String mobile = wechatLogic.authMobile(phoneCode, companyWechatId);
            wechatOpenBean.setMobile(mobile);
        }
        return ResponseHelper.success(wechatOpenBean);
    }

    @PostMapping("/qrcode/get")
    @ApiOperation(value = "获取小程序二维码")
    public ResponseHelper<String> getQrcode(
            @ApiParam("二维码类型") @RequestParam String qrcodeType,
            @RequestParam String objId,
            @RequestParam String userId,
            @RequestParam String companyWechatId
    ) {
        String qrcode = wechatLogic.getQrcode(qrcodeType, objId, userId, companyWechatId);
        return ResponseHelper.success(qrcode);
    }

    @ApiNotAuth
    @PostMapping("/zdhf/get")
    @ApiOperation("获取自动回复的内容")
    public ResponseHelper<String> getZdhf(@RequestParam String appid) throws WxErrorException {
        AdminCompanyWechat adminCompanyWechat = adminCompanyWechatService.lambdaQuery()
                .eq(AdminCompanyWechat::getPubAppId, appid)
                .last("limit 1").one();
        if(adminCompanyWechat != null) {
            String zdhf = wechatLogic.getZdhf(adminCompanyWechat.getCompanyWechatId());
            return ResponseHelper.success(zdhf);
        }
        return ResponseHelper.success("");
    }


    @PostMapping("/payment/h5")
    @ApiOperation(value = "微信h5支付")
    public ResponseHelper<PayDetail> paymentH5(
            @ApiParam("订单号") @RequestParam String orderId,
            @RequestParam BigDecimal payAmount,
            @RequestParam String openId,
            @RequestParam String profitSharingFlag,
            @RequestParam String ip,
            @RequestParam String companyWechatId
    ) throws Exception {
        PayDetail payDetail = wechatLogic.paymentH5(orderId, payAmount, openId, profitSharingFlag, ip, companyWechatId);
        return ResponseHelper.success(payDetail);
    }

    @PostMapping("/payment")
    @ApiOperation(value = "微信小程序支付")
    public ResponseHelper<PayDetail> payment(
            @ApiParam("订单号") @RequestParam String orderId,
            @RequestParam BigDecimal payAmount,
            @RequestParam String openId,
            @RequestParam String profitSharingFlag,
            @RequestParam String ip,
            @RequestParam String companyWechatId,
            @ApiParam(value = "是否小程序支付",required = false) @RequestParam(required = false) Boolean miniPay
    ) throws Exception {
        PayDetail payDetail = wechatLogic.paymentJSAPI(orderId, payAmount, openId, profitSharingFlag, ip, companyWechatId,miniPay);
        return ResponseHelper.success(payDetail);
    }


    @PostMapping("/payment/fkm")
    @ApiOperation(value = "付款码支付")
    public ResponseHelper<String> paymentFkm(
            @ApiParam("订单号") @RequestParam String orderId,
            @RequestParam BigDecimal payAmount,
            @RequestParam String profitSharingFlag,
            @RequestParam String ip,
            @RequestParam String companyWechatId,
            @RequestParam String authCode,
            @RequestParam String source
    ) throws Exception {
        String transcationId = wechatLogic.paymentFkm(orderId, payAmount, profitSharingFlag, ip, companyWechatId, authCode,source);
        return ResponseHelper.success(transcationId);
    }

    @PostMapping("/payment/fkmWebsit")
    @ApiOperation(value = "付款码支付")
    public ResponseHelper<String> paymentfkmWebsit(
            @ApiParam("订单号") @RequestParam String orderId,
            @RequestParam BigDecimal payAmount,
            @RequestParam String profitSharingFlag,
            @RequestParam String ip,
            @RequestParam String companyWechatConfigId,
            @RequestParam String authCode,
            @RequestParam String source
    ) throws Exception {
        String transcationId = wechatLogic.paymentfkmWebsit(orderId, payAmount, profitSharingFlag, ip, companyWechatConfigId, authCode,source);
        return ResponseHelper.success(transcationId);
    }

//    @ApiNotAuth
//    @PostMapping("/pub/auth/url")
//    @ApiOperation("公众号授权绑定小程序url")
//    public ResponseHelper<String> pubAuthUrl(
//            @RequestParam String code,
//            @RequestParam String pubAppId
//    ) throws WxErrorException, IOException {
//        String urlScheme = commonLogic.getPubOpenIdUrl(code, pubAppId);
//        return ResponseHelper.success(urlScheme);
//    }

    @ApiNotAuth
    @PostMapping("/pub/auth")
    @ApiOperation("公众号授权")
    public ResponseHelper<String> pubAuth(
            @RequestParam String code,
            @RequestParam String pubAppId
    ) throws WxErrorException, IOException {
        String openId = commonLogic.getPubOpenId(code, pubAppId);
        return ResponseHelper.success(openId);
    }



    @PostMapping("/pub/jsapi/sign")
    @ApiOperation("公众号jsapsign")
    public ResponseHelper<WxJsApiSignBean> pubJsapiSign(
            @RequestParam String companyWechatId,
            @RequestParam String url
    ) throws WxErrorException, IOException {
        WxJsApiSignBean jsapiSign = wechatLogic.getJsapiSign(companyWechatId, url);
        return ResponseHelper.success(jsapiSign);
    }

    @ApiNotAuth
    @PostMapping("/get/mp/qrcode")
    @ApiOperation("生成带参数的二维码")
    public ResponseHelper<WxMpQrCodeTicket> getMpQrcode(
            @RequestParam String userId,
            @RequestParam String companyWechatId
    ) throws Exception {
        WxMpQrCodeTicket ticket = wechatLogic.getMpQrcode(userId, companyWechatId);
        return ResponseHelper.success(ticket);
    }

    @ApiNotAuth
    @PostMapping("/authCode")
    @ApiOperation(value = "授权小程序id")
    public ResponseHelper<WechatOpenBean> authCode(
            @RequestParam String code,
            @ApiParam(required = false) @RequestParam(required = false) String phoneCode,
            @RequestParam String appid
    ) {
        WechatOpenBean wechatOpenBean = wechatLogic.authCode(code, appid);
        if(StringUtils.isNotBlank(phoneCode)) {
            String mobile = wechatLogic.authMobile(phoneCode, appid);
            wechatOpenBean.setMobile(mobile);
        }
        return ResponseHelper.success(wechatOpenBean);
    }



//    @PostMapping("/payment/zfire")
//    @ApiOperation(value = "众炬续费")
//    public ResponseHelper<Boolean> zfire(
//    ) throws Exception {
//        wechatLogic.zfirePayment();
//        return ResponseHelper.success();
//    }



    @ApiNotAuth
    @PostMapping("/deliver")
    //@ApiOperation(value = "发货")
    public ResponseHelper<Boolean> paymentDeliver(
            @RequestParam String companyWechatId,
            @RequestParam String transactionId,
            @RequestParam String openId
    ) throws Exception {
        wechatLogic.deliver(companyWechatId, transactionId,null,openId);
        return ResponseHelper.success(true);
    }

}
