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

import com.red.circle.common.business.dto.cmd.app.AppRoomIdCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.dto.CommonCommand;
import com.red.circle.framework.core.response.ResponseErrorCode;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.app.dto.cmd.activity.RewardPoolCmd;
import com.red.circle.other.app.scheduler.ResetWeekStarGiftTask;
import com.red.circle.other.app.scheduler.TeamBillTask;
import com.red.circle.other.app.scheduler.TeamSalaryPaymentTask;
import com.red.circle.other.app.service.game.GameKingService;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.database.mongo.service.game.GameRedPacketSendRecordService;
import com.red.circle.tool.core.json.JacksonUtils;
import com.red.circle.wallet.inner.endpoint.wallet.WalletGoldClient;
import java.util.HashMap;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author pengliang on 2023/10/18
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/open-other-test", produces = MediaType.APPLICATION_JSON_VALUE)
public class TestRestController extends BaseController {

  private final WalletGoldClient walletGoldClient;
  private final UserProfileGateway userProfileGateway;
  private final GameRedPacketSendRecordService gameRedPacketSendRecordService;

  private final ResetWeekStarGiftTask resetWeekStarGiftTask;
  private final TeamBillTask teamBillTask;
  private final TeamSalaryPaymentTask teamSalaryPaymentTask;
  private final GameKingService gameKingService;

//  @GetMapping("/redPacketReturnV2")
//  public String redPacketReturn(CommonCommand cmd) {
//
//    List<GameRedPacket> refundRedPackets = gameRedPacketSendRecordService.refundListByTypeUser();
//    if (CollectionUtils.isEmpty(refundRedPackets)) {
//      return "没有需要退款的红包呀";
//    }
//
//    refundRedPackets.forEach(red -> walletGoldClient.changeBalance(
//            GoldReceiptCmd.builder()
//                    .appIncome()
//                    .userId(red.getUserId())
//                    .eventId(red.getId())
//                    .sysOrigin(red.getSysOrigin())
//                    .origin(GoldOrigin.RECEIVE_RED_PACKET_REFUND)
//                    .amount(red.getAmount())
//                    .remark("房间红包退款:2024-2-27")
//                    .build()));
//
//    return "处理完成size:" + refundRedPackets.size();
//  }
//
//  @GetMapping("/redPacketReturnStateV2")
//  public String redPacketReturnState(CommonCommand cmd) {
//    gameRedPacketSendRecordService.updateTypeUserAvailableExistReceivedUser();
//    return "修改状态成功";
//  }

  @GetMapping("/ping")
  public CommonCommand ping(CommonCommand cmd) {
    log.warn("test:{}", JacksonUtils.toJson(cmd));
    return cmd;
  }

  /**
   * 测试服务依赖重启是否会终端，依赖关系： 1级
   */
  @GetMapping("/ping/one")
  public String ping1(CommonCommand cmd) {
    userProfileGateway.getByUserId(1L);
    walletGoldClient.getBalance(1L);
    return cmd.getReqTraceId();
  }


  /**
   * 循环依赖: other -> user -> team -> other -> wallet.
   */
  @GetMapping("/ping/for")
  public String ping2() {
    return "SUCCESS";
  }


  @GetMapping("/testResponse")
  public Map<String, String> testResponse() {
    Map<String, String> result = new HashMap<>();
    result.put("name", "Other");
    return result;
  }

  @PostMapping("/testRequestBody")
  public AppRoomIdCmd testRequestBody(@RequestBody AppRoomIdCmd cmd) {
    return cmd;
  }


  @PostMapping("/testResponseError")
  public void testRequestBody() {
    log.warn("测试异常 testRequestBody1");
    ResponseAssert.failure(ResponseErrorCode.NOT_ACCEPTABLE);
  }

  @PostMapping("/testResponseError2")
  public void testRequestBody2() {
    log.warn("测试异常 testRequestBody2");
    int i = 0 / 0;
  }

  @PostMapping("/resetWeekStarGiftTask")
  public void resetWeekStarGiftTask() {
    resetWeekStarGiftTask.resetWeekStarGift();
  }

  @PostMapping("/teamSalaryPaymentTask")
  public void teamSalaryPaymentTask() {
    teamSalaryPaymentTask.teamSalaryPaymentTaskTest();
  }

  @PostMapping("/processTeamMonthBill")
  public void processTeamMonthBill() {
    teamBillTask.processTeamBillRetry();
  }

  @PostMapping("/activity/send/game")
  public void activitySendGame(@RequestBody RewardPoolCmd cmd) {
    gameKingService.sendRewardPool(cmd);
  }


}
