package com.izouma.nineth.web;


import com.izouma.nineth.domain.MetaBonusScene;
import com.izouma.nineth.dto.MetaRestResult;
import com.izouma.nineth.repo.MetaBonusSceneRepo;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/metaBonusScene")
@AllArgsConstructor
public class MetaBonusSceneController {

    private MetaBonusSceneRepo metaBonusSceneRepo;

    @GetMapping("/{userId}/find")
    public MetaRestResult<List<MetaBonusScene>> find(@PathVariable Long userId) {
        List<MetaBonusScene> metaBonusScenes = metaBonusSceneRepo.findAllByUserId(userId);
        return MetaRestResult.returnSuccess(metaBonusScenes);
    }

    @PostMapping("/save")
    public MetaRestResult<Boolean> save(@RequestBody MetaBonusScene metaBonusScene) {
        if (Objects.nonNull(metaBonusScene.getId())) {
            return MetaRestResult.returnSuccess("参数错误：id必须为空", false);
        }
        if (Objects.isNull(metaBonusScene.getUserId())) {
            return MetaRestResult.returnError("参数错误：缺少用户id", false);
        }
        if (!(0 < metaBonusScene.getButton())) {
            return  MetaRestResult.returnError("参数错误：缺少按钮信息", false);
        }
        MetaBonusScene dbMetaBonusScene = metaBonusSceneRepo.findByUserIdAndButton(metaBonusScene.getUserId(), metaBonusScene.getButton());
        if (Objects.nonNull(dbMetaBonusScene)) {
          return MetaRestResult.returnError("该玩家已经触发过同一个彩蛋，无法多次触发", false);
        }
        metaBonusSceneRepo.save(metaBonusScene);
        return MetaRestResult.returnSuccess("触发成功", true);
    }

    @GetMapping("/count")
    public MetaRestResult<Integer> count() {
        long num = metaBonusSceneRepo.countNum();
        return MetaRestResult.returnSuccess(Integer.parseInt(String.valueOf(num)));
    }
}
