class BuildingType 
{
    static HOUSE = 0;
    static WATER = 1;
    static FARM = 2;
    static POLICE = 3;
    static COMMUNITY_CENTER = 4;
}

class BuildingManager 
{
    constructor( engine , log )
    {
        this.engine = engine;
        this.log = log;
        this.buildings = [];
    }

    restart()
    {
        for ( let i = 0 ; i < this.buildings.length ; ++i ) this.buildings[i].delete();
        this.buildings = [];

        this.buildings.push( new CommunityCenter( this.engine , 5 , 1 ) );
        this.log.setObstructed( 5 , 1 , true );
    }

    build( x , z )
    {
        if ( game.selectedBuilding === BuildingType.HOUSE )
        {
            if ( game.money >= Config.HOUSE_PRICE )
            {
                this.buildings.push( new House( this.engine , x , z ) );
                game.log.setObstructed( x , z , true );
                game.b_buildingMode = false;
                game.log.b_drawHoverQuad = false;
                game.money -= Config.HOUSE_PRICE;
                ui.updateGameUI();
                sound_build.play();
            }
            else
            {
                ui.showPopup( "Not enough money." );
            }
        }
        else if ( game.selectedBuilding === BuildingType.WATER )
        {
            if ( game.money >= Config.WATER_PRICE )
            {
                this.buildings.push( new RainwaterCollector( this.engine , x , z ) );
                game.log.setObstructed( x , z , true );
                game.b_buildingMode = false;
                game.log.b_drawHoverQuad = false;
                game.money -= Config.WATER_PRICE;
                ui.updateGameUI();
                sound_build.play();
            }
            else
            {
                ui.showPopup( "Not enough money." );
            }
        }
        else if ( game.selectedBuilding === BuildingType.FARM )
        {
            if ( game.money >= Config.FARM_PRICE )
            {
                this.buildings.push( new Farm( this.engine , x , z ) );
                game.log.setObstructed( x , z , true );
                game.b_buildingMode = false;
                game.log.b_drawHoverQuad = false;
                game.money -= Config.FARM_PRICE;
                ui.updateGameUI();
                sound_build.play();
            }
            else
            {
                ui.showPopup( "Not enough money." );
            }
        }
        else if ( game.selectedBuilding === BuildingType.POLICE )
        {
            if ( game.money >= Config.POLICE_PRICE )
            {
                this.buildings.push( new Police( this.engine , x , z ) );
                game.log.setObstructed( x , z , true );
                game.b_buildingMode = false;
                game.log.b_drawHoverQuad = false;
                game.money -= Config.POLICE_PRICE;
                ui.updateGameUI();
                sound_build.play();
            }
            else
            {
                ui.showPopup( "Not enough money." );
            }
        }
    }

    demolish( building )
    {
        for ( let i = 0 ; i < this.buildings.length ; ++i )
        {
            if ( this.buildings[i] === building )
            {
                sound_demolish.play();
                this.buildings[i].delete();
                game.log.setObstructed( this.buildings[i].x , this.buildings[i].z , false );
                this.buildings.splice( i , 1 );
                return;
            }
        }
    }

    buildingClicked( building )
    {
        if ( game.state === GameState.GAME )
        {
            if ( !game.b_buildingMode )
            {
                console.log( "building clicked" );
                console.log( building );
                ui.buildingWindow.show( building );
            }
        }
    }

    updateWaterFillLevel()
    {
        for ( let i = 0 ; i < this.buildings.length ; ++i )
        {
            if ( this.buildings[i].type === BuildingType.WATER )
            {
                this.buildings[i].updateWaterFillLevel();
            }
        }
    }

    update( dt )
    {

    }

    draw()
    {
        for ( let i = 0 ; i < this.buildings.length ; ++i )
        {
            this.buildings[i].draw();
        }
    }
}