package com.td.boss.game.comusers.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.comusers.pojo.ComUsers;
import com.td.boss.game.comusers.vo.ComUsersVo;
import com.td.boss.game.comusers.service.ComUsersService;
import com.td.boss.util.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@RestController
@RequestMapping("/game/comUsers/")
public class ComUsersController extends CommonController<ComUsersVo, ComUsers, String> {
    @Autowired
    private ComUsersService comUsersService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Value("${spring.profiles.active}")
    private String active;


    @GetMapping("loginToken")
    public Result<Map> userLogin(@RequestParam(value = "loginId",required = false)String loginId) {
        log.info("login active:"+active);
        //todo 正常流程下，需要获取验证登录过的信息来处理token，这里暂时走测试信息流程
        String userId ;
        if(active.equals("dev")){
            if(loginId == null || !loginId.equals("1")){
                return Result.of(null,false,"请输入参数 loginId = '1' ",ResultEnum.NO_PARAMETERS_CARRIED.getCode());
            }
            //如果测试环境中，提供测试账号
            userId = "1";
        }else{
            //todo 正式环境未处理
            userId = "";
        }
        final String token = jwtTokenUtil.generateToken(userId);
        Map map = new HashMap();
        map.put("active",active);
        map.put("token",token);
        return Result.of(map);
    }


    /**
     * 获取用户信息
     * @param userId
     * @return
     */
    @GetMapping("getUserInfo")
    public Result<Map> getUserInfo(@RequestParam(value = "userId") String userId) {
        Map map = new HashMap();
        ComUsersVo comUsersVo = comUsersService.findByUserId(userId);

        if(comUsersVo == null){
            return Result.of(null,false, ResultEnum.USER_DOES_NOT_EXIST.getMessage(),ResultEnum.USER_DOES_NOT_EXIST.getCode());
        }

        map.put("energy",comUsersVo.getEnergy());
        map.put("purchaseEnergy",comUsersVo.getPurchaseEnergy());
        map.put("mnt",comUsersVo.getMnt());
        map.put("mnw",comUsersVo.getMnw());
        map.put("bgb",comUsersVo.getBgb());
        map.put("mns",comUsersVo.getMns());

        return Result.of(map);
    }

}
