//Player +|+ Vangame

class Player {
    private pid : number = 0;
    private pname : string;
    private world : World;

    private tree1 : number = 5;
    private tree2 : number = 6;

    private town : number = 13;

    constructor(w : World, name : string, id : number)
    {
        this.world = w;
        this.pname = name;
        this.pid = id;
    }

    public getWorld()   
    {
        return this.world;
    }

    public getName()
    {
        return this.pname;
    }

    public getID()
    {
        return this.pid;
    }

    public Action() : void
    {
        this.playerDoAction();
    }

    public playerMovement(dir : string) : void
    {
    	this.movePlayerInWorld(dir);
    }

    public spawnPlayerInWorld() : void
    {
        let spawnlocs = this.world.getSpawnLocations(),
            randspawn = Math.floor(Math.random()*(spawnlocs.length));

        //Add player to the world
        this.world.addPlayer(this.pid);

        //Sets player into the world
        this.world.setPlayerWorldPos(this.pid, spawnlocs[randspawn][0], spawnlocs[randspawn][1]);

        //Sets player camera in focus on player position
        this.world.setPlayerCamera(this.pid, spawnlocs[randspawn][0]-8, spawnlocs[randspawn][1]-8)
        
        console.log("pID: "+this.pid +", cam-pos-start=" + this.world.getPlayerCamX(this.pid) + "-" + this.world.getPlayerCamY(this.pid) + ", player-spawn-count:" + randspawn);
    }

    private playerDoAction()
    {
        let pX = this.world.getPlayerWorldX(this.pid), 
            pY = this.world.getPlayerWorldY(this.pid),
            ignore = false;

        if(this.world.getPlayerTile() == this.tree1 || this.world.getPlayerTile() == this.tree2)
        {
            if(this.world.getPlayerTile() == this.tree1)
            {
                console.log("cutting tree (inventory adding here)")
                return this.world.setTileType(pX, pY, 30);
            }else{
                console.log("cutting tree (inventory adding here)")
                return this.world.setTileType(pX, pY, 31);
            }
        }else if(this.isTileTypeAroundPlayer(this.tree1) || this.isTileTypeAroundPlayer(this.tree2)){
            //no second tree support yet
            let ret = new Object();
            ret = this.world.searchForTileTypeReturnClosest(pX, pY, 5);
            console.log("Checklist " + ret);
            console.log("cutting nearby tree (inventory adding here)");
            return this.world.setTileType(pX, pY, 30);
        }
        
        if(this.isTileTypeAroundPlayer(this.town)){
            return console.log("a town interaction here (placeholder)")
        }else{
            return console.log("nothing to interact with! (placeholder)")
        }
    } 
    
    //only returns true or false
    private isTileTypeAroundPlayer(tiletype)
    {
        let pX = this.world.getPlayerWorldX(this.pid), 
            pY = this.world.getPlayerWorldY(this.pid);
        return this.world.searchForTileType(pX, pY, tiletype)
    }

    //experimental 
    /*private searchForTileTypeReturnClosest(tiletype : number, newtile : number, tiletype2 : number = -1, newtile2 : number = -1)
    {
        let pX = this.world.getPlayerWorldX(), 
            pY = this.world.getPlayerWorldY();

        let ret = this.world.searchForTileTypeReturnClosest(pX, pY, tiletype);
        if(tiletype2 != -1 && ret == false)
        {
            ret = this.world.searchForTileTypeReturnClosest(pX, pY, tiletype2);
            return this.world.setTileType(ret[0], ret[1], newtile2);   
        }
        return this.world.setTileType(ret[0], ret[1], newtile);
    }*/

    private movePlayerInWorld(dir : string) : void
    {
        let pX = this.world.getPlayerWorldX(this.pid), 
            pY = this.world.getPlayerWorldY(this.pid);

    	if(dir == 'left' && this.world.getTileAroundPlayer(this.pid, -1) != 0)
        { pX -= 1 }
        else if(dir == 'right' && this.world.getTileAroundPlayer(this.pid, 1) != 0)
        { pX += 1 }
        else if(dir == 'up' && this.world.getTileAroundPlayer(this.pid, 0, -1) != 0)
        { pY -= 1 }
        if(dir == 'down' && this.world.getTileAroundPlayer(this.pid, 0, 1) != 0)
        { pY += 1 }
        
        //Sets new position of player
        this.world.setPlayerWorldPos(this.pid, pX, pY);

        //Update the world with the new player position
        this.world.updateWorldDisplay();
    }
}