package com.td.WaDa.game.complayerscore.controller;

import com.td.WaDa.common.controller.*;
import com.td.WaDa.common.pojo.Result;
import com.td.WaDa.config.enums.ResultEnum;
import com.td.WaDa.game.commallseed.pojo.ComMallSeed;
import com.td.WaDa.game.commallseed.service.ComMallSeedService;
import com.td.WaDa.game.commallseed.vo.ComMallSeedVo;
import com.td.WaDa.game.complayerscore.pojo.ComPlayerScore;
import com.td.WaDa.game.complayerscore.vo.ComPlayerScoreVo;
import com.td.WaDa.game.complayerscore.service.ComPlayerScoreService;
import com.td.WaDa.util.CopyUtil;
import com.td.WaDa.util.RedisData;
import com.td.WaDa.util.RedisLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/game/comPlayerScore/")
public class ComPlayerScoreController extends CommonController<ComPlayerScoreVo, ComPlayerScore, Integer> {
    @Autowired
    private ComPlayerScoreService comPlayerScoreService;

    @Autowired
    private ComMallSeedService comMallSeedService;

    @Autowired
    private RedisLock redisLock;

    /**
     * 积分兑换种子
     *
     * @param userId 用户ID
     * @param amount 本次兑换数量
     * @param seedId 种子id
     * @return {@link Result}<{@link String}>
     */
    @PostMapping("exchange")
    @ResponseBody
    public Result<Map> exchange(
            @RequestParam(value = "userId") String userId,
            @RequestParam(value = "amount") Integer amount,
            @RequestParam(value = "seedId") Integer seedId
    ) {
        long time = System.currentTimeMillis() + RedisData.getPlayerScoreTimeout();
        String _redisKey = "LOCK:PLAYER_SCORE_TIMEOUT:" + userId;
        try {
            if (!redisLock.lock(_redisKey, String.valueOf(time))) {
                return Result.of(null, false, ResultEnum.REDIS_IS_LOCK.getMessage(), ResultEnum.REDIS_IS_LOCK.getCode());
            }
            Result result = comPlayerScoreService.exchange(userId, amount, seedId);
            redisLock.unlock(_redisKey, String.valueOf(time));
            return result;
        } catch (Exception e) {
            redisLock.unlock(_redisKey, String.valueOf(time));
            return Result.of(null, false, ResultEnum.REDIS_IS_LOCK_ERROR.getMessage(), ResultEnum.REDIS_IS_LOCK_ERROR.getCode());
        }
    }


    /**
     * 返回十天期的当前种子
     *
     * @param userId
     * @return
     */
    @GetMapping("getSeeds")
    @ResponseBody
    public Result<Map> getSeeds(
            @RequestParam(value = "userId") String userId
    ) {

        Integer _limitDay = 10;
        List<ComMallSeed> comMallSeedList = comMallSeedService.findMallSeedAll().stream().
                filter(e -> e.getMaturity().equals(_limitDay) && e.getMallType().equals(0) && !e.getAmount().equals(0)).collect(Collectors.toList());
        //返回当前积分
        Map map = new HashMap();
        map.put("seeds", CopyUtil.copyList(comMallSeedList, ComMallSeedVo.class));
        map.put("score", comPlayerScoreService.getScore(userId));
        return Result.of(map);

    }
}
