package com.red.circle.other.app.command.game.luckybox;

import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.common.business.dto.cmd.app.AppIdLongCmd;
import com.red.circle.common.business.enums.PropsActivityRewardEnum;
import com.red.circle.common.business.enums.SendPropsOrigin;
import com.red.circle.component.redis.service.RedisService;
import com.red.circle.other.infra.common.activity.PropsActivitySendCommon;
import com.red.circle.other.infra.common.activity.send.SendRewardParam;
import com.red.circle.other.infra.database.cache.service.other.GameNewLuckyBoxCacheService;
import com.red.circle.other.infra.database.rds.entity.game.GameLuckyBoxBountyConfig;
import com.red.circle.other.infra.database.rds.entity.game.GameLuckyBoxBountyDetailsConfig;
import com.red.circle.other.infra.database.rds.entity.props.PropsSourceRecord;
import com.red.circle.other.infra.database.rds.service.game.GameLuckyBoxBountyConfigService;
import com.red.circle.other.infra.database.rds.service.game.GameLuckyBoxBountyDetailsConfigService;
import com.red.circle.other.infra.database.rds.service.props.PropsSourceRecordService;
import com.red.circle.other.inner.model.cmd.material.PrizeDescribe;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * luckyBox领取赏金任务
 *
 * @author zongpubin on 2023/9/5
 */

@Component
@RequiredArgsConstructor
public class GameLuckyBoxClaimTaskCmdExe {

  private final RedisService redisService;
  private final PropsActivitySendCommon propsActivitySendCommon;
  private final PropsSourceRecordService propsSourceRecordService;
  private final GameNewLuckyBoxCacheService gameNewLuckyBoxCacheService;
  private final GameLuckyBoxBountyConfigService gameLuckyBoxBountyConfigService;
  private final GameLuckyBoxBountyDetailsConfigService gameLuckyBoxBountyDetailsConfigService;

  public void execute(AppIdLongCmd cmd) {
    String lockKey = "LUCKY_BOX_CLAIM_TASK:" + cmd.requiredReqUserId();
    try {

      if (redisService.lock(lockKey, 3)) {
        GameLuckyBoxBountyDetailsConfig bountyDetailsConfig = gameLuckyBoxBountyDetailsConfigService.getById(
            cmd.getId());
        if (Objects.isNull(bountyDetailsConfig)) {
          return;
        }

        GameLuckyBoxBountyConfig boxBountyConfig = gameLuckyBoxBountyConfigService.getById(
            bountyDetailsConfig.getBountyId());

        if (Objects.isNull(boxBountyConfig)) {
          return;
        }

        PropsSourceRecord props = propsSourceRecordService.getPropsById(
            bountyDetailsConfig.getPropsId());

        if (Objects.isNull(props)) {
          return;
        }

        propsActivitySendCommon.sendSingle(SendRewardParam.builder()
            .acceptUserId(cmd.requiredReqUserId())
            .origin(SendPropsOrigin.LUCKY_BOX_RECEIVE)
            .sysOrigin(SysOriginPlatformEnum.valueOf(cmd.requireReqSysOrigin()))
            .trackId(bountyDetailsConfig.getId())
            .build(), new PrizeDescribe()
            .setType(PropsActivityRewardEnum.CHAT_BUBBLE.eq(props.getType())
                ? PropsActivityRewardEnum.CHAT_BUBBLE.name() : PropsActivityRewardEnum.PROPS.name())
            .setTrackId(bountyDetailsConfig.getId())
            .setContent(bountyDetailsConfig.getPropsId().toString())
            .setQuantity(Integer.valueOf(bountyDetailsConfig.getValidDays()))
            .setDetailType(props.getType())
            .setRemark(SendPropsOrigin.LUCKY_BOX_REWARD.getDesc())
        );
        if (Objects.equals(boxBountyConfig.getType(), "DAILY")) {
          gameNewLuckyBoxCacheService.receiveLuckyBoxAwardDay(cmd.requiredReqUserId(),
              bountyDetailsConfig.getTargetQuantity());
        } else {
          gameNewLuckyBoxCacheService.receiveLuckyBoxAwardWeek(cmd.requiredReqUserId(),
              bountyDetailsConfig.getTargetQuantity());
        }
      }
    } finally {
      redisService.unlock(lockKey);
    }


  }
}
