package com.td.boss.game.complayersattri.controller;

import com.td.boss.common.controller.*;
import com.td.boss.common.pojo.Result;
import com.td.boss.config.enums.ResultEnum;
import com.td.boss.game.complayersattri.pojo.ComPlayersAttri;
import com.td.boss.game.complayersattri.vo.ComPlayersAttriSimpleVo;
import com.td.boss.game.complayersattri.vo.ComPlayersAttriVo;
import com.td.boss.game.complayersattri.service.ComPlayersAttriService;
import com.td.boss.game.comsetting.service.ComSettingService;
import com.td.boss.game.comsetting.vo.ComSettingVo;
import com.td.boss.util.CopyUtil;
import com.td.boss.util.DappUtil;
import com.td.boss.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/game/comPlayersAttri/")
public class ComPlayersAttriController extends CommonController<ComPlayersAttriVo, ComPlayersAttri, Integer> {
    @Autowired
    private ComPlayersAttriService comPlayersAttriService;

    @Autowired
    private ComSettingService comSettingService;
    /**
     * 获取玩家属性
     *
     * @param userId
     * @return
     */
    @GetMapping("getPlayerAttribute")
    public Result<ComPlayersAttriSimpleVo> getPlayerAttributeFunction(@RequestParam(value = "userId") String userId) {
        //获取数据库相关配置
        ComSettingVo comSettingVo = comSettingService.get("1").getData();
        if (comSettingVo.equals(null)) {
            return Result.of(null, false, ResultEnum.SETTING_IS_NULL.getMessage(), ResultEnum.SETTING_IS_NULL.getCode());
        }
        //获取一下用户属性，新增表
        ComPlayersAttriVo playersAttribute = comPlayersAttriService.findByUserId(userId);
        if (playersAttribute == 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());
            comPlayersAttriService.save(playersAttribute);
        }
        ComPlayersAttriSimpleVo comPlayersAttriSimpleVo = CopyUtil.copy(playersAttribute, ComPlayersAttriSimpleVo.class);
        //todo 后台可配置,获取一个链上数据
        Integer _configStrength = comSettingVo.getMaxStrength() , _chainStrength = DappUtil.getChildrenCount(userId);
        //前端的显示,输出一个不小于0的体力值
        Integer _maxStrength = _configStrength + _chainStrength;
        Integer _out = _maxStrength - Math.abs(playersAttribute.getStrength());
        comPlayersAttriSimpleVo.setCurrentStrength(_out < 0 ? 0 : _out);
        //最大值是配置的值和链上数据的值相加
        comPlayersAttriSimpleVo.setMaxStrength(_maxStrength);

        return Result.of(comPlayersAttriSimpleVo);
    }

}
