package com.example.demo01.controller;

import com.example.demo01.common.result.Result;
import com.example.demo01.entity.Player;
import com.example.demo01.entity.Room;
import com.example.demo01.service.BattleService;
import com.example.demo01.service.MapService;
import com.example.demo01.service.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/game")
public class GameController {

    @Autowired
    private PlayerService playerService;

    @Autowired
    private MapService mapService;

    @Autowired
    private BattleService battleService;

    @PostMapping("/move")
    public Result<Room> move(@RequestBody MoveRequest request) {
        // 1. 获取玩家信息
        Player player = playerService.getPlayerStatus(request.getPlayerId());
        if (player == null) return Result.error("玩家不存在");

        // 2. 调用地图服务判断是否可以移动
        Room nextRoom = mapService.move(player.getCurrentRoomId(), request.getDirection());

        if (nextRoom != null) {
            // 3. 移动成功，更新玩家所在的房间ID
            playerService.moveToRoom(player.getId(), nextRoom.getId());
            // 返回新房间的信息，让前端显示描述文字
            return Result.success(nextRoom);
        } else {
            return Result.error("哎呀！你撞到了墙上，那里没有路。");
        }
    }

    @PostMapping("/attack")
    public Result<BattleResult> doAttack(@RequestParam Long playerId, @RequestParam Long monsterId) {
        BattleResult result = battleService.attack(playerId, monsterId);
        return Result.success(result);
    }
}