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.domain.Order;
import com.izouma.nineth.enums.OrderStatus;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.OrderRepo;
import com.izouma.nineth.service.*;
import com.izouma.nineth.utils.SecurityUtils;
import io.swagger.annotations.ApiOperation;
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.math.BigDecimal;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/payOrder/v2")
@Slf4j
@AllArgsConstructor
public class OrderPayControllerV2 {
    private final OrderRepo       orderRepo;
    private final SandPayService  sandPayService;
    private final OrderPayService orderPayService;

    @RequestMapping(value = "/alipay", method = RequestMethod.GET)
    @ResponseBody
    public String payOrderAlipayH5(Long id) {
        return orderPayService.payOrder(id);
    }

    @RequestMapping(value = "/alipay_wx", method = RequestMethod.GET)
    public String payOrderAlipayWx(Long id, Model model) {
        Order order = orderRepo.findById(id).orElseThrow(new BusinessException("订单不存在"));
        if (order.getStatus() != OrderStatus.NOT_PAID) {
            return "redirect:/9th/store";
        }
        String payUrl = orderPayService.payOrder(id);
        model.addAttribute("payUrl", payUrl);
        model.addAttribute("orderId", id);
        return "AlipayHtml";
    }

    @RequestMapping(value = "/balance", method = RequestMethod.GET)
    @ResponseBody
    public void payOrderBalance(@RequestParam Long id, @RequestParam String tradeCode) {
        orderPayService.payOrderBalance(id, SecurityUtils.getAuthenticatedUser().getId(), tradeCode);
    }

    @ApiOperation("衫德h5快捷")
    @RequestMapping(value = "/sandQuick", method = RequestMethod.GET, produces = "text/html")
    @ResponseBody
    public String sandQuick(@RequestParam Long id) {
        return sandPayService.payOrderQuick(id);
    }

    @RequestMapping(value = "/gift/alipay", method = RequestMethod.GET)
    @ResponseBody
    public String payGiftOrderAlipayH5(Long id) {
        return orderPayService.payGiftOrder(id);
    }

    @RequestMapping(value = "/gift/alipay_wx", method = RequestMethod.GET)
    public String payGiftOrderAlipayWx(Long id, Model model) {
        String payUrl = orderPayService.payGiftOrder(id);
        model.addAttribute("payUrl", payUrl);
        model.addAttribute("orderId", id);
        return "AlipayHtml";
    }

    @RequestMapping(value = "/gift/balance", method = RequestMethod.GET)
    public void payGiftOrderBalance(@RequestParam Long id, @RequestParam String tradeCode) {
        orderPayService.payGiftBalance(id, SecurityUtils.getAuthenticatedUser().getId(), tradeCode);
    }

    @RequestMapping(value = "/gift/sandQuick", method = RequestMethod.GET, produces = "text/html")
    @ResponseBody
    public String payGiftQuick(@RequestParam Long id) {
        return sandPayService.payGiftQuick(id);
    }

    @RequestMapping(value = "/mint/alipay", method = RequestMethod.GET)
    @ResponseBody
    public String payMintOrderAlipayH5(Long id) {
        return orderPayService.payMintOrder(id);
    }

    @RequestMapping(value = "/mint/alipay_wx", method = RequestMethod.GET)
    public String payMintOrderAlipayWx(Long id, Model model) {
        String payUrl = orderPayService.payMintOrder(id);
        model.addAttribute("payUrl", payUrl);
        model.addAttribute("orderId", id);
        return "AlipayHtml2";
    }

    @RequestMapping(value = "/mint/sandQuick", method = RequestMethod.GET, produces = "text/html")
    @ResponseBody
    public String payMintQuick(@RequestParam Long id) {
        return sandPayService.payMintQuick(id);
    }

    @RequestMapping(value = "mint/balance", method = RequestMethod.GET)
    public void payMintOrderBalance(@RequestParam Long id, @RequestParam String tradeCode) {
        orderPayService.payMintOrderBalance(id, SecurityUtils.getAuthenticatedUser().getId(), tradeCode);
    }

    @RequestMapping("/recharge")
    public String recharge(@RequestParam BigDecimal amount) {
        return orderPayService.recharge(SecurityUtils.getAuthenticatedUser().getId(), amount);
    }
}
