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.annotation.ModifySecurity;
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.*;
import com.ssj.service.conch.game.service.GameV2Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @description 脑力测试小程序
 * @author yrf
 * @time 2019/10/10 11:14
 */
@Api(value = "脑力测试小程序相关的接口", tags = "脑力测试小程序相关的接口")
@RestController
@RequestMapping("/api/game/v2")
public class GameV2Controller extends BaseController {

	@Reference(version = "1.0" ,timeout = 50000)
    private GameV2Service gameV2Service;

    @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 = gameV2Service.homepageInfo(userId);
        } catch (Exception e) {
            logger.error("查询首页信息异常", e);
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @ApiOperation(value = "答题情况保存", httpMethod = "POST", notes = "答题情况保存")
    @RequestMapping(value = "answerInfoSave", method = RequestMethod.POST)
    public ResponseEntity answerInfoSave(@RequestParam(name = "accessToken") String accessToken,
                                         @RequestBody @Valid AnswerInfoSaveReq req) {

        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = gameV2Service.answerInfoSave(userId, req);
        } catch (Exception e) {
            logger.error("req={}", req);
            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 @Valid SaveV2Req req) {
        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = gameV2Service.saveResult(userId, req);
        } catch (Exception e) {
            logger.error("req={}", req);
            logger.error("保存游戏结果异常", e);
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/saveV2", method = RequestMethod.POST)
    @ApiOperation(value = "保存游戏结果v2", httpMethod = "POST", notes = "保存游戏结果v2")
    @ModifySecurity
    public ResponseEntity saveV2(@RequestParam(name = "accessToken") String accessToken,
                                 @RequestBody @Valid SaveV2Req req) {

        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = gameV2Service.saveResultV2(userId, req);
        } catch (Exception e) {
            logger.error("保存游戏结果v2异常", e);
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @ApiOperation(value = "历史记录", httpMethod = "POST", notes = "历史记录")
    @RequestMapping(value = "/history", method = RequestMethod.POST)
    public ResponseEntity findHistory(@RequestParam(name = "accessToken") String accessToken,
                                      @RequestBody @Valid RanklistReq req) {

        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = gameV2Service.findHistory(userId, req);
        } catch (Exception e) {
            logger.error("req={}", req);
            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 @Valid RanklistReq req) {

        ResponseEntity responseEntity;
        try {
            responseEntity = gameV2Service.rankingList(req);
        } 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 = gameV2Service.savePersonInfo(userId, req);
        } catch (Exception e) {
            logger.error("保存游戏结果异常", e);
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器发生异常，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/saveRespondentInfo", method = RequestMethod.POST)
    @ApiOperation(value = "保存答题人信息", httpMethod = "POST", notes = "保存答题人信息")
    @ModifySecurity
    public ResponseEntity saveRespondentInfo(@RequestParam(name = "accessToken") String accessToken,
                                             @RequestBody @Valid SaveRespondentInfoReq req) {

        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = gameV2Service.saveRespondentInfo(userId, req);
        } catch (Exception e) {
            logger.error("req={}", req);
            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,
                                     @RequestBody @Valid TestResultReq req) {

        ResponseEntity responseEntity;
        String userId = null;
        try {
            userId = tokenManager.getUserId(accessToken);
            responseEntity = gameV2Service.findResult(userId, req);
        } catch (Exception e) {
            logger.error("userId={}, req={}", userId, req);
            logger.error("查看测试结果异常", e);
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
        }
        return responseEntity;
    }

    @RequestMapping(value = "/reset", method = RequestMethod.POST)
    @ApiOperation(value = "重置", httpMethod = "POST", notes = "重置")
    public ResponseEntity reset(@RequestParam(name = "accessToken") String accessToken) {
        ResponseEntity responseEntity;
        try {
            String userId = tokenManager.getUserId(accessToken);
            responseEntity = gameV2Service.reset(userId);
        } catch (Exception e) {
            logger.error("重置测试数据异常", e);
            responseEntity = new ResponseEntity();
            responseEntity.failure(ResponseConstant.CODE_500, "服务器繁忙，请稍后再试");
        }
        return responseEntity;
    }

}