package com.izouma.nineth.web;

import com.alibaba.fastjson.JSON;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.huifu.adapay.core.exception.BaseAdaPayException;
import com.izouma.nineth.service.AssetService;
import com.izouma.nineth.service.GiftOrderService;
import com.izouma.nineth.service.OrderService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.commons.codec.EncoderException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.regex.Pattern;

@Controller
@RequestMapping("/payOrder")
@Slf4j
@AllArgsConstructor
public class OrderPayController {
    private final OrderService     orderService;
    private final AssetService     assetService;
    private final WxMpService      wxMpService;
    private final GiftOrderService giftOrderService;

    @RequestMapping(value = "/alipay_h5", method = RequestMethod.GET)
    @ResponseBody
    public String payOrderAlipayH5(Long id, Model model) throws BaseAdaPayException {
        return (String) orderService.payAdapay(id, "alipay_wap", null);
    }

    @RequestMapping(value = "/alipay_wx", method = RequestMethod.GET)
    public String payOrderAlipayWx(Long id, Model model) throws BaseAdaPayException {
        String payUrl = (String) orderService.payAdapay(id, "alipay_wap", null);
        model.addAttribute("payUrl", payUrl);
        model.addAttribute("orderId", id);
        return "AlipayHtml";
    }

    @RequestMapping(value = "/alipay_qr", method = RequestMethod.GET)
    @ResponseBody
    public String payOrderAlipayQR(Long id, Model model) throws BaseAdaPayException {
        return (String) orderService.payAdapay(id, "alipay_qr", null);
    }

    @RequestMapping(value = "/alipay_app", method = RequestMethod.GET)
    @ResponseBody
    public String payOrderAlipayApp(Long id, Model model) throws BaseAdaPayException {
        return (String) orderService.payAdapay(id, "alipay", null);
    }

    @RequestMapping(value = "/weixin_h5")
    public String payOrderWeixinH5(Long id, Model model, @RequestHeader(value = "User-Agent") String userAgent) throws EncoderException, WxPayException {
        detectUA(userAgent, model);
        model.addAttribute("payUrl", orderService.payOrderWeixin(id, WxPayConstants.TradeType.MWEB, null));
        return "WeixinPayHtml";
    }

    @RequestMapping(value = "/weixin")
    @ResponseBody
    public Object payOrderWeixin(@RequestParam Long id) throws BaseAdaPayException {
        return orderService.payAdapay(id, "wx_lite", null);
    }

    @RequestMapping(value = "/weixin_pc")
    public String payOrderWeixinPC(@RequestParam Long id, @RequestParam String code, Model model) throws WxPayException, EncoderException, WxErrorException {
        WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code);
        WxMpUser user = wxMpService.oauth2getUserInfo(accessToken, null);
        WxPayMpOrderResult payParams = (WxPayMpOrderResult) orderService.payOrderWeixin(id, WxPayConstants.TradeType.JSAPI, user.getOpenId());
        model.addAttribute("payParams", JSON.toJSONString(payParams));
        return "PayOrderPC";
    }

    @RequestMapping(value = "/gift/alipay", method = RequestMethod.GET)
    public String payGiftOrderAlipay(Long id, Model model, @RequestHeader(value = "User-Agent") String userAgent) {
        detectUA(userAgent, model);
        giftOrderService.payOrderAlipay(id, model);
        return "AlipayHtml";
    }

    @RequestMapping(value = "/gift/weixin_h5")
    public String payGiftOrderWeixinH5(Long id, Model model, @RequestHeader(value = "User-Agent") String userAgent) throws EncoderException, WxPayException {
        detectUA(userAgent, model);
        model.addAttribute("payUrl", giftOrderService.payOrderWeixin(id, WxPayConstants.TradeType.MWEB, null));
        return "WeixinPayHtml";
    }

    @RequestMapping(value = "/gift/weixin_pc")
    public String payGiftOrderWeixinPC(@RequestParam Long id, @RequestParam String code, Model model) throws WxPayException, EncoderException, WxErrorException {
        WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code);
        WxMpUser user = wxMpService.oauth2getUserInfo(accessToken, null);
        WxPayMpOrderResult payParams = (WxPayMpOrderResult) giftOrderService.payOrderWeixin(id, WxPayConstants.TradeType.JSAPI, user.getOpenId());
        model.addAttribute("payParams", JSON.toJSONString(payParams));
        return "PayOrderPC";
    }

    @RequestMapping(value = "/gift/weixin")
    @ResponseBody
    public Object payGiftOrderWeixin(@RequestParam Long id, @RequestParam String openId) throws WxPayException, EncoderException {
        return giftOrderService.payOrderWeixin(id, WxPayConstants.TradeType.JSAPI, openId);
    }

    public void detectUA(String ua, Model model) {
        boolean weixin = Pattern.matches(".*(micromessenger).*", ua.toLowerCase());
        boolean ios = Pattern.matches(".*(ipad|iphone).*", ua.toLowerCase());
        boolean android = Pattern.matches(".*(android).*", ua.toLowerCase());
        model.addAttribute("weixin", weixin);
        model.addAttribute("ios", ios);
        model.addAttribute("android", android);
    }
}
