import canvas2d from './utils/canvas2d'
import looper from './utils/looper'
import mouseinput from './inputs/mouseinput'

const canvas = document.getElementById('canvas')

let _scenes = {
    start: () => {}
}

let _currentScene = null

const start = (scenes, options) => {
    _scenes = scenes

    scene('start')

    const ctx = canvas2d(canvas, options)

    looper(dt => {
        _currentScene.update(dt)
        _currentScene.draw(ctx)
    })
}

const scene = new_scene => {
    if (!_scenes[new_scene]) return

    if (_currentScene) {
        _currentScene.stop()
    }

    _currentScene = _scenes[new_scene]

    _currentScene.start()
}

let k = 1
const update_multipliers = () => k = canvas.width / window.innerWidth
window.addEventListener('resize', () => update_multipliers)
update_multipliers()

mouseinput.on('click', ({ x, y }) => {
    _currentScene && _currentScene.onMouseClick(x * k, y * k)
})

mouseinput.on('move', ({ x, y }) => {
    _currentScene && _currentScene.onMouseMove(x * k, y * k)
})

mouseinput.on('zoom', ({ delta }) => {
    _currentScene && _currentScene.onMouseZoom(delta)
})

export default {
    start,
    scene
}

/*
class App {
    constructor() {

    }

    start(options) {
        const ctx = canvas2d(canvas, options)

        looper(dt => {
            this.scene.update(dt)
            this.scene.draw(ctx)
        })
    }

    withResources(items) {
        return this
    }

    withMouse(events) {
        return this
    }

    setScene(scene) {
        this.scene.stop()
        this.scene = scene
        this.scene.start()
    }
}
*/
