package com.red.circle.order.adapter.app;

import com.red.circle.common.business.dto.cmd.IdLongCmd;
import com.red.circle.common.business.dto.cmd.IdStringCmd;
import com.red.circle.order.app.dto.clientobject.pay.ApplicationCommodityCardV2CO;
import com.red.circle.order.app.dto.clientobject.pay.PayApplicationCO;
import com.red.circle.order.app.dto.clientobject.pay.PayCountryCO;
import com.red.circle.order.app.dto.clientobject.pay.PayOrderStatusCO;
import com.red.circle.order.app.dto.clientobject.pay.PlaceAnOrderResponseCO;
import com.red.circle.order.app.dto.clientobject.pay.UserProfileAndCountryCO;
import com.red.circle.order.app.dto.cmd.PayOrderStatusCmd;
import com.red.circle.order.app.dto.cmd.PayPlaceAnOrderCmd;
import com.red.circle.order.app.dto.cmd.PayPlaceAnOrderReceiptCmd;
import com.red.circle.order.app.dto.cmd.PayWebApplicationCommodityCmd;
import com.red.circle.order.app.dto.cmd.PayWebUserCmd;
import com.red.circle.order.app.service.InAppPurchaseProductService;
import com.red.circle.order.app.service.WebPayService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * web内购.
 *
 * @author pengliang on 2021/7/8
 * @eo.api-type http
 * @eo.groupName 内购服务.web内购
 * @eo.path /order/web/pay
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/web/pay", produces = MediaType.APPLICATION_JSON_VALUE)
public class WebPayRestController {

  private final WebPayService webPayService;
  private final InAppPurchaseProductService inAppPurchaseProductService;

  /**
   * 获得应用数据.
   *
   * @eo.name 获得应用数据.
   * @eo.url /application
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/application/{applicationId}")
  public PayApplicationCO getApplication(@PathVariable("applicationId") Long applicationId) {
    IdLongCmd idLongCmd = new IdLongCmd();
    idLongCmd.setId(applicationId);
    return webPayService.getApplication(idLongCmd);
  }

  /**
   * 用户基本资料信息.
   *
   * @eo.name 用户基本资料信息.
   * @eo.url /user-profile
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/user-profile")
  public UserProfileAndCountryCO getProfile(@Validated PayWebUserCmd cmd) {
    return webPayService.getUserProfileAndCountry(cmd);
  }

  /**
   * 开放支付国家.
   *
   * @eo.name 开放支付国家.
   * @eo.url /country
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/country")
  public List<PayCountryCO> country() {
    return webPayService.listOpenPayCountry();
  }

  /**
   * 获取商品列表.
   *
   * @eo.name 获取商品列表.
   * @eo.url /commodity
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/commodity")
  public ApplicationCommodityCardV2CO commodityV2(PayWebApplicationCommodityCmd cmd) {
    return webPayService.getChannelCommodityV2(cmd);
  }

  /**
   * 下单.
   *
   * @eo.name 下单.
   * @eo.url /recharge
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/recharge")
  public PlaceAnOrderResponseCO placeAnOrder(@RequestBody @Validated PayPlaceAnOrderCmd cmd) {
    return inAppPurchaseProductService.placeAnOrder(cmd);
  }

  /**
   * 查询支付单状态.
   *
   * @eo.name 查询支付单状态.
   * @eo.url /order-status
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/order-status")
  public PayOrderStatusCO orderStatus(@Validated PayOrderStatusCmd cmd) {
    return inAppPurchaseProductService.orderStatus(cmd);
  }

  /**
   * 支付收据 (后续版本计划移除).
   *
   * @eo.name 支付收据 (后续版本计划移除).
   * @eo.url /pay-receipt
   * @eo.method post
   * @eo.request-type json
   */
  @Deprecated
  @PostMapping("/pay-receipt")
  public PlaceAnOrderResponseCO payReceipt(@RequestBody @Validated PayPlaceAnOrderReceiptCmd cmd) {
    return inAppPurchaseProductService.payReceipt(cmd);
  }

  /**
   * 获取收据信息（后续版本计划移除）.
   *
   * @eo.name 获取收据信息（后续版本计划移除）.
   * @eo.url /collection-receipt
   * @eo.method post
   * @eo.request-type json
   */
  @Deprecated
  @PostMapping("/collection-receipt")
  public Object collectionReceipt(@RequestBody @Validated IdStringCmd cmd) {
    return inAppPurchaseProductService.collectionReceipt(cmd);
  }

}
