package com.example.demo01.service.impl;

import com.example.demo01.entity.Room;
import com.example.demo01.mapper.RoomMapper;
import com.example.demo01.service.MapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MapServiceImpl implements MapService {

    @Autowired
    private RoomMapper roomMapper;

    @Override
    public Room move(Long currentRoomId, String direction) {
        // 1. 先找到玩家所在的当前房间
        Room currentRoom = roomMapper.selectById(currentRoomId);
        if (currentRoom == null) return null;

        // 2. 根据方向获取目标房间的 ID
        Long nextRoomId = null;
        switch (direction.toLowerCase()) {
            case "north": nextRoomId = currentRoom.getNorthRoomId(); break;
            case "south": nextRoomId = currentRoom.getSouthRoomId(); break;
            case "east":  nextRoomId = currentRoom.getEastRoomId(); break;
            case "west":  nextRoomId = currentRoom.getWestRoomId(); break;
        }

        // 3. 如果 ID 为空，说明那边是墙
        if (nextRoomId == null) return null;

        // 4. 返回目标房间的完整信息
        return roomMapper.selectById(nextRoomId);
    }

    @Override
    public Room getRoomById(Long roomId) {
        return roomMapper.selectById(roomId);
    }
}