package com.red.circle.wallet.inner.endpoint.wallet;

import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.framework.web.spring.RequestCallable;
import com.red.circle.wallet.infra.database.rds.entity.diamond.DiamondRunningWater;
import com.red.circle.wallet.inner.endpoint.wallet.api.WalletDiamondClientApi;
import com.red.circle.wallet.inner.model.cmd.*;
import com.red.circle.wallet.inner.model.dto.*;
import com.red.circle.wallet.inner.service.diamond.SalaryDiamondBalanceClientService;
import com.red.circle.wallet.inner.service.wallet.WalletDiamondClientService;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author pengliang on 2023/5/26
 */
@Validated
@RestController
@RequestMapping(value = WalletDiamondClientApi.API_PREFIX, produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class WalletDiamondClientEndpoint implements WalletDiamondClientApi {

  private final WalletDiamondClientService walletDiamondClientService;

  @Override
  public ResultResponse<BigDecimal> getBalance(Long userId) {
    return ResultResponse.success(walletDiamondClientService.getBalance(userId));
  }

  @Override
  public ResultResponse<Map<Long, BigDecimal>> mapBalance(Collection<Long> userIds) {
    return ResultResponse.success(walletDiamondClientService.mapBalance(userIds));
  }

  @Override
  public ResultResponse<Map<Long, BigDecimal>> salaryMapBalance(Collection<Long> userIds) {
    return ResultResponse.success(walletDiamondClientService.salaryMapBalance(userIds));
  }

  @Override
  public ResultResponse<Void> autoExchangeGold(Long userId, BigDecimal amount) {
    walletDiamondClientService.autoExchangeGold(userId,amount);
    return ResultResponse.success();

  }

  @Override
  public ResultResponse<WalletReceiptDTO> changeBalance(DiamondReceiptCmd cmd) {
    return ResultResponse.success(walletDiamondClientService.changeBalance(cmd));
  }

  @Override
  public CompletableFuture<ResultResponse<WalletReceiptDTO>> changeBalanceAsync(
      DiamondReceiptCmd cmd) {
    return RequestCallable.supplyAsync(() -> {
      walletDiamondClientService.changeBalance(cmd);
      return ResultResponse.success();
    });
  }

  @Override
  public ResultResponse<PageResult<DiamondBalanceDTO>> pageUserDiamondBalance(
      UserDiamondBalanceQryCmd query) {
    return ResultResponse.success(walletDiamondClientService.pageUserDiamondBalance(query));
  }

  @Override
  public ResultResponse<PageResult<UserDiamondRunWaterDTO>> pageUserDiamondRunWater(
      UserDiamondRunWaterQryCmd query) {
    return ResultResponse.success(walletDiamondClientService.pageUserDiamondRunWater(query));
  }

  @Override
  public ResultResponse<PageResult<UserDiamondOriginDTO>> pageUserDiamondOriginDetails(
      UserDiamondOriginQryCmd query) {
    return ResultResponse.success(walletDiamondClientService.pageUserDiamondOriginDetails(query));
  }

}
