import Scene from '../scene'

import config from '../../config'
import res from '../../cgt/resources'

import font from '../font'

import StateLoading from './state-loading.js'
import StateReady from './state-ready.js'

export default class StartScene extends Scene {
    init() {
        let bgImg
        res.load.image('start', config.resources().startImg)
            .then(img => {
                bgImg = img
                this.setState(new StateLoading(bgImg))
                return res.load.all(config.resources().all)
            })
            .then(() => {
                font.init(res.get('font'), 7, 10)
                this.setState(new StateReady(bgImg)).on('start', () => this.startGame())
            })
            .catch(() => {
                throw 'Error during loading'
            })
    }

    startGame() {
        config.startGame()
        this.setScene('map')
    }
}
