/*
 * Copyright 2014 martin.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ucigame.example;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import java.util.HashSet;
import java.util.Set;
import org.javatuples.Pair;
import tile80.Console80;
import tile80.fsm80.Pulse;
import tile80.fsm80.State80;
import tile80.tile80.Tile80;
import tile80.world80.World80;
import tile80.world80.World80HOF;
import tool.Json;
import ucigame.Ucigame;
import ucigame.tile80.Console80UciGame;
import ucigame.tile80.Sprite80UciGame;

/**
 *
 * @author martin
 */
public class Demo extends Ucigame{
    ImmutableMap <String,Sprite80UciGame> mapSprite;
    Console80 console;
    World80 world;
    Set<String> event;
    State80 activate;
    int max;

    @Override
    public void setup()
    {
        activate = Pulse.make("spacebarDown", "spacebarUp");
        max =0;      
        window.size(1024, 512);
        window.title("move with arrow");
        window.showFPS();
        framerate(30);
        canvas.background(0);

        ImmutableMap tmp = ImmutableMap.builder()
                                .putAll(Sprite80UciGame.makeSpriteFactoryUciGame(this,Json.loadFileJson("data/sprite/abandonaut/tiles.json")))
                                .putAll(Sprite80UciGame.makeSpriteFactoryUciGame(this,Json.loadFileJson("data/sprite/abandonaut/astro.json")))
                                .putAll(Sprite80UciGame.makeSpriteFactoryUciGame(this, Json.loadFileJson("data/sprite/cursor.json")))
                                .putAll(Sprite80UciGame.makeSpriteFactoryUciGame(this, Json.loadFileJson("data/sprite/abandonaut/powerlight.json")))
                                .putAll(Sprite80UciGame.makeSpriteFactoryUciGame(this, Json.loadFileJson("data/sprite/abandonaut/beam.json")))
                                .build();
        mapSprite=tmp;

        console = new Console80UciGame(this, "Monospaced", 8, 255, 255, 255, 200);

        //world = World80.load(World80HOF.builder(),Json.loadFileJson("data/world/droidDemo.json")); 
        world = World80.load(World80HOF.builder(),Json.loadFileJson("data/world/doorDemo.json"));        
        event = new HashSet();

    }
    
    static Pair<Integer,Integer> topLeft = new Pair(0,0);
    static Pair<Integer,Integer> bottomRight = new Pair(128,64);
    @Override
    public void draw()
    {
        if ("on".equals(activate.name()))
            event.add("activate");
        activate=activate.next("update");
        world = world.crunch(event);
        event.clear();
        canvas.clear();
        for (Tile80 tile : world.getTileByRect(topLeft, bottomRight)){
            mapSprite.get(tile.getFromKeyspace("sprite"))
                     .makeSprite(tile.getX(), tile.getY())
                     .draw();
        }
        int cnt = Iterables.size(world.getTileLst());
        if (cnt>max){
            console.addMessage("max # tiles : "+cnt);
            max=cnt;
        }
        console.draw(0, 8, 3);
    }

    @Override
    public void onKeyRelease(){
        if (!keyboard.isDown(keyboard.SPACE)){
            activate=activate.next("spacebarUp");
        }
    }
    
    @Override
    public void onKeyPress(){
        if (keyboard.isDown(keyboard.SPACE)){
                activate=activate.next("spacebarDown");
        }
        if (keyboard.isDown(keyboard.UP, keyboard.W))
            event.add("up");
        if (keyboard.isDown(keyboard.DOWN, keyboard.S))
            event.add("down");
        if (keyboard.isDown(keyboard.LEFT, keyboard.A))
            event.add("left");
        if (keyboard.isDown(keyboard.RIGHT, keyboard.D))
            event.add("right");
     }

}
