package cn.wisenergy.web.admin.controller.app;

import cn.wisenergy.common.constant.RedisConsts;
import cn.wisenergy.common.utils.R;
import cn.wisenergy.common.utils.RedisUtils;
import cn.wisenergy.model.app.*;
import cn.wisenergy.model.dto.*;
import cn.wisenergy.model.vo.*;
import cn.wisenergy.service.app.*;
import cn.wisenergy.web.common.BaseController;
import cn.wisenergy.web.config.JwtConfig;
import cn.wisenergy.web.shiro.JwtUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

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


/**
 * @author 86187
 */
@Api(tags = "用户管理")
@RestController
@Slf4j
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private CultivatingPrizeService cultivatingPrizeService;


    @Autowired
    private CultivatingPrizeInfoService cultivatingPrizeInfoService;

    @Autowired
    private UserForestStatusService userForestStatusService;

    @Autowired
    RecommendUserService recommendUserService;

    @Autowired
    TeamUserInfoService teamUserInfoService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private JwtConfig jwtConfig;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedisUtils redisUtils;

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息", httpMethod = "GET")
    @ApiImplicitParam(name = "userId", value = "用户id", dataType = "String")
    @GetMapping("/user/getByUserId")
    public User getByUserId(String userId) {
        return userService.getById(userId);
    }

    /**
     * shiro登录
     *
     * @return/user
     */
    @ApiOperation(value = "获取token接口", notes = "获取token接口", httpMethod = "POST")
    @PostMapping(value = "/user/login")
    public R<String> login(String id) {
        if (null == id) {
            return R.error("入参为空!");
        }

        //用户信息
        User user = userService.getByUserId(id);

        // 创建token
        String token = jwtUtil.generateToken(user);
        // 保存Redis
        redisTemplate.opsForValue().set(RedisConsts.JWT_ACCESS_TOKEN + token, token);
        return R.ok(token);
    }

    @ApiOperation(value = "获取用户森林状态接口", notes = "获取用户森林状态接口", httpMethod = "GET")
    @GetMapping("/user/forestStatus")
    public R<Map> forestStatus(String userId) {
        if (null == userId) {
            return R.error("入参为空");
        }

        try {
            Map userForestStatus = userForestStatusService.getUserForestStatus(userId);
            return R.ok(userForestStatus);
        } catch (Exception e) {
            return R.error(1, "请重新尝试");
        }
    }

    @ApiOperation(value = "获取用户我的直推信息", notes = "获取用户我的直推信息", httpMethod = "GET")
    @GetMapping("/user/myRecommend")
    public R<Map> getMyRecommend(String userId) {
        //用户id
        int userLevel = userService.getByUserId(userId).getUserLevel();
        //用户各等级人数
        RecommendUser myRecommed = recommendUserService.getMyRecommed(userId);
        Map myRecommedMap = new HashMap();
        myRecommedMap.put("userlevel", userLevel);
        myRecommedMap.put("myRecommed", myRecommed);
        return R.ok(myRecommedMap);
    }

    @ApiOperation(value = "获取用户我的团队信息", notes = "获取用户我的团队推信息", httpMethod = "GET")
    @GetMapping("/user/myTeam")
    public R<TeamUserInfo> getMyTeam(String userId) {
        TeamUserInfo myTeam = teamUserInfoService.getOneById(userId);
        return R.ok(myTeam);
    }

    @ApiOperation(value = "获取用户我的直推信息详情", notes = "获取用户我的直推信息详情", httpMethod = "GET")
    @GetMapping("/user/myRecommendInfo")
    public R<List<UserAndRecommendVo>> getMyRecommendInfo(@RequestParam("userId") String userId, @RequestParam("userlevel") int userlevel) {
        List<UserAndRecommendVo> myRecommendInfo = recommendUserService.getMyRecommendInfo(userId, userlevel);
        return R.ok(myRecommendInfo);
    }

    @ApiOperation(value = "查询空投池信息", notes = "查询空投池信息", httpMethod = "GET")
    @GetMapping("/user/queryAerialDelivery")
    public R<AerialDeliveryVo> queryAerialDelivery() {
        log.info("shop-mall[]UserController[]queryAerialDelivery[]input.method");

        return userService.queryAerialDelivery();
    }


    @ApiOperation(value = "设置头像", notes = "设置头像", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "String"),
            @ApiImplicitParam(name = "headImage", value = "头像Url", dataType = "String")
    })
    @PostMapping("/user/setHeadImage")
    public R<Boolean> setHeadImage(String userId, String headImage) {
        log.info("shop-mall[]UserController[]setHeadImage[]input.param.userId,headImage:" + userId, headImage);
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(headImage)) {
            return R.error("入参不能为空!");
        }

        return userService.setHeadImage(userId, headImage);
    }

    @ApiOperation(value = "设置页面-邀请码填写", notes = "设置页面-邀请码填写", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "String"),
            @ApiImplicitParam(name = "inviteCode", value = "邀请码", dataType = "String")
    })
    @PostMapping("/user/fillInInviteCode")
    public R<Boolean> fillInInviteCode(String userId, String inviteCode) {
        log.info("shop-mall[]UserController[]fillInInviteCode[]input.param.userId,inviteCode:" + userId, inviteCode);
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(inviteCode)) {
            return R.error("入参不能为空!");
        }

        return userService.fillInInviteCode(userId, inviteCode);
    }

    @ApiOperation(value = "后台-用户列表", notes = "用户列表", httpMethod = "GET")
    @ApiImplicitParam(name = "query", value = "查询参数", dataType = "TeamQueryDto")
    @GetMapping("/admin/user/queryUserList")
    public R<PageInfo<User>> queryUserList(TeamQueryDto queryDto) {
        log.info("shop-mall[]UserController[]queryUserList[]input.param.query:" + queryDto);
        return userService.getUserList(queryDto);
    }

    @ApiOperation(value = "后台-直推用户列表", notes = "直推用户列表", httpMethod = "GET")
    @ApiImplicitParam(name = "query", value = "查询参数", dataType = "RecommendUserDto")
    @GetMapping("/admin/user/queryRecommendUserList")
    public R<PageInfo<RecommendUserVo>> queryRecommendUserList(RecommendUserDto recommendUserDto) {
        log.info("shop-mall[]UserController[]queryRecommendUserList[]input.param.query:" + recommendUserDto);
        return recommendUserService.getRecommendUserList(recommendUserDto);
    }

    @ApiOperation(value = "后台-团队用户列表", notes = "团队用户列表", httpMethod = "GET")
    @ApiImplicitParam(name = "query", value = "查询参数", dataType = "TeamUserInfoDto")
    @GetMapping("/admin/user/queryTeamUserInfoList")
    public R<PageInfo<TeamUserInfoVo>> queryTeamUserInfoList(TeamUserInfoDto teamUserInfoDto) {
        log.info("shop-mall[]UserController[]queryTeamUserInfoList[]input.param.query:" + teamUserInfoDto);
        return teamUserInfoService.getTeamUserInfoList(teamUserInfoDto);
    }

    @ApiOperation(value = "后台-培育奖记录表", notes = "培育奖记录表", httpMethod = "GET")
    @ApiImplicitParam(name = "query", value = "查询参数", dataType = "CultivatingPrizeInfoDto")
    @GetMapping("/admin/user/queryCultivatingInfo")
    public R<PageInfo<CultivatingPrizeInfoVo>> queryCultivatingPrizeInfoList(CultivatingPrizeInfoDto cultivatingPrizeInfoDto) {
        log.info("shop-mall[]UserController[]queryCultivatingPrizeInfoList[]input.param.query:" + cultivatingPrizeInfoDto);
        return cultivatingPrizeInfoService.getCultivatingPrizeInfoList(cultivatingPrizeInfoDto);
    }

    @ApiOperation(value = "后台-培育奖表列表", notes = "培育奖表列表", httpMethod = "GET")
    @GetMapping("/admin/user/queryCultivatingPrizeInfoList")
    public R<List<CultivatingPrize>> queryCultivatingPrizeList() {
        log.info("shop-mall[]UserController[]queryCultivatingPrizeList[]input.param.query:");
        return cultivatingPrizeService.getCultivatingPrizeList();
    }

    @ApiOperation(value = "后台-设置用户等级为幼苗", notes = "后台-设置用户等级为幼苗", httpMethod = "GET")
    @GetMapping("admin/user/setUserLevelToSeedling")
    public R<String> setUserLevelToSeedling(String userId,Integer userLevel){
        if (null == userId || null == userLevel){
            return R.error("入参为空");
        }

        User userByUserId = userService.getByUserId(userId);
        if (null == userByUserId){
            return R.error(1,"当前用户不存在");
        }

        if (userLevel != 0){
            return R.error("用户等级不是普通用户，请查看自己等级");
        }
        //进行用户等级设置
        int flag = userService.setUserLevelToSeedling(userId , userLevel);
        if (flag == 0){
            return R.ok(0,"设置成功");
        }else {
            return R.error(1,"设置失败，请重试");
        }

    }
}
