
import {MainView} from "./custom/mainView";
import {Game} from "@engine/core/game";
import {Container} from "@engine/model/impl/ui/generic/container";
import {Scene} from "@engine/model/impl/scene";
import {Snake} from "./custom/snake";
import {TextField} from "@engine/model/impl/ui/components/textField";
import {AppLib} from "./custom/appLib";


export class IntroSceneBehaviour {

    game:Game;
    scene:Scene;
    scoreLabel:TextField;

    onCreate(){
        let view:Container = MainView.build(this.game);
        let introView = this.game.uiBuilder.build({
            AbsoluteLayout: {
                pos:{x:0,y:0},
                children: [
                    {
                        // pos: {} todo error
                        TextField: {
                            pos: {x:10,y:10},
                            fontName: 'fnt',
                            text: 'Змейка-2 (IGDC)'
                        }
                    },
                    {
                        TextField: {
                            pos: {x:10,y:60},
                            fontName: 'fnt',
                            text: 'Рекорд:'
                        }
                    },
                    {
                        TextField: {
                            id: 'scoreLabel',
                            pos: {x:120,y:60},
                            fontName: 'fnt',
                            text: '0'
                        }
                    },
                    {
                        TextField: {
                            pos: {x:20,y:180},
                            fontName: 'fnt',
                            text: '<Нажмите пробел>'
                        }
                    }
                ]
            }
        });
        view.findObject({id:'mainFrame'}).appendChild(introView);
        this.scene.getDefaultLayer().appendChild(view);
        this.scoreLabel = this.scene.getDefaultLayer().findObject({id:'scoreLabel'}) as TextField;
    }

    onUpdate(){
        this.scoreLabel.setText(AppLib.getInstance().loadHiScore().toString());
        if (this.game.keyboard.isJustPressed(this.game.keyboard.KEY.SPACE)) {
            let scene = this.game.repository.getArray('Scene').find(it=>it.name=='mainScene');
            if (Snake.instance) Snake.instance.clear();
            AppLib.getInstance().reset();
            this.game.setCurrScene(scene);
            scene.prepared = true;
        }
    }

}
