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

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.common.business.dto.cmd.app.AppUserIdCmd;
import com.red.circle.framework.core.dto.ReqSysOrigin;
import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.wallet.app.dto.clientobject.FreightAccountCO;
import com.red.circle.wallet.app.dto.clientobject.FreightSellerCO;
import com.red.circle.wallet.app.dto.cmd.FreightDealerShipCmd;
import com.red.circle.wallet.app.dto.cmd.FreightDealerToUserShipCmd;
import com.red.circle.wallet.app.dto.cmd.FreightSellerCmd;
import com.red.circle.wallet.app.dto.cmd.FreightSellerQryCmd;
import com.red.circle.wallet.app.dto.cmd.FreightSellerUsableGoldCmd;
import com.red.circle.wallet.app.dto.cmd.FreightShipSendCmd;
import com.red.circle.wallet.app.service.UserFreightService;
import com.red.circle.wallet.inner.endpoint.tmp.api.FreightClientRestApi;
import com.red.circle.wallet.inner.model.tmp.CommonRestApiCmd;
import com.red.circle.wallet.inner.model.tmp.DealerSendRestApiCmd;
import com.red.circle.wallet.inner.model.tmp.FreightAccountTmpCO;
import com.red.circle.wallet.inner.model.tmp.FreightSellerRestApiCmd;
import com.red.circle.wallet.inner.model.tmp.FreightSellerTmpCO;
import com.red.circle.wallet.inner.model.tmp.FreightSellerUsableGoldRestApiCmd;
import com.red.circle.wallet.inner.model.tmp.FreightShipSellerSendRestApiCmd;
import com.red.circle.wallet.inner.model.tmp.FreightShipSendRestApiCmd;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
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 2024/1/4
 */
