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

import com.red.circle.component.game.hotgame.props.HotGameProperties;
import com.red.circle.component.game.hotgame.request.HotGameUserCoinUpdate;
import com.red.circle.component.game.hotgame.request.HotGameUserProfileQuery;
import com.red.circle.component.game.hotgame.response.HotGameCoin;
import com.red.circle.component.game.hotgame.response.HotGameErrorEnum;
import com.red.circle.component.game.hotgame.response.HotGameResponse;
import com.red.circle.framework.web.annotation.IgnoreResultResponse;
import com.red.circle.other.app.service.game.override.domain.HotGameUserProfile;
import com.red.circle.other.app.service.game.override.service.HotGameService;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import com.red.circle.tool.core.text.StringUtils;
import com.red.circle.tool.crypto.SecurityUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * 提供 hotgame api.
 *
 * @author pengliang on 2023/4/17
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/game/hot")
public class HotGameRestController {

  private final HotGameService hostGameService;
  private final HotGameProperties hotGameProperties;

  @IgnoreResultResponse
  @PostMapping("/getUserInfo")
  public HotGameResponse<HotGameUserProfile> getUserProfile(
      @RequestBody HotGameUserProfileQuery query) {

    if (Objects.isNull(query) || StringUtils.isBlanks(query.getUid(), query.getGameId(),
        query.getToken(), query.getSign())) {
      return new HotGameResponse<HotGameUserProfile>()
          .setErrorCode(HotGameErrorEnum.PARAM_ERROR.getErrorCode());
    }

    query.setToken(SecurityUtils.urlDecode(query.getToken()));
    if (!query.verifySign(hotGameProperties.getSignKey())) {
      return new HotGameResponse<HotGameUserProfile>()
          .setErrorCode(HotGameErrorEnum.SIGN_VERIFY_FAIL.getErrorCode());
    }
    return hostGameService.getUserProfile(query);
  }

  @IgnoreResultResponse
  @PostMapping("/updateBalance")
  public HotGameResponse<HotGameCoin> updateUserCoin(@RequestBody HotGameUserCoinUpdate update) {
    if (Objects.isNull(update) || StringUtils.isBlanks(update.getUid(), update.getGameId(),
        update.getOrderId(), update.getSign())
        || Objects.isNull(update.getType()) || Objects.isNull(update.getCoin())) {
      return new HotGameResponse<HotGameCoin>()
          .setData(new HotGameCoin()
              .setBalanceCoin(0L)
              .setResponseId(IdWorkerUtils.getIdStr())
          ).setErrorCode(HotGameErrorEnum.PARAM_ERROR.getErrorCode());
    }
    update.setToken(SecurityUtils.urlDecode(update.getToken()));
    if (!update.verifySign(hotGameProperties.getSignKey())) {
      return new HotGameResponse<HotGameCoin>()
          .setData(new HotGameCoin()
              .setBalanceCoin(0L)
              .setResponseId(IdWorkerUtils.getIdStr()))
          .setErrorCode(HotGameErrorEnum.SIGN_VERIFY_FAIL.getErrorCode());
    }
    return hostGameService.updateUserCoin(update);
  }


}
