// https://www.google.ru/search?q=gameboy+pokemon+red&safe=off&tbm=isch&tbs=rimg:CXTmNgcAHy4DIjiEm9CsqxNDeXroGI4LSXUf6gXW_1Wi609Gyj7WLRa_1BgSSkzcnAUgiaRJPD5ovfKkWnuckoGKVWGyoSCYSb0KyrE0N5EQIx-f9JJJqkKhIJeugYjgtJdR8RA4Ud3kuZjn4qEgnqBdb9aLrT0RFmO4KQjqfs4CoSCbKPtYtFr8GBEYgHDY6lZ-oCKhIJJKTNycBSCJoR7_12MaOUPpZYqEglEk8Pmi98qRRFo_1ph9m42VIyoSCae5ySgYpVYbEarPJZemfip6&tbo=u&sa=X&ved=0ahUKEwjvws3uxMnVAhUmGZoKHTOgCh8Q9C8IHw&biw=1586&bih=892&dpr=1#imgrc=nr-GzjyOoi-H0M:
// http://orig03.deviantart.net/1d20/f/2011/024/a/a/dead_space_sprite___isaac_by_digitaleva-d37zk8j.png
// https://www.pond5.com/sound-effects/1/dead-space.html#1

/*
dead
https://www.google.ru/imgres?imgurl=http%3A%2F%2Fwww.gameland.ru%2Fpost%2F49257%2Fimg%2Fdead_space_2_05.jpg&imgrefurl=http%3A%2F%2Fwww.gameland.ru%2Fxbox360%2Fdead-space-2%2Fpreviews%2F49257%2F&docid=u8Ka6HLfTq0cWM&tbnid=lX74cTwxnxfPKM%3A&vet=10ahUKEwjFnOOl38zVAhXLvBoKHX2jCR8QMwhFKBUwFQ..i&w=1280&h=720&safe=off&bih=870&biw=1703&q=dead%20space%20%D1%81%D0%BC%D0%B5%D1%80%D1%82%D1%8C&ved=0ahUKEwjFnOOl38zVAhXLvBoKHX2jCR8QMwhFKBUwFQ&iact=mrc&uact=8
https://www.google.ru/imgres?imgurl=http%3A%2F%2Fstatic.diary.ru%2Fuserdir%2F2%2F5%2F1%2F6%2F2516458%2F77684686.jpg&imgrefurl=http%3A%2F%2Fdiary.ru%2F~grell777%2F%3Ffrom%3D80&docid=IGGMYPc7nhrEIM&tbnid=Qd2YOgRazNAQbM%3A&vet=10ahUKEwjFnOOl38zVAhXLvBoKHX2jCR8QMwhfKC8wLw..i&w=640&h=360&safe=off&bih=870&biw=1703&q=dead%20space%20%D1%81%D0%BC%D0%B5%D1%80%D1%82%D1%8C&ved=0ahUKEwjFnOOl38zVAhXLvBoKHX2jCR8QMwhfKC8wLw&iact=mrc&uact=8


https://youtu.be/Rmtshr1IglI?list=PLC2B435402E18BEC5&t=92
http://www.newgrounds.com/audio/listen/353073

40	save
41	closed door
42	opened door
43	trigger
44	лифт
45	item
46	shop
*/

import Scene from '../scene'
import screen from './screen'
import config from '../../config'

import actions from './actions'

import StandingState from './state-standing'
import MovingState from './state-moving'
import SaveState from './state-save'
import PickupState from './state-pickup'

export default class StartScene extends Scene {
    init() {
        screen.init()

        this.standing = new StandingState()
        this.standing.on('action', act => this.action(act))

        this.moving = new MovingState()
        this.moving.on('endmoving', () => this.endmoving())

        this.save = new SaveState()
        this.save.on('done', () => this.setState(this.standing))

        this.pickup = new PickupState()
        this.pickup.on('done', () => this.setState(this.standing))

        this.setState(this.standing)
    }

    action(act) {
        const directions = {
            'DOWN': 1,
            'RIGHT': 2,
            'LEFT': 3,
            'UP': 4
        }

        const dir = directions[act]
        if (dir) {
            let x = config.player().position.x
            let y = config.player().position.y
            config.player().dir = dir
            if (dir === 4) y -= 1
            else if (dir === 2) x += 1
            else if (dir === 1) y += 1
            else if (dir === 3) x -= 1

            if (actions.canMoveTo(x, y)) {
                this.moving.setDirection(dir)
                this.setState(this.moving)
            }
        }
    }

    endmoving() {
        const x = config.player().position.x
        const y = config.player().position.y
        const action = actions.getAction(x, y)
        switch (action) {
            // case 'save':
            //    this.setState(this.save)
            case 'battle':
                this.setScene('battle')
                break
            case 'item':
                this.setState(this.pickup)
                break
            case 'lift':
                this.setScene('map')
                break
            default:
                this.setState(this.standing)
        }
    }
}

