package com.iformall.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxCouponOrder;
import com.iformall.domain.po.WxGame;
import com.iformall.domain.po.WxGameActionLog;
import com.iformall.domain.po.WxGameTemplate;
import com.iformall.enums.EnumGameStatus;
import com.iformall.service.WxCouponOrderService;
import com.iformall.service.WxGameService;
import com.iformall.service.WxGameTemplateService;
import com.iformall.service.WxOrderService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/game")
public class WxGameController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxGameService wxGameService;

    @Autowired
    private WxCouponOrderService wxCouponOrderService;

    @ApiOperation("蒙层/游戏入口获取游戏信息")
    @GetMapping("getOne")
    public ResultData getOne(@ModelAttribute WxGame wxGame) {
        if (wxGame == null) wxGame = new WxGame();
        wxGame.setTenantId(getTenantId());
        wxGame.setStatus(EnumGameStatus.STATUS_THROW_IN.getCode());
        // 因为有算法要求，分成两步获取
        WxGame record = wxGameService.getOne(wxGame);
        if (record == null) {
            return new ResultData(ErrorCode.GAME_NOT_FOUND);
        }

        return new ResultData(record);
    }

    @ApiOperation("获取游戏信息")
    @GetMapping("getOneGame")
    public ResultData getOneGame(@ModelAttribute WxGame wxGame) {
        if (wxGame == null) wxGame = new WxGame();
        wxGame.setTenantId(getTenantId());
        wxGame.setStatus(EnumGameStatus.STATUS_THROW_IN.getCode());
        // 因为有算法要求，分成两步获取
        WxGame record = wxGameService.getOne(wxGame);
        if (record == null) {
            return new ResultData(ErrorCode.GAME_NOT_FOUND);
        }

        return new ResultData(record);
    }


    @ApiOperation("添加游戏参与记录")
    @PostMapping("addActionLog")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gameId", value = "游戏ID", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "orderId", value = "券订单ID", dataType = "String", paramType = "query", required = true)})
    public ResultData addActionLog(@RequestBody Map<String,String> paramMap) {
        Long gameIdL = null;
        Long orderIdL = null;

        try {
            gameIdL = Long.valueOf(paramMap.get("gameId"));
            orderIdL = Long.valueOf(paramMap.get("orderId"));
        } catch (Exception e){
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
        }

        return wxGameService.addActionLog(getUserId(), gameIdL, orderIdL);
    }

    @ApiOperation("添加游戏参与记录")
    @GetMapping("getCount")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gameId", value = "游戏ID", dataType = "String", paramType = "query", required = true)})
    public ResultData getCount(String gameId) {
        Long gameIdL = null;
        try {
            gameIdL = Long.valueOf(gameId);
        } catch (Exception e){
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
        }

        return wxGameService.getCount(gameIdL,getUserId());
    }
}
