package com.sonic.frog.event.inner.handler;

import com.sonic.common.event.Event;
import com.sonic.common.event.EventConsumer;
import com.sonic.common.event.EventHandler;
import com.sonic.common.utils.RedisLock;
import com.sonic.frog.dao.AiUserStatDao;
import com.sonic.frog.event.inner.EventType;
import com.sonic.frog.event.inner.payload.CalcAiUserHeartbeatRankPayload;
import com.sonic.frog.service.AiUserHeartbeatRankService;
import com.sonic.frog.utils.RedisKeyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

import static com.sonic.frog.enums.Constants.threshold;

/**
 * 计算心动值榜单
 * @author code
 */
@Slf4j
@Component
public class CalcAiUserHeartbeatRankHandler implements EventHandler, InitializingBean {

    @Autowired
    private EventConsumer eventConsumer;
    @Autowired
    private AiUserHeartbeatRankService aiUserHeartbeatRankService;
    @Autowired
    private AiUserStatDao aiUserStatDao;
    @Autowired
    private RedisLock.RedisWrapper redisWrapper;
    @Autowired
    private RedisKeyUtils redisKeyUtils;

    @Override
    public void onEvent(Event event) {
        CalcAiUserHeartbeatRankPayload payload = event.normalizedData(CalcAiUserHeartbeatRankPayload.class);
        log.info("CalcAiUserHeartbeatRankHandler payload:{}", payload);
        //加锁处理
        RedisLock redisLock = new RedisLock(redisKeyUtils.calcHeartbeatRankLockKey(payload.getUserId()), redisWrapper);
        redisLock.tryAcquireRun(() -> {
            handler(payload);
            return true;
        });
    }

    /**
     *  处理
     * @param payload
     */
    private void handler(CalcAiUserHeartbeatRankPayload payload) {
        //计算 当前AI与所有和他聊天过的用户的心动值总和的榜单
        aiUserStatDao.incrementHeartbeatVal(payload.getAiId(), payload.getNewHeartbeatVal().subtract(payload.getOldHeartbeatVal()));

        BigDecimal orgVal = payload.getOldHeartbeatVal();
        BigDecimal newVal = payload.getNewHeartbeatVal();
        //比较的基础值
        BigDecimal heartbeatVal = BigDecimal.ZERO;

        log.info("CalcAiUserHeartbeatRankHandler orgVal:{}, newVal:{}, threshold:{}", orgVal, newVal, threshold);
        // 场景1：用户从15分以下升到15分以上，此时触发 + 新总分值
        if (orgVal.compareTo(threshold) < 0 && newVal.compareTo(threshold) >= 0) {
            heartbeatVal = new BigDecimal(newVal.intValue());
        }
        // 场景2：用户之前是15分，现在也是15分，此时触发 ± (新总分值 - 老总分值)
        else if (orgVal.compareTo(threshold) > 0 && newVal.compareTo(threshold) > 0) {
            heartbeatVal = newVal.subtract(orgVal);
        }
        // 场景3：用户从15分以上降到15分以下，此时触发 - 老分值
        else if (orgVal.compareTo(threshold) > 0 && newVal.compareTo(threshold) < 0) {
            heartbeatVal = orgVal.negate();
        }
        log.info("CalcAiUserHeartbeatRankHandler heartbeatVal:{}", heartbeatVal);
        //计算 当前用户和所有聊天过的AI的心动值总和榜单
        aiUserHeartbeatRankService.incrementHeartbeatVal(payload.getUserId(), heartbeatVal);
    }

    @Override
    public void afterPropertiesSet() {
        eventConsumer.registerHandler(EventType.CALC_HEARTBEAT_RANK.getEventCode(), this);
    }
}
