package electrosphere.server.pathfinding.blocker;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A cache of the blocker fields for terrain meshes
 */
public class NavTerrainBlockerCache {
    //Basic idea is we associate string that contains chunk x&y with elevation
    //While we incur a penalty with converting ints -> string, think this will
    //offset regenerating the array every time we want a new one
    int cacheSize = 50;
    Map<String, NavBlocker> navBlockerMapCache = new HashMap<String, NavBlocker>();
    List<String> navBlockerMapCacheContents = new ArrayList<String>();
    
    
    public String getKey(int x, int y){
        return x + "_" + y;
    }
    
    public NavBlocker getBlocker(int x, int y){
        NavBlocker rVal;
        String key = getKey(x,y);
        //if in cache
        if(navBlockerMapCache.containsKey(key)){
            navBlockerMapCacheContents.remove(key);
            navBlockerMapCacheContents.add(0, key);
            rVal = navBlockerMapCache.get(key);
            return rVal;
        } else {
            //else fetch from sql if available
            
        }
        //else, create an empty one I guess
        rVal = new NavBlocker();
        rVal.setHeightfieldBlockerValue(5,5,true);
        rVal.setHeightfieldBlockerValue(6,5,true);
        rVal.setHeightfieldBlockerValue(5,6,true);
        rVal.setHeightfieldBlockerValue(6,6,true);
        rVal.setHeightfieldBlockerValue(7,5,true);
        rVal.setHeightfieldBlockerValue(11,5,true);
        rVal.setHeightfieldBlockerValue(5,8,true);
        rVal.setHeightfieldBlockerValue(5,22,true);
        rVal.setHeightfieldBlockerValue(5,50,true);
        if(navBlockerMapCache.size() > cacheSize){
            String oldChunk = navBlockerMapCacheContents.remove(navBlockerMapCacheContents.size() - 1);
            navBlockerMapCache.remove(oldChunk);
        }
        navBlockerMapCacheContents.add(0, key);
        navBlockerMapCache.put(key, rVal);
        return rVal;
    }
    
}
