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

import com.red.circle.common.business.enums.SendPropsOrigin;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.app.common.props.GroupRewardPoolCommon;
import com.red.circle.other.app.dto.clientobject.activity.RewardPoolCO;
import com.red.circle.other.app.dto.clientobject.activity.TemporaryActivityCountCO;
import com.red.circle.other.app.dto.cmd.activity.RewardPoolCmd;
import com.red.circle.other.app.dto.cmd.activity.TemporaryActivityQueryCmd;
import com.red.circle.other.app.service.activity.AppTemporaryActivityService;
import com.red.circle.other.infra.common.activity.PropsActivitySendCommon;
import com.red.circle.other.infra.common.activity.send.ReceiveActivityReward;
import com.red.circle.other.infra.database.mongo.entity.activity.UserTasksCount;
import com.red.circle.other.infra.database.mongo.service.activity.UserTasksCountService;
import com.red.circle.other.infra.database.rds.entity.activity.PropsActivityRuleConfig;
import com.red.circle.other.inner.asserts.PropsErrorCode;
import com.red.circle.other.inner.enums.activity.PropsActivityTypeEnum;
import com.red.circle.other.inner.model.dto.activity.rule.GameWeeklyTasksContent;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 发送游戏王奖励.
 *
 * @author lixiaofei on 2024/9/6
 */
@Service
@RequiredArgsConstructor
public class GameKingRewardPoolSendExe {

    private static final Logger log = LoggerFactory.getLogger(GameKingRewardPoolSendExe.class);
    private final PropsActivitySendCommon propsActivitySendCommon;
    private final AppTemporaryActivityService appTemporaryActivityService;
    private final GroupRewardPoolCommon groupRewardPoolCommon;
    private final UserTasksCountService userTasksCountService;

    public void execute(RewardPoolCmd cmd) {
        // 查询获奖用户
        TemporaryActivityQueryCmd temporaryActivityQueryCmd = new TemporaryActivityQueryCmd();
        temporaryActivityQueryCmd.setActivityType(cmd.getActivityType());
        temporaryActivityQueryCmd.setCountType("RANK");
        temporaryActivityQueryCmd.setLimit(10);
        temporaryActivityQueryCmd.setReqSysOrigin(cmd.getReqSysOrigin());
        List<TemporaryActivityCountCO> activityCount = appTemporaryActivityService.listRankGd(temporaryActivityQueryCmd);
        log.warn("activityCount：{}", activityCount);

        // 查询奖励
        List<PropsActivityRuleConfig> propsActivityRuleConfigs =  getActivityRuleConfigs(cmd);
        log.warn("propsActivityRuleConfigs：{}", propsActivityRuleConfigs);

        for (int i = 0; i < 10; i++) {
            if (i == 0) {
                PropsActivityRuleConfig rewardPoolItem = propsActivityRuleConfigs.get(i);
                TemporaryActivityCountCO item = activityCount.get(i);
                log.warn("发放第一名奖励：{},{}", item.getUserProfile().getId(), rewardPoolItem.getId());
                receiveActivityReward(rewardPoolItem, item, cmd);
            }
            if (i == 1) {
                PropsActivityRuleConfig rewardPoolItem = propsActivityRuleConfigs.get(i);
                TemporaryActivityCountCO item = activityCount.get(i);
                log.warn("发放第二名奖励：{},{}", item.getUserProfile().getId(), rewardPoolItem.getId());
                receiveActivityReward(rewardPoolItem, item, cmd);
            }
            if (i == 2) {
                PropsActivityRuleConfig rewardPoolItem = propsActivityRuleConfigs.get(i);
                TemporaryActivityCountCO item = activityCount.get(i);
                log.warn("发放第三名奖励：{},{}", item.getUserProfile().getId(), rewardPoolItem.getId());
                receiveActivityReward(rewardPoolItem, item, cmd);
            }
            if (i > 2) {
                PropsActivityRuleConfig rewardPoolItem = propsActivityRuleConfigs.get(3);
                TemporaryActivityCountCO item = activityCount.get(i);
                log.warn("发放第4-5名奖励：{},{}", item.getUserProfile().getId(), rewardPoolItem.getId());
                receiveActivityReward(rewardPoolItem, item, cmd);
            }
        }
    }

    private void receiveActivityReward(PropsActivityRuleConfig rewardPoolItem, TemporaryActivityCountCO item, RewardPoolCmd cmd) {
        propsActivitySendCommon.receiveActivityReward(new ReceiveActivityReward()
                .setRuleId(rewardPoolItem.getId())
                .setAcceptUserId(item.getUserProfile().getId())
                .setSysOrigin(cmd.requireReqSysOriginEnum())
                .setSendPropsOrigin(SendPropsOrigin.GAME_KING_AWARD)
                .setPropsActivityType(PropsActivityTypeEnum.GAME_KING)
                .setReceiveBefore(rule -> {})
                .setReceiveAfter(rule -> {})
        );
    }

    private List<PropsActivityRuleConfig> getActivityRuleConfigs(RewardPoolCmd cmd) {
        List<PropsActivityRuleConfig> configs = groupRewardPoolCommon
                .listByActivityTypeLimit(cmd.requireReqSysOriginEnum(),
                        PropsActivityTypeEnum.GAME_KING);
        ResponseAssert.notEmpty(CommonErrorCode.DATA_ERROR, configs);
        ResponseAssert.isTrue(CommonErrorCode.DATA_ERROR, configs.size() >= 3);
        return configs;
    }

}
