package com.td.boss.game.complayers.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.comgeneralattributes.pojo.ComGeneralAttributes;
import com.td.boss.game.comgeneralattributes.service.ComGeneralAttributesService;
import com.td.boss.game.complayers.pojo.ComPlayers;
import com.td.boss.game.complayers.vo.ComPlayersVo;
import com.td.boss.game.complayers.service.ComPlayersService;
import com.td.boss.util.CopyUtil;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/game/comPlayers/")
public class ComPlayersController extends CommonController<ComPlayersVo, ComPlayers, Integer> {
    @Autowired
    private ComPlayersService comPlayersService;

    @Autowired
    private ComGeneralAttributesService comGeneralAttributesService;
    /**
     * 创建玩家
     * @param userId
     * @return
     */
    @PostMapping("createPlayer")
    public Result<Map> createPlayer(@RequestParam(value = "userId") String userId,
                                    @RequestParam(value = "name")String name,
                                    @RequestParam(value = "avatar")String avatar) {
        //todo 疑问avatar获取渠道？
        ComPlayers player = comPlayersService.findByUserId(userId);
        if(player == null){
            //player 不存在
            player = new ComPlayers();
            player.setUserId(userId);
            player.setAttributeType(0);
        }
        player.setAvatar(avatar);
        player.setName(name);
        ComPlayers addPlayer = comPlayersService.addComPlayers(player);

        Map map = new HashMap();
        map.put("avatar",addPlayer.getAvatar());
        map.put("name",addPlayer.getName());
        map.put("attributeType",addPlayer.getAttributeType());
        return Result.of(map);
    }


    /**
     * 获取玩家信息
     * @param userId
     * @return
     */
    @GetMapping("getPlayer")
    public Result<Map> getPlayer(@RequestParam(value = "userId") String userId) {
        ComPlayers player = comPlayersService.findByUserId(userId);
        if(player == null){
            //player 不存在
            return  Result.of(null,false,ResultEnum.PLAYER_DOES_NOT_EXIST.getMessage(),ResultEnum.PLAYER_DOES_NOT_EXIST.getCode());
        }
        Map map = new HashMap();
        map.put("playerId",player.getId());
        map.put("avatar",player.getAvatar());
        map.put("name",player.getName());
        map.put("attributeType",player.getAttributeType());
        return Result.of(map);
    }


    @GetMapping("getBattlePlayerAttribute")
    public Result<ComGeneralAttributes> getBattlePlayerAttribute(@RequestParam("attributeType") Integer attributeType ) {

        ComGeneralAttributes playerAttribute = comGeneralAttributesService.findByType(attributeType);
        if(playerAttribute == null){
            return Result.of(null,false, ResultEnum.PLAYER_DOES_NOT_ATTRIBUTES.getMessage(),ResultEnum.PLAYER_DOES_NOT_ATTRIBUTES.getCode());
        }
        return Result.of(playerAttribute);
    }



    /**
     * 获取玩家信息并且携带属性
     * @param userId
     * @return
     */
    @GetMapping("getPlayerAndBattleAttribute")
    public Result<Map> getPlayerAndBattleAttribute(@RequestParam(value = "userId") String userId,
                                                   @RequestParam(value = "name")String name,
                                                   @RequestParam(value = "avatar")String avatar) {
        ComPlayers player = comPlayersService.findByUserId(userId);
        if(player == null){
            // todo player 如果角色不存在的话，直接创建一个。
            player = new ComPlayers();
            player.setUserId(userId);
            player.setAttributeType(0);
            player.setAvatar(avatar);
            player.setName(name);
            player = comPlayersService.addComPlayers(player);
        }
        ComGeneralAttributes playerAttribute = comGeneralAttributesService.findByType(player.getAttributeType());

        Map map = new HashMap();
        map.put("playerId",player.getId());
        map.put("avatar",player.getAvatar());
        map.put("name",player.getName());
        map.put("attribute",playerAttribute);
        return Result.of(map);
    }
}
