/**
 * 游戏角色controller，管理前端游戏相关的角色操作
 */

package com.td.Dawa.game.comcharacters.controller;

import cn.hutool.core.date.DateUtil;
import com.td.Dawa.annotation.Encrypt;
import com.td.Dawa.common.controller.*;
import com.td.Dawa.common.pojo.Result;
import com.td.Dawa.config.enums.GameEnum;
import com.td.Dawa.config.enums.ResultEnum;
import com.td.Dawa.config.exception.AuthorizeException;
import com.td.Dawa.game.comcharacters.pojo.ComCharacters;
import com.td.Dawa.game.comcharacters.vo.ComCharactersClientVo;
import com.td.Dawa.game.comcharacters.vo.ComCharactersRandomVo;
import com.td.Dawa.game.comcharacters.vo.ComCharactersVo;
import com.td.Dawa.game.comcharacters.service.ComCharactersService;
import com.td.Dawa.game.comusers.service.ComUsersService;
import com.td.Dawa.game.excelcharacterattri.service.ExcelCharacterAttriService;
import com.td.Dawa.game.excelcharacterattri.vo.ExcelCharacterAttriVo;
import com.td.Dawa.util.CopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/game/comCharacters/")
public class ComCharactersController extends CommonController<ComCharactersVo, ComCharacters, Integer> {
    @Autowired
    private ComCharactersService comCharactersService;

    @Autowired
    private ExcelCharacterAttriService excelCharacterAttriService;

    @Autowired
    private ComUsersService comUsersService;

    /**
     * 返回三个随机的角色数据
     *
     * @param userId
     * @return
     */
    @PostMapping("getRole")
    @Encrypt
    public Result<List<ComCharactersRandomVo>> getRole(@RequestParam(value = "userId") String userId) {

        //消耗
        try {
            //消耗金币返回一个提示
            comUsersService.consumesGoldFromGetRole(userId);
            //1.excel 随机拿出number 数据, todo 暂时以attri表为主
            List<ComCharactersRandomVo> excelCharacter =  comCharactersService.randomRoleByNumber(3);
            //2.返回给前端一个character 数据表里面
            return Result.of(excelCharacter);
        }catch (AuthorizeException e) {
            return Result.of(null, false, e.getMessage(), e.getCode());
        }


    }

    /**
     * 玩家选择一个添加角色,
     * 因为召唤已经扣除了金币，添加角色直接检测是否召唤消耗过金币
     *
     * @param userId
     * @return
     */
    @PostMapping("addRole")
    @Encrypt
    public Result<ComCharactersClientVo> addRole(@RequestParam(value = "userId") String userId,
                                                 @RequestParam(value = "roleId") Integer roleId) {

        //todo  暂时roleType对应这roleId
        //      暂时可以创建多个相同的角色
        try {
            //拿出一个角色对应的配置数据来生成角色
            ExcelCharacterAttriVo excelCharacterAttriVo = excelCharacterAttriService.getVo(roleId);
            if (excelCharacterAttriVo == null) {
                return Result.of(null, false, ResultEnum.ROLE_DOES_NOT_EXIST);
            }
            //根据roleId 对应的数据，生成玩家数据,属性
            ComCharactersClientVo comCharacters = comCharactersService.addRole(excelCharacterAttriVo, userId);
            return Result.of(comCharacters);
        } catch (Exception e) {
            return Result.of(null, false, ResultEnum.ROLE_ADD_ERROR);
        }

    }


    /**
     * 修改名字
     * @param userId
     * @param id 使用 com_characters 里面的 id修改名字
     * @param name
     * @return
     */
    @PostMapping("modifyRoleName")
    @Encrypt
    public Result<?> modifyRoleName(@RequestParam(value = "userId") String userId,
                                                        @RequestParam(value = "id") Integer id,
                                                        @RequestParam(value = "name") String name) {
        try {
            //todo  暂时roleType对应这roleId
            ComCharactersVo comCharactersVo = comCharactersService.getVo(id);
            if (comCharactersVo == null) {
                return Result.of(null, false, ResultEnum.ROLE_DOES_NOT_EXIST);
            }
            //判定是否是自己的数据
            if(!comCharactersVo.getUserId().equals(userId)){
                return Result.of(null, false, ResultEnum.ROLE_ADD_ERROR);
            }
            if(name.length()>12){
                return Result.of(null, false, ResultEnum.ROLE_NAME_TOO_LONG);
            }
            if(StringUtils.hasText(name)){
                comCharactersVo.setName(name);
            }else{
                //加入传入是空的，用id来作为名字
                comCharactersVo.setName(comCharactersVo.getId().toString());
            }
            comCharactersVo.setUpdateTime(DateUtil.date());
            comCharactersService.save(comCharactersVo);
            return Result.of(CopyUtil.copy(comCharactersVo, ComCharactersClientVo.class));


        } catch (Exception e) {
            return Result.of(e.getMessage(), false, ResultEnum.ROLE_ADD_ERROR);
        }

    }


    /**
     * 列表排列顺序，根据选中职业对应属性高低排序
     * 挖矿(伐木)：力量高的排前边
     * 勘探：智力高的排前边
     * 运输：技巧高的排前边
     *
     * @param userId
     * @return
     */
    @PostMapping("getRoleList")
    @Encrypt
    public Result<List<ComCharactersClientVo>> getRoleList(@RequestParam(value = "userId") String userId,
    @RequestParam(value = "attribute") Integer attribute) {
        List<ComCharactersClientVo> charactersClientVos = comCharactersService.findByProfessionalSort(userId,attribute);
        return Result.of(charactersClientVos);
    }

    /**
     * 遣返角色
     * 根据当前角色state判断是否可遣返
     * @param userId
     * @param id
     * @return
     */
    @PostMapping("repatriateRole")
    @Encrypt
    public Result<String> repatriateRole(@RequestParam(value = "userId") String userId,
                                                        @RequestParam(value = "id") Integer id) {
        try {
            //todo  暂时roleType对应这roleId
            ComCharactersVo comCharactersVo = comCharactersService.getVo(id);
            if (comCharactersVo == null) {
                return Result.of(null, false, ResultEnum.ROLE_DOES_NOT_EXIST);
            }
            //判定是否是自己的数据
            if(!comCharactersVo.getUserId().equals(userId)){
                return Result.of(null, false, ResultEnum.ROLE_REPATRIATE_ERROR);
            }
            //todo 判断是否处于空闲中，否则不能遣返
            if(comCharactersVo.getState().equals(GameEnum.CHARACTERS_STATE_NORMAL.getValue())){
                //删除角色
                comCharactersService.delete(id);
                return Result.of("删除成功");
            }else{
                return Result.of(GameEnum.getByValue(comCharactersVo.getState()).getMessage());
            }
        } catch (Exception e) {
            return Result.of(null, false, ResultEnum.ROLE_REPATRIATE_ERROR);
        }

    }
}
