class Game {
    private context: CanvasRenderingContext2D;
    private canvas: HTMLCanvasElement;
    
    private world : World;
    private player : Player;
    private image : Images;

    private tiles : number = 500;
    private hills : number = 1500;

    private runonce : boolean = true;

    // player or map movement
    private toggleMovement : boolean = false;
    
    constructor() {
        this.image = new Images();
        //not yet usuable, planning to put some customization options through (like worldgen options)
    }
    
    public Start() : void {

        this.canvas = document.getElementsByTagName("canvas")[0];
        this.context = this.canvas.getContext('2d');
        
        this.canvas.width = window.innerWidth;
        this.canvas.height = (window.innerHeight-64);
        
        //If a world is already running destroy its timers (example TileSwitch causes duplicate flickering tiles)
        if(!this.runonce)
        {
            this.world.destroyTimer()
            this.runonce = true;
        }
        
        this.setUpNewGame();

        requestAnimationFrame(() => this.update()); //i dont remember if this actually works, seems not????           
    }
    
    private update() : void {
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
        this.world.updateWorldDisplay();

        //requestAnimationFrame(() => this.update());
    }

    private setUpNewGame() : void {
        this.world = new World(this.tiles, this.hills)
        this.world.setupWorld();
        
        this.player = new Player(this.world, 'player1', 0)
        this.player.spawnPlayerInWorld();
        this.runonce = false;
    }
    
    public keyPress(event:KeyboardEvent) : void {
        
        switch(event.keyCode){
            //player interaction (e key)
            case 69:
                this.player.Action();
                break;

            //movement
            case 38:
                if(this.toggleMovement == true)
                {
                    this.player.playerMovement('up');
                }else{
                    this.world.camMovement(this.player.getID(), 'up');
                }
                break;
            case 39:
                if(this.toggleMovement == true)
                {
                    this.player.playerMovement('right');
                }else{
                    this.world.camMovement(this.player.getID(), 'right');
                }
                break;
            case 40:
                if(this.toggleMovement == true)
                {
                    this.player.playerMovement('down');
                }else{
                    this.world.camMovement(this.player.getID(), 'down');
                }
                break;
            case 37:
                if(this.toggleMovement == true)
                {
                    this.player.playerMovement('left');
                }else{
                    this.world.camMovement(this.player.getID(), 'left');
                }
                break;
            
            //c key
            case 67:
                if(this.toggleMovement == true) {
                    this.toggleMovement = false;
                } else {
                    this.toggleMovement = true;
                }
                break;
            
            //map zoom in/out
            case 73: 
                this.world.mapZoom('in');
                break;
            case 79: 
                this.world.mapZoom('out');
                break;
        }
    }

    public clickedMouse(event):void {
        
        var ex = event.pageX - canvas.offsetLeft,
            ey = event.pageY - canvas.offsetTop;

        this.world.getPlayerClickedTileInfo(this.player.getID(), ex, ey)
    	
    }
}