import State from '../state'
import config from '../../config'
import keys from '../../cgt/inputs/keyboard'
import screen from './screen'

class ActionState extends State {
    init() {

    }

    prepare(action) {
        this.waitKeyA = keys.get('A')

        let text = []

        if (action === 'attack') {
            const a = ~~(Math.random() * 20 + 10)
            config.player().health = Math.max(0, config.player().health - a)
            text.push('Получен урон: ' + a)
        }

        if (action === 'fire') {
            const a = ~~(Math.random() * 40 + 20)
            config.player().battle.health = Math.max(0, config.player().battle.health - a)
            if (config.player().battle.health === 0) {
                text.push('Противник мертв')
            } else {
                text.push('Нанесен урон: ' + a)
            }
        }

        if (action === 'health') {
            config.player().health = Math.min(100, config.player().health + 50)
            text.push('Использована', 'аптечка')
        }

        if (action === 'slow') {
            text.push('Стазис модуль не заряжен')
        }

        if (action === 'run') {
            if (Math.random() > .2) {
                text.push('Побег удался')
                this.emit('run')
            } else {
                text.push('Не удалось убежать')
            }
        }

        text.push('A: далее')

        screen.setText( text )
        screen.setMarker( -1 )
    }

    draw(ctx) {
        screen.render(ctx)
    }

    update() {
        if (this.waitKeyA) {
            if (keys.get('A')) return
            this.waitKeyA = false
        }

        if (keys.get('A')) {
            this.emit('done')
        }
    }
}

export default ActionState
