package com.ichangzuo.system.controller;


import cn.hutool.json.JSONString;
import com.alibaba.fastjson.JSONObject;
import com.ichangzuo.common.result.AjaxResult;
import com.ichangzuo.config.WxpayConfig;
import com.ichangzuo.system.model.entity.User;
import com.ichangzuo.system.service.PayService;
import com.ichangzuo.system.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/client")
@RequiredArgsConstructor
public class ClientController {
    private final PayService payService;
    private final UserService userService;

    /**
     * 苹果及老版本请求订单
     * @param user
     * @param password
     * @param type
     * @param money
     * @return
     */
    @PostMapping(value = "/orderPlace", produces = MediaType.TEXT_HTML_VALUE)
    public String orderPlace(@RequestParam String user, @RequestParam String password, @RequestParam String type, @RequestParam String money) {
        return payService.getAppOrderInfo(user, password, type, money).toJSONString();
    }

//    /**
//     * 苹果支付验签 PHP网站调用
//     * @param param
//     * @return
//     */
//    @PostMapping("/verifyAppleReceipt")
//    public AjaxResult verifyAppleReceipt(@RequestBody String param) {
////        System.out.println(param);
//        return payService.verifyAppleReceipt(param, false);
//    }

    /**
     * 苹果支付结果查询
     * @param receipt
     * @param isSandbox
     * @param user
     * @param password
     * @param order
     * @return
     */
    @PostMapping(value = "/appleOrderFinish", produces = MediaType.TEXT_HTML_VALUE)
    public String appleOrderFinis(@RequestParam String receipt,
                                  @RequestParam String isSandbox,
                                  @RequestParam String user,
                                  @RequestParam String password,
                                  @RequestParam String order) {
        return payService.appleOrderFinis(receipt, isSandbox, user, password, order).toJSONString();
    }

    @PostMapping(value = "/appleOrderCheck")
    public JSONObject appleOrderCheck(@RequestParam String receipt,
                                  @RequestParam String isSandbox,
                                  @RequestParam String user,
                                  @RequestParam String password,
                                  @RequestParam String order) {
        return payService.appleOrderFinis(receipt, isSandbox, user, password, order);
    }

//    /**
//     * 支付宝/微信预约订单 php网站调用
//     * @param order
//     * @param money
//     * @param type
//     * @return
//     */
//    @PostMapping("/getPrepayInfo")
//    public AjaxResult getPrepayInfo(@RequestParam String order, @RequestParam String money, @RequestParam String type) {
//        System.out.println("getPrepayInfo: " + order + " " + money + " " + type);
//
//        String result = null;
//        if(Objects.equals(type, "ali_ad"))
//            result = payService.getAliPayAppInfo(money, order);
//        else if(Objects.equals(type, "wx_ad"))
//            result = payService.getWxPayAppInfo(money, order);
//        else
//            return AjaxResult.error("无效的支付方式");
//
//        if(result==null)
//            return AjaxResult.error("支付接口调用失败");
//
//        return AjaxResult.success(result);
//    }

//    /**
//     * alipay验签 php网站调用
//     * @param request
//     * @return
//     */
//    @PostMapping("/notifyAlipayOrder")
//    public AjaxResult notifyAlipayOrder(HttpServletRequest request) {
//        return payService.handleAlipayAppNotify(request);
//    }

//    /**
//     * wxpay验签 php网站调用
//     * @param request
//     * @return
//     */
//    @PostMapping("/notifyWxpayOrder")
//    public AjaxResult notifyWxpayOrder(HttpServletRequest request) {
//        return payService.handleWxpayNotify(request);
//    }

    /**
     * 删除账户
     * @param user
     * @param password
     * @return
     */
    @PostMapping(value = "/deleteUser", produces = MediaType.TEXT_HTML_VALUE)
    public String deleteUser(@RequestParam String user, @RequestParam String password) {
        JSONObject result = new JSONObject();
        result.put("result", userService.deleteUser(user, password));
        return result.toJSONString();
    }

    @PostMapping(value = "/delAccount")
    public JSONObject delAccount(@RequestParam String user, @RequestParam String password) {
        JSONObject result = new JSONObject();
        result.put("result", userService.deleteUser(user, password));
        return result;
    }

    /**
     * 创建订单
     * @param user
     * @param password
     * @param type
     * @param money
     * @return
     */
    @PostMapping("/getOrderInfo")
    public JSONObject getOrderInfo(@RequestParam String user, @RequestParam String password,
                                   @RequestParam String type, @RequestParam String money) {
        return payService.getAppOrderInfo(user, password, type, money);
    }

    /**
     * 订单查询
     * @param user
     * @param password
     * @param order
     * @return
     */
    @PostMapping("/orderCheck")
    public JSONObject orderCheck(@RequestParam String user, @RequestParam String password, @RequestParam String order) {
        return payService.orderCheck(user, password, order);
    }

    /**
     * 支付宝异步通知
     * @param request
     * @return
     */
    @PostMapping("/alipayOrderNotify")
    public String alipayOrderNotify(HttpServletRequest request) {
        AjaxResult result = payService.handleAlipayAppNotify(request);

        Integer code = (Integer) result.get(AjaxResult.CODE_TAG);
        if(code==200){
            String orderId = (String) result.get(AjaxResult.DATA_TAG);
            payService.orderFinished(orderId, 1.0);

            return "success";
        }

        return "fail";
    }

    /**
     * 微信WEB异步通知
     * @param request
     * @return
     */
    @PostMapping("/wxpayWebOrderNotify")
    public void wxpayPageOrderNotify(HttpServletRequest request, HttpServletResponse response) {
        payService.handleWxpayNotify(request, response, WxpayConfig.WX_WEB);
    }

    /**
     * 微信APP异步通知
     * @param request
     * @return
     */
    @PostMapping("/wxpayAppOrderNotify")
    public void wxpayAppOrderNotify(HttpServletRequest request, HttpServletResponse response) {
        payService.handleWxpayNotify(request, response, WxpayConfig.WX_APP );
    }
}