@Validated
@RestController
@RequestMapping(value = FreightClientRestApi.API_PREFIX, produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class FreightClientRestApiEndpoint implements FreightClientRestApi {

  private final UserFreightService userFreightService;

  @Override
  public ResultResponse<Void> addFreightSeller(FreightSellerRestApiCmd cmd) {
    FreightSellerCmd newCmd = new FreightSellerCmd();
    newCmd.setReqSysOrigin(ReqSysOrigin.of(cmd.getSysOrigin()));
    newCmd.setReqUserId(cmd.getReqUserId());
    newCmd.setUserId(cmd.getUserId());
    newCmd.setFreightId(cmd.getFreightId());
    userFreightService.addFreightSeller(newCmd);
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<List<FreightSellerTmpCO>> listFreightSeller(CommonRestApiCmd cmd) {
    AppExtCommand newCmd = new AppExtCommand();
    newCmd.setReqSysOrigin(ReqSysOrigin.of(cmd.getSysOrigin()));
    newCmd.setReqUserId(cmd.getReqUserId());

    List<FreightSellerCO> freightSellers = userFreightService.listFreightSeller(newCmd);

    if (CollectionUtils.isEmpty(freightSellers)) {
      return ResultResponse.success(CollectionUtils.newArrayList());
    }

    List<FreightSellerTmpCO> sellerTmp = new ArrayList<>();
    for (FreightSellerCO freightSeller : freightSellers) {
      sellerTmp.add(
          new FreightSellerTmpCO()
              .setId(freightSeller.getId())
              .setEarnGold(freightSeller.getEarnGold())
              .setBalance(freightSeller.getBalance())
              .setCreateTime(freightSeller.getCreateTime())
              .setUserProfile(freightSeller.getUserProfile())
      );
    }
    return ResultResponse.success(sellerTmp);
  }

  @Override
  public ResultResponse<Boolean> checkSeller(CommonRestApiCmd cmd) {
    AppExtCommand newCmd = new AppExtCommand();
    newCmd.setReqSysOrigin(ReqSysOrigin.of(cmd.getSysOrigin()));
    newCmd.setReqUserId(cmd.getReqUserId());
    return ResultResponse.success(userFreightService.checkSeller(newCmd));
  }

  @Override
  public ResultResponse<FreightSellerTmpCO> searchSellerById(Long sellerId) {
    FreightSellerQryCmd cmd = new FreightSellerQryCmd();
    cmd.setSellerId(sellerId);
    FreightSellerCO freightSeller = userFreightService.searchSellerById(cmd);
    if (Objects.isNull(freightSeller)) {
      return ResultResponse.success();
    }
    return ResultResponse.success(
        new FreightSellerTmpCO()
            .setId(freightSeller.getId())
            .setEarnGold(freightSeller.getEarnGold())
            .setBalance(freightSeller.getBalance())
            .setCreateTime(freightSeller.getCreateTime())
            .setUserProfile(freightSeller.getUserProfile())
    );
  }

  @Override
  public ResultResponse<Void> updateUsableGold(FreightSellerUsableGoldRestApiCmd cmd) {
    FreightSellerUsableGoldCmd newCmd = new FreightSellerUsableGoldCmd();
    newCmd.setReqSysOrigin(ReqSysOrigin.of(cmd.getSysOrigin()));
    newCmd.setReqUserId(cmd.getReqUserId());
    newCmd.setId(cmd.getId());
    newCmd.setEarnGold(cmd.getEarnGold());
    userFreightService.updateUsableGold(newCmd);
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<FreightAccountTmpCO> getFreightAccountInfo(CommonRestApiCmd cmd) {

    AppExtCommand newCmd = new AppExtCommand();
    newCmd.setReqSysOrigin(ReqSysOrigin.of(cmd.getSysOrigin()));
    newCmd.setReqUserId(cmd.getReqUserId());
    FreightAccountCO co = userFreightService.getFreightAccountInfo(newCmd);
    if (Objects.isNull(co)) {
      return ResultResponse.success();
    }

    return ResultResponse.success(
        new FreightAccountTmpCO()
            .setFreightBalance(co.getFreightBalance())
            .setEarnGold(co.getEarnGold())
            .setBalance(co.getBalance())
            .setUserProfile(co.getUserProfile())
    );
  }

  @Override
  public ResultResponse<BigDecimal> sendFreightShip(FreightShipSendRestApiCmd cmd) {
    FreightShipSendCmd newCmd = new FreightShipSendCmd();
    newCmd.setReqSysOrigin(ReqSysOrigin.of(cmd.getSysOrigin()));
    newCmd.setReqUserId(cmd.getReqUserId());
    newCmd.setAcceptUserId(cmd.getAcceptUserId());
    newCmd.setQuantity(cmd.getQuantity());
    newCmd.setReqTraceId(cmd.getReqTraceId());
    return ResultResponse.success(userFreightService.sendFreightShip(newCmd));
  }

  @Override
  public ResultResponse<BigDecimal> freightShipSellerSendUser(FreightShipSellerSendRestApiCmd cmd) {
    FreightDealerToUserShipCmd newCmd = new FreightDealerToUserShipCmd();
    newCmd.setReqSysOrigin(ReqSysOrigin.of(cmd.getSysOrigin()));
    newCmd.setReqUserId(cmd.getReqUserId());
    newCmd.setAcceptUserId(cmd.getAcceptUserId());
    newCmd.setQuantity(cmd.getQuantity());
    newCmd.setPassword(cmd.getPassword());
    newCmd.setReqTraceId(cmd.getReqTraceId());
    return ResultResponse.success(userFreightService.dealerToUserShip(newCmd));
  }

  @Override
  public ResultResponse<BigDecimal> sendToFreightShip(DealerSendRestApiCmd cmd) {
    FreightDealerShipCmd newCmd = new FreightDealerShipCmd();
    newCmd.setReqSysOrigin(ReqSysOrigin.of(cmd.getSysOrigin()));
    newCmd.setReqUserId(cmd.getReqUserId());
    newCmd.setAcceptUserId(cmd.getAcceptUserId());
    newCmd.setQuantity(cmd.getQuantity());
    newCmd.setPassword(cmd.getPassword());
    newCmd.setReqTraceId(cmd.getReqTraceId());
    return ResultResponse.success(
        userFreightService.dealerToFreightShip(newCmd)
    );
  }

  @Override
  public ResultResponse<Boolean> checkFreightFirstRecharge(String sysOrigin, Long userId) {
    AppUserIdCmd newCmd = new AppUserIdCmd();
    newCmd.setReqSysOrigin(ReqSysOrigin.of(sysOrigin));
    newCmd.setReqUserId(userId);
    newCmd.setUserId(userId);
    return ResultResponse.success(
        userFreightService.checkFreightFirstRecharge(newCmd)
    );
  }

  @Override
  public ResultResponse<BigDecimal> getFreightAvailableBalance(CommonRestApiCmd cmd) {
    AppExtCommand newCmd = new AppExtCommand();
    newCmd.setReqSysOrigin(ReqSysOrigin.of(cmd.getSysOrigin()));
    newCmd.setReqUserId(cmd.getReqUserId());
    return ResultResponse.success(userFreightService.getFreightAvailableBalance(newCmd));
  }

}
