package org.wfc.payment.pay.paypal.service.impl;

import cn.hutool.core.util.StrUtil;
import com.paypal.sdk.PaypalServerSdkClient;
import com.paypal.sdk.controllers.OrdersController;
import com.paypal.sdk.exceptions.ApiException;
import com.paypal.sdk.http.response.ApiResponse;
import com.paypal.sdk.models.AmountWithBreakdown;
import com.paypal.sdk.models.CaptureOrderInput;
import com.paypal.sdk.models.CheckoutPaymentIntent;
import com.paypal.sdk.models.CreateOrderInput;
import com.paypal.sdk.models.Order;
import com.paypal.sdk.models.OrderRequest;
import com.paypal.sdk.models.PurchaseUnitRequest;
import org.springframework.stereotype.Service;
import org.wfc.common.core.domain.R;
import org.wfc.payment.pay.paypal.service.IPayPalService;
import org.wfc.user.api.RemoteUUserService;
import org.wfc.user.api.domain.vo.UOrderVo;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.RoundingMode;
import java.util.Arrays;

/**
 * @author: cyc
 * @since: 2025-04-23
 */
@Service
public class PaypalServiceImpl implements IPayPalService {

    @Resource
    private PaypalServerSdkClient client;

    @Resource
    private RemoteUUserService remoteUUserService;

    @Override
    public Order createOrder(Long orderId) throws IOException, ApiException {
        UOrderVo orderVo = getAmountByOrder(orderId);
        String amount = orderVo.getOrderAmount().setScale(2, RoundingMode.HALF_UP).toString();
        String currency = "USD";
        if (StrUtil.isNotBlank(orderVo.getCurrency())) {
            currency = orderVo.getCurrency();
        }
        CreateOrderInput createOrderInput = new CreateOrderInput.Builder(
                null,
                new OrderRequest.Builder(
                        CheckoutPaymentIntent.CAPTURE,
                        Arrays.asList(
                                new PurchaseUnitRequest.Builder(
                                        new AmountWithBreakdown.Builder(
                                                currency,
                                                amount)
                                                .build())
                                        .build()))
                        .build())
                .build();

        OrdersController ordersController = client.getOrdersController();

        ApiResponse<Order> apiResponse = ordersController.createOrder(createOrderInput);

        return apiResponse.getResult();
    }

    private UOrderVo getAmountByOrder(Long orderId) {
        R<UOrderVo> orderRes = remoteUUserService.getOrderById(orderId);
        return orderRes.getData();
    }

    @Override
    public Order captureOrders(String paypalOrderId, Long orderId) throws IOException, ApiException {
        CaptureOrderInput ordersCaptureInput = new CaptureOrderInput.Builder(
                paypalOrderId,
                null)
                .build();
        OrdersController ordersController = client.getOrdersController();
        ApiResponse<Order> apiResponse = ordersController.captureOrder(ordersCaptureInput);

        remoteUUserService.paySuccess(orderId, "inner");
        return apiResponse.getResult();
    }
}
