import config from '../../config'

const getAction = (x, y) => {
    const cell = config.map().get(x, y)
    const toggler = config.map().action(x, y)

    if (cell === 1) {
        const v = Math.random() > .8
        if (v) {
            const enemyId = Math.floor(Math.random() * config.enemies().length)
            const enemy = config.enemies()[enemyId]
            const health = 100
            config.player().battle = {
                enemy,
                health
            }
            return 'battle'
        }
    }
    // if (cell === 40) return 'save' // TODO: ??????
    if (cell === 44) {
        config.loadLevel(toggler)
        return 'lift'
    }
    if (cell === 3) {
        config.player().items.push(toggler)
        config.map().update(x, y, 0)
        config.player().lastItem = toggler
        return 'item'
    }
    // if (cell === 46) return 'shop' // TODO: ??????

    if (cell === 2) {
        config.map().update(toggler.x, toggler.y, toggler.value)
        return 'door'
    }

    return null
}

const cellsToMove = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 42, 44, 46]
const canMoveTo = (x, y) => {
    const cell = config.map().get(x, y)
    return cellsToMove.indexOf(cell) >= 0
}

export default {
    getAction,
    canMoveTo
}
