package com.red.circle.other.app.command.activity.query;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Lists;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.app.dto.clientobject.activity.ActivityUserCO;
import com.red.circle.other.app.dto.clientobject.game.GameKingDataCO;
import com.red.circle.other.app.dto.clientobject.game.GameKingTop2CO;
import com.red.circle.other.app.dto.clientobject.game.GameKingTop4CO;
import com.red.circle.other.app.dto.clientobject.game.GameKingTop8CO;
import com.red.circle.other.infra.database.cache.service.other.GameKingActivityCacheService;
import com.red.circle.tool.core.json.JacksonUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 游戏王本周记录 - 每周固定活动.
 *
 * @author pengshigang on 2021/12/29.
 */
@Service
@RequiredArgsConstructor
public class GameKingThisWeekQueryExe {

  private final GameKingActivityCacheService gameKingActivityCacheService;

  public GameKingDataCO execute(AppExtCommand cmd) {

    GameKingDataCO kingData = new GameKingDataCO();

    String top32UserListStr = gameKingActivityCacheService.getTop32UserList(
        cmd.requireReqSysOriginEnum());
    String top16UserListStr = gameKingActivityCacheService.getTop16UserList(
        cmd.requireReqSysOriginEnum());
    String top8UserListStr = gameKingActivityCacheService.getTop8UserList(
        cmd.requireReqSysOriginEnum());
    String top8UserGroupStr = gameKingActivityCacheService.getTop8UserGroup(
        cmd.requireReqSysOriginEnum());
    String top4UserGroupStr = gameKingActivityCacheService.getTop4UserGroup(
        cmd.requireReqSysOriginEnum());
    String top2UserGroupStr = gameKingActivityCacheService.getTop2UserGroup(
        cmd.requireReqSysOriginEnum());
    String championUserStr = gameKingActivityCacheService.getChampionUser(
        cmd.requireReqSysOriginEnum());
    String tmpTop2UserGroupStr = gameKingActivityCacheService
        .getTmpTop2UserGroup(cmd.requireReqSysOriginEnum());

    if (StringUtils.isNotBlank(top32UserListStr)) {
      kingData.setTop32List(Optional.ofNullable(JacksonUtils.readValue(top32UserListStr, new
              TypeReference<List<ActivityUserCO>>() {
              }))
          .orElse(Lists.newArrayList()));
    }

    if (StringUtils.isNotBlank(top16UserListStr)) {
      kingData.setTop16List(Optional.ofNullable(JacksonUtils.readValue(top16UserListStr, new
              TypeReference<List<ActivityUserCO>>() {
              }))
          .orElse(Lists.newArrayList()));
    }

    if (StringUtils.isNotBlank(top8UserListStr)) {
      kingData.setTop8List(Optional.ofNullable(JacksonUtils.readValue(top8UserListStr, new
              TypeReference<List<ActivityUserCO>>() {
              }))
          .orElse(Lists.newArrayList()));
    }

    if (StringUtils.isNotBlank(top8UserGroupStr)) {
      kingData.setTop8Group(
          Optional.ofNullable(JacksonUtils.readValue(top8UserGroupStr, GameKingTop8CO.class))
              .orElse(new GameKingTop8CO()));
    }

    if (StringUtils.isNotBlank(top4UserGroupStr)) {
      kingData.setTop4Group(
          Optional.ofNullable(JacksonUtils.readValue(top4UserGroupStr, GameKingTop4CO.class))
              .orElse(new GameKingTop4CO()));
    }

    if (StringUtils.isNotBlank(top2UserGroupStr)) {
      kingData.setTop2Group(
          Optional.ofNullable(JacksonUtils.readValue(top2UserGroupStr, GameKingTop2CO.class))
              .orElse(new GameKingTop2CO()));
    }

    if (StringUtils.isNotBlank(tmpTop2UserGroupStr)) {
      kingData.setTmpTop2Group(
          Optional.ofNullable(JacksonUtils.readValue(tmpTop2UserGroupStr, GameKingTop2CO.class))
              .orElse(new GameKingTop2CO()));
    }

    if (StringUtils.isNotBlank(championUserStr)) {
      kingData.setChampionUser(
          Optional.ofNullable(JacksonUtils.readValue(championUserStr, ActivityUserCO.class))
              .orElse(new ActivityUserCO()));
    }

    kingData.setTop4To1CountDown(gameKingActivityCacheService.getCountDown());
    kingData.setTop8StartTimeStr(gameKingActivityCacheService.getTop8StartTimeStr());
    kingData.setTop16StartTimeStr(gameKingActivityCacheService.getTop16StartTimeStr());
    kingData.setTop32StartTimeStr(gameKingActivityCacheService.getTop32StartTimeStr());
    kingData.setGameProgress(gameKingActivityCacheService.getGameProgress());
    return kingData;
  }


}
