package com.ssj.aijia.game.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.security.manager.TokenManager;
import com.ssj.framework.core.util.ResponseConstant;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.service.conch.game.request.QuestionReq;
import com.ssj.service.conch.game.request.RanklistReq;
import com.ssj.service.conch.game.request.SavePersonInfoReq;
import com.ssj.service.conch.game.request.SaveV1Req;
import com.ssj.service.conch.game.service.GameService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.*;

/**
 * @author Mount Liu
 * @date 2018/1/22
 */
@Api(value = "小程序游戏相关的接口", tags = "小程序游戏相关的接口")
@RestController
@RequestMapping("/api/game/V1")
public class GameV1Controller extends BaseController {

	@Reference(version = "1.0" ,timeout = 50000)
    private GameService gameService;

    @Autowired
    private TokenManager tokenManager;

    @RequestMapping(value = "/homepageInfo", method = RequestMethod.POST)
    @ApiOperation(value = "查询首页信息", httpMethod = "POST", notes = "作业记录列表")
    public ResponseEntity homepageInfo(@RequestParam(name = "accessToken") String accessToken) {
        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = gameService.homepageInfo(userId);
        } catch (Exception e) {
            logger.error("查询首页信息失败", e);
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "保存游戏结果", httpMethod = "POST", notes = "作业记录列表")
    public ResponseEntity save(@RequestParam(name = "accessToken") String accessToken,
                               @RequestBody SaveV1Req req) {
        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = gameService.saveResult(userId, req);
        } catch (Exception e) {
            logger.error("保存游戏结果失败", e);
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/rankingList", method = RequestMethod.POST)
    @ApiOperation(value = "排行榜列表", httpMethod = "POST", notes = "排行榜列表")
    public ResponseEntity rankingList(@RequestParam(name = "accessToken") String accessToken, @RequestBody RanklistReq req) {
        ResponseEntity responseEntity;
        try {
            Pageable pageable = initPage(req.getPageNo(), req.getPageSize(), Direction.DESC, "score", "name");
            responseEntity = gameService.rankingList(req, pageable);
        } catch (Exception e) {
            logger.error("查看排行榜失败", e);
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/savePersonInfo", method = RequestMethod.POST)
    @ApiOperation(value = "保存游戏结果", httpMethod = "POST", notes = "作业记录列表")
    public ResponseEntity savePersonInfo(@RequestParam(name = "accessToken") String accessToken,
                                         @RequestBody SavePersonInfoReq req) {
        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = gameService.savePersonInfo(userId, req);
        } catch (Exception e) {
            logger.error("保存游戏结果失败", e);
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/findResult", method = RequestMethod.POST)
    @ApiOperation(value = "查看测试结果", httpMethod = "POST", notes = "作业记录列表")
    public ResponseEntity findResult(@RequestParam(name = "accessToken") String accessToken) {
        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = gameService.findResult(userId);
        } catch (Exception e) {
            logger.error("查看测试结果失败", e);
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/findQuestion", method = RequestMethod.POST)
    @ApiOperation(value = "获取关卡题目", httpMethod = "POST", notes = "作业记录列表")
    public ResponseEntity findQuestion(@RequestParam(name = "accessToken") String accessToken, QuestionReq req) {
        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = gameService.findQuestion(userId, req.getSelection());
        } catch (Exception e) {
            logger.error("查看测试结果失败", e);
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

}