package com.YuyeTech.TPlat.game.userinfo.controller;

import com.YuyeTech.TPlat.VO.ResultVO;
import com.YuyeTech.TPlat.constant.RedisConstant;
import com.YuyeTech.TPlat.game.userinfo.projo.UserInfo;
import com.YuyeTech.TPlat.enums.ResultEnum;
import com.YuyeTech.TPlat.form.FeedbackForm;
import com.YuyeTech.TPlat.game.gameInfo.service.GameInfoService;
import com.YuyeTech.TPlat.game.userinfo.service.MainInfoService;
import com.YuyeTech.TPlat.game.userinfo.service.UserInfoService;
import com.YuyeTech.TPlat.utils.AliyunOSSUtil;
import com.YuyeTech.TPlat.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * 管理数据库数据
 *
 * @author:slambb
 * @date:2020/4/3
 */
@RestController
@RequestMapping("/manager")
@Slf4j
public class ManagerController {

    @Autowired
    private MainInfoService mainInfoService;

    @Autowired
    private GameInfoService gameInfoService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 删除用户数据
     * @param userId
     * @return
     */
    @GetMapping("/deleteUser")
    public ResultVO deleteUser(@RequestParam("userId") String userId,
                               HttpServletRequest request) {
        //1.删除token
        String headToken = request.getHeader("token");
        log.info("{}",request.getParameterValues("userId")[0]);
        if (StringUtils.isNotBlank(headToken)) {
            log.info(String.format(RedisConstant.TOKEN_PREFIX, headToken));
            Boolean bSuccess = redisTemplate.delete(String.format(RedisConstant.TOKEN_PREFIX, headToken));
            if (!bSuccess) {
                //删除不成功，redis不存在，是非法token
                log.info("redis 没有对应的token");
                return ResultVOUtil.error(ResultEnum.TOKEN_IS_ILLEGAL.getCode(), ResultEnum.TOKEN_IS_ILLEGAL.getMessage());
            }
        }
        //2.删除头像W
        UserInfo userInfo = userInfoService.findUserInfoById(userId, false);
        if (!userInfo.getAvatarUrl().equals("publicAvatar/defaultAvatar.png") && userInfo.getAvatarUrl() != null && userInfo.getAvatarUrl().length() != 0) {
            if (aliyunOSSUtil.deleteBlog(userInfo.getAvatarUrl())) {
                log.info("删除头像成功");
            }
        }
        //3.删除数据库数据
        mainInfoService.deleteMainInfoById(userId);

        return ResultVOUtil.success();
    }

    /**
     * 更加userId 来删除信息
     * @param userId
     * @return
     */
    @GetMapping("/deleteUserById")
    public ResultVO deleteUserById(@RequestParam("userId") String userId) {
        UserInfo userInfo = userInfoService.findUserInfoById(userId, false);
        if (!userInfo.getAvatarUrl().equals("publicAvatar/defaultAvatar.png") && userInfo.getAvatarUrl() != null && userInfo.getAvatarUrl().length() != 0) {
            if (aliyunOSSUtil.deleteBlog(userInfo.getAvatarUrl())) {
                log.info("删除头像成功");
            }
        }
        //3.删除数据库数据
        mainInfoService.deleteMainInfoById(userId);

        return ResultVOUtil.success();
    }

    /**
     * 删除游戏
     * @param gameId
     * @return
     */
    @GetMapping("/deleteGame")
    @Transactional
    public ResultVO deleteGame(@RequestParam("gameId") String gameId) {
        gameInfoService.deleteByGameId(gameId);
        return ResultVOUtil.success();
    }


    @PostMapping("/upload_feedback")
    @ResponseBody
    public ResultVO toUploadBlogPost(@RequestParam(value = "files", required = false) MultipartFile[] files,
                                     @Valid FeedbackForm feedbackForm,
                                     Map<String, Object> map) {

        log.info("{}",files.length);


        return ResultVOUtil.success();
    }
}
