package com.td.Dawa.game.complayersattri.service;

import cn.hutool.core.util.StrUtil;
import com.td.Dawa.common.pojo.Result;
import com.td.Dawa.common.service.*;
import com.td.Dawa.config.enums.ResultEnum;
import com.td.Dawa.config.exception.AuthorizeException;
import com.td.Dawa.game.complayersattri.pojo.ComPlayersAttri;
import com.td.Dawa.game.complayersattri.vo.ComPlayersAttriClientVo;
import com.td.Dawa.game.complayersattri.vo.ComPlayersAttriSimpleVo;
import com.td.Dawa.game.complayersattri.vo.ComPlayersAttriVo;
import com.td.Dawa.game.complayersattri.repository.ComPlayersAttriRepository;
import com.td.Dawa.game.excelparameter.service.ExcelParameterService;
import com.td.Dawa.game.excelparameter.vo.ExcelParameterVo;
import com.td.Dawa.util.CopyUtil;
import com.td.Dawa.util.DateUtil;
import com.td.Dawa.util.RedisData;
import com.td.Dawa.util.RedisLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.naming.AuthenticationException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Service
@Transactional
public class ComPlayersAttriServiceImpl extends CommonServiceImpl<ComPlayersAttriVo, ComPlayersAttri, Integer> implements ComPlayersAttriService {

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private ComPlayersAttriRepository comPlayersAttriRepository;

    @Autowired
    private RedisLock redisLock;

    /**
     * 如果没有体力相关数据，创建一个新的数据，此代码块加redis锁
     * @param userId
     * @return
     */
    @Override
    public ComPlayersAttriVo findByUserId(String userId) {
        long time = System.currentTimeMillis() + RedisData.getPlayerTimeout();
        String redisKey = StrUtil.format("players_attri_{}", userId);
        // redis锁
        if (!redisLock.lock(redisKey, String.valueOf(time))) {
            throw new AuthorizeException(ResultEnum.PLAYER_ATTRIBUTE_SAVE_LOCK);
        }
        ComPlayersAttri comPlayersAttri = comPlayersAttriRepository.findByUserId(userId).orElse(null);
        ComPlayersAttriVo playersAttribute = CopyUtil.copy(comPlayersAttri, ComPlayersAttriVo.class);
        if (comPlayersAttri == null) {
            // todo player 如果角色不存在的话，直接创建一个。
            playersAttribute = new ComPlayersAttriVo();
            playersAttribute.setUserId(userId);
            playersAttribute.setStrength(0);
            playersAttribute.setLucky(0);
            playersAttribute.setResetStrength(DateUtil.getNowDate());
            playersAttribute.setCreateTime(DateUtil.getNowDate());
            playersAttribute.setUpdateTime(DateUtil.getNowDate());
            playersAttribute = super.save(playersAttribute).getData();
        }
        redisLock.unlock(redisKey, String.valueOf(time));
        return playersAttribute;
    }

    /**
     * 返回一个当前的显示体力计算
     * @param attriVo
     * @param strength
     * @return
     */
    @Override
    public ComPlayersAttriClientVo setStrengthAttriClientVo(ComPlayersAttriVo attriVo,Integer strength){
        //前端的显示,输出一个不小于0的体力值
        Integer _maxStrength = strength;
        Integer _out = _maxStrength - Math.abs(attriVo.getStrength());
        ComPlayersAttriClientVo clientVo = CopyUtil.copy(attriVo, ComPlayersAttriClientVo.class);
        clientVo.setCurrentStrength(_out < 0 ? 0 : _out);
        //最大值是配置的值和链上数据的值相加
        clientVo.setMaxStrength(_maxStrength);
        return  clientVo;
    }


    @Override
    public void consumesStrengthFromGetRole() {

    }
}
