package com.izouma.nineth.web;

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.OrderPayService;
import com.izouma.nineth.service.SandPayService;
import com.izouma.nineth.utils.SecurityUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Map;

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

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

    @RequestMapping(value = "/alipay_wx")
    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")
    @ResponseBody
    public void payOrderBalance(@RequestParam Long id, @RequestParam String tradeCode) {
        orderPayService.payOrderBalance(id, SecurityUtils.getAuthenticatedUser().getId(), tradeCode);
    }

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

    @ApiOperation("首信易协议支付")
    @RequestMapping(value = "/agreement")
    @ResponseBody
    public Map<String, Object> agreement(@RequestParam Long id, String bindCardId) {
        return orderPayService.payOrderAgreement(id, bindCardId);
    }

    @ApiOperation("首信易协议支付确认(通用)")
    @RequestMapping(value = "/confirmAgreement")
    @ResponseBody
    public void confirmAgreement(@RequestParam String requestId, @RequestParam String paymentOrderId,
                                 @RequestParam String code) {
        orderPayService.confirmOrderAgreement(requestId, paymentOrderId, code);
    }

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

    @RequestMapping(value = "/gift/alipay_wx")
    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")
    @ResponseBody
    public void payGiftOrderBalance(@RequestParam Long id, @RequestParam String tradeCode) {
        orderPayService.payGiftBalance(id, SecurityUtils.getAuthenticatedUser().getId(), tradeCode);
    }

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

    @RequestMapping(value = "/gift/agreement")
    @ResponseBody
    public Map<String, Object> payGiftAgreement(@RequestParam Long id, String bindCardId) {
        return orderPayService.payGiftOrderAgreement(id, bindCardId);
    }

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

    @RequestMapping(value = "/mint/alipay_wx")
    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", produces = "text/html")
    @ResponseBody
    public String payMintQuick(@RequestParam Long id) {
        return orderPayService.payMintQuick(id);
    }

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

    @RequestMapping(value = "/mint/agreement")
    @ResponseBody
    public Map<String, Object> payMintAgreement(@RequestParam Long id, String bindCardId) {
        return orderPayService.payMintOrderAgreement(id, bindCardId);
    }

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

    @RequestMapping(value = "/recharge/agreement")
    @ResponseBody
    public Map<String, Object> payRechargeAgreement(@RequestParam Long userId, @RequestParam BigDecimal amount,
                                                    String bindCardId) {
        return orderPayService.rechargeAgreement(userId, amount, bindCardId);
    }

    @RequestMapping(value = "/recharge/sandQuick", produces = "text/html")
    @ResponseBody
    public String payRechargeQuick(@RequestParam Long userId, @RequestParam BigDecimal amount) {
        return orderPayService.rechargeQuick(userId, amount);
    }

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

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

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

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

    @RequestMapping(value = "/auction/agreement")
    @ResponseBody
    public Map<String, Object> payAuctionAgreement(@RequestParam Long id, String bindCardId) {
        return orderPayService.payAuctionOrderAgreement(id, bindCardId);
    }
}
