package com.red.circle.game.fruit.application.command;

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.game.fruit.application.dto.enums.ClaimStatusEnum;
import com.red.circle.game.fruit.infrastructure.database.entity.GameFruitAwardRecord;
import com.red.circle.game.fruit.infrastructure.database.entity.GameFruitRoundRecord;
import com.red.circle.game.fruit.infrastructure.database.memory.service.GameFruitMemoryService;
import com.red.circle.game.fruit.infrastructure.database.repository.GameCodeManageService;
import com.red.circle.game.fruit.infrastructure.database.repository.GameFruitAwardRecordService;
import com.red.circle.game.fruit.infrastructure.database.repository.GameFruitLotteryRecordService;
import com.red.circle.game.fruit.infrastructure.database.repository.GameFruitRoundRecordService;
import com.red.circle.game.fruit.infrastructure.websocket.message.MessageSender;
import com.red.circle.game.fruit.infrastructure.websocket.message.TopicMessage;
import com.red.circle.game.fruit.props.GameProperties;
import com.red.circle.other.inner.asserts.GameErrorCode;
import com.red.circle.other.inner.endpoint.sys.SysGameListConfigClient;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.LocalDateUtils;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author lisizhe 2023/12/5  17:17
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class StartGameExe {

  private final MessageSender messageSender;
  private final GameProperties gameProperties;
  private final GameCodeManageService gameCodeManageService;
  private final SysGameListConfigClient gameListConfigClient;
  private final GameFruitMemoryService gameFruitMemoryService;
  private final GameFruitAwardRecordService gameFruitAwardRecordService;
  private final GameFruitRoundRecordService gameFruitRoundRecordService;
  private final GameFruitLotteryRecordService gameFruitLotteryRecordService;

  public Integer execute() {

    // 检查游戏是否正常运行
    Set<Long> gameIds = gameCodeManageService.getGameIdsByCode("GAME_FRUIT");

    if (CollectionUtils.isEmpty(gameIds)) {
      messageSender.sendTopic(
          TopicMessage.of(
              "broadcast", ResultResponse.failure(
                  GameErrorCode.THE_GAME_HAS_BEEN_TAKEN_DOWN)));
      return null;
    }
    Boolean isUnshelve = ResponseAssert.requiredSuccess(
        gameListConfigClient.checkGameUnshelve(gameIds));

    // 游戏是否下架
    if (isUnshelve) {
      messageSender.sendTopic(
          TopicMessage.of(
              "broadcast", ResultResponse.failure(
                  GameErrorCode.THE_GAME_HAS_BEEN_TAKEN_DOWN)));
      return null;
    }

    String sysOrigin = gameProperties.getSysOrigin();

    // 检查配合、奖品是否正常
    GameFruitRoundRecord gameFruitRoundRecord = gameFruitRoundRecordService.getGameRound();
    int round = 1;
    if (Objects.nonNull(gameFruitRoundRecord)) {
      // 上一轮还未结束，继续跑上一轮
      if (Objects.isNull(gameFruitRoundRecord.getEndTime())) {
        return null;
      }

/*
      // 上一轮还未结束，继续跑上一轮
      if (CollectionUtils.isEmpty(
          gameFruitLotteryRecordService.getRoundLotteryPrize(gameFruitRoundRecord.getId()))) {
        // 跳到开奖环节
//        gameFruitMemoryService.putRoundSec(10);
        return null;
      }

      // 判断上一轮奖励是否已经发放
      List<GameFruitAwardRecord> gameFruitAwardRecords = gameFruitAwardRecordService.listWaitingUser(
          gameFruitRoundRecord.getId(), ClaimStatusEnum.WAITING.name());

      if(CollectionUtils.isNotEmpty(gameFruitAwardRecords) && gameFruitAwardRecords.size() > 0){
        // 跳到发放奖励环节
//        gameFruitMemoryService.putRoundSec(5);
        return null;
      }*/

      if (Objects.equals(LocalDateUtils.nowToInteger().toString(),
          gameFruitRoundRecord.getDate())) {
        round = gameFruitRoundRecord.getRound() + 1;
      }
    }

    GameFruitRoundRecord roundRecord = new GameFruitRoundRecord()
        .setRound(round)
        .setSysOrigin(sysOrigin)
        .setDate(LocalDateUtils.nowToInteger().toString());

    gameFruitRoundRecordService.start(roundRecord);
    return round;
  }
}
