/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.nplusc.iZc.MazeGame.cells;

import de.nplusc.iZc.MazeGame.Objects.Player;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;

/**
 *
 * @author LH
 */
public abstract class MazeCell
{
    public static final int DIRECTION_NORTH=0;
    public static final int DIRECTION_EAST=1;
    public static final int DIRECTION_SOUTH=2;
    public static final int DIRECTION_WEST=3;
    private Map<Integer,boolean[]> exits = new HashMap<>();//ints for layer and bools for directions
    private Map<Integer,boolean[]> entrances = new HashMap<>();//ints for layer and bools for directions
                                   //entrance but no exit ->onewaydoor
    
    private String TexturePath = "";
    private int direction=0;
    private int baseLayer=0,layerHeight=1; //wichtig bei stacked-tiles wie brücken, tunnels;

    public MazeCell(Map<Integer,boolean[]> pexits, Map<Integer,boolean[]> pentrances,int bl,int lh)
    {
        exits = pexits; entrances = pentrances;
        
    }
    
    public MazeCell(Map<Integer,boolean[]> pexits, Map<Integer,boolean[]> pentrances,int bl)
    {
        this(pexits,pentrances,bl,1);
    }
    
    
    public MazeCell()
    {
        
    }        
    
    
    
    
    public boolean canExit(int direction,int layer,Player p)
    {
        if(!exits.containsKey(layer-baseLayer))
        {
            return false;
        }
        return exits.get(layer-baseLayer)[direction];
    }
    
    public boolean canEnter(int direction,int layer,Player p)
    {
        if(!entrances.containsKey(layer-baseLayer))
        {
            return false;
        }
        return entrances.get(layer-baseLayer)[direction];
    }
    
    
    public int getDirection()  //setter ist serialisierungskram also nach unten verbannt
    {
        return direction;
    }
    
    
    //setTileImage() ->setTexturePath()
    
    
    public Image getTextureTile() 
    {
        Image i = null;
        try 
        {
            i = ImageIO.read(new File(TexturePath));
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        return i;
   }

    int getExitLayer(int direction) //geht da nur maximal ein exit pro richtung möglich
    {
        int ret = Integer.MIN_VALUE; //integer_minimum == kein exit in diese richtung
        Integer[] layers = exits.keySet().toArray(new Integer[]{0});
        for (int i = 0; i < layers.length; i++)
        {
            int k = layers[i];
            if(exits.get(k)[direction])
            {
                return k;
            }
        }
        return ret; //fallback bei kein exit verfügbar
    }
    //Serialisuerungskram#
    
    
    public int getBaseLayer()
    {
        return baseLayer;
    }

    public void setBaseLayer(int baseLayer)
    {
        this.baseLayer = baseLayer;
    }

    public int getLayerHeight()
    {
        return layerHeight;
    }

    public void setLayerHeight(int layerHeight)
    {
        this.layerHeight = layerHeight;
    }

    
    public void setDirection(int dir)
    {
        direction=dir;
    }
    public Map<Integer, boolean[]> getExits()
    {
        return exits;
    }

    public void setExits(Map<Integer, boolean[]> exits)
    {
        this.exits = exits;
    }

    public Map<Integer, boolean[]> getEntrances()
    {
        return entrances;
    }

    public void setEntrances(Map<Integer, boolean[]> entrances)
    {
        this.entrances = entrances;
    }

    public String getTexturePath()
    {
        return TexturePath;
    }

    public void setTexturePath(String TexturePath)
    {
        this.TexturePath = TexturePath;
    }
    
    
    public abstract String onCellEnter(Player p); //interface für msgs

    public boolean requiresFOV() // ob aufgedeckt oder nicht wird im player gespeichert
    {
        return true;
    }
    
}
