package world;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;

import _texture.loader.Textures;
import engine.Game;
import engine.Handler;
import engine.MouseInput;
import interfaces.Biomes;
import interfaces.GameObject;
import interfaces.Mob;
import player.MouseMovement;
import player.Player;
import utils.Animation;
import utils.Utils;

/*  ------------- Nihilne ------------
    * Created by Mitch Weaver - 2016 *
      ~~~~ All Rights Reserved ~~~~ */

public final class Tile implements GameObject, Biomes {
	
	private static final int TILE_SIZE = Game.TILE_SIZE; /* will need to be referenced a lot, cut down on external references */
	private static Animation[] animations;
	public static boolean drawHighLighted;
    public static final Point pointToCheck = new Point(0,0);
    public static final int radius = TILE_SIZE/2;

/*************************************************************************************************************************************************************************/
    public int x, y, biome;
    public boolean highlighted, discovered; // <--- discovered = fog of war
    public Rectangle bounds;
    public Tile(final int x, final int y, final int biome) {
    	this.x = x; this.y = y; this.biome = biome; 
    	bounds = new Rectangle(x, y, TILE_SIZE, TILE_SIZE); 
    }

    public final void tick() {
    	if(!discovered && Player.DISCOVERY.contains(x, y)) discovered = true;
    	else if(drawHighLighted) {
	    	if(MouseMovement.PLACING_STRUCTURE) { if(getBounds().contains(pointToCheck)) highlighted = true; else highlighted = false;}
	    	else if(getBounds().contains(MouseInput.adjustedMouseXY)) highlighted = true; else highlighted = false;
    	}
    }
    
    public final void render(Graphics2D g2d) {
    	if(highlighted){
        	g2d.drawImage(Textures.tileSheet[HIGHLIGHTED], x, y, null);
        	return;
    	} else if(discovered) animations[biome].drawAnimation(g2d, x, y);
    }
    
    public final Rectangle getBounds(){ return bounds; }
/***************************************************************************************************************************************************************************/
    
    public static final void init(){
    	animations = new Animation[18];
    	for(int i = 0; i < animations.length; i++) animations[i] = new Animation(0, Textures.tileSheet[i]); /* Fill array with default images */
    	
    	final int OCEAN_ANIMATION_SPEED = 25;
    	final int RIVER_ANIMATION_SPEED = 12;
    	final int LAKE_ANIMAITON_SPEED = 20;
    	final int VOLCANIC_ANIMATION_SPEED = 30;
    	
    	animations[OCEAN] = new Animation(OCEAN_ANIMATION_SPEED,
    			Textures.tileSheet[18], Textures.tileSheet[19], Textures.tileSheet[20], Textures.tileSheet[21],
    			Textures.tileSheet[22], Textures.tileSheet[23], Textures.tileSheet[24], Textures.tileSheet[25],
    			Textures.tileSheet[26], Textures.tileSheet[27], Textures.tileSheet[28], Textures.tileSheet[29],
    			Textures.tileSheet[30], Textures.tileSheet[31], Textures.tileSheet[32], Textures.tileSheet[33]);
    	
    	
    	animations[RIVER] = new Animation(RIVER_ANIMATION_SPEED, Textures.tileSheet[RIVER], Utils.reverseImage(Textures.tileSheet[RIVER]));
    	animations[LAKE] = new Animation(LAKE_ANIMAITON_SPEED, Textures.tileSheet[LAKE], Utils.reverseImage(Textures.tileSheet[LAKE]));
    	// VOLCANIC:
    	animations[VOLCANIC] = new Animation(VOLCANIC_ANIMATION_SPEED, Textures.tileSheet[VOLCANIC], Utils.reverseImage(Textures.tileSheet[VOLCANIC]));
    }
    
    public static final void runAnimations(){
    	animations[OCEAN].runAnimation();
    	animations[LAKE].runAnimation();
    	animations[RIVER].runAnimation();
    	animations[VOLCANIC].runAnimation();
    }
    
    public static final void resetAllHighlighted(){
    	for(int i = 0; i < Handler.sections.length; i++)
			for(int j = 0; j < Handler.sections[i].length; j++)
				for(Tile t : Handler.world.get(i).get(j).get(0))
					t.highlighted = false;
    }
    
    public static final void DISCOVER_ALL(){
    	for(int i = 0; i < Handler.sections.length; i++)
			for(int j = 0; j < Handler.sections[i].length; j++)
				for(Tile t : Handler.world.get(i).get(j).get(0)) {
					t.discovered = true; t.highlighted = false; } 
    	
    	for(final Sapling i : Handler.saplings) i.discovered = true;
    	for(final Tree i : Handler.trees) i.discovered = true;
    	for(final Flower i : Handler.flowers) i.discovered = true;
    	for(final TallGrass i : Handler.grass) i.discovered = true;
    	for(final Boulder i : Handler.boulders) i.discovered = true;
    	for(final Mob i : Handler.mobs) i.discovered = true;
    }
    
    public static final void UN_DISCOVER_ALL(){
    	for(int i = 0; i < Handler.sections.length; i++)
			for(int j = 0; j < Handler.sections[i].length; j++)
				for(final Tile t : Handler.world.get(i).get(j).get(0)) {
					t.discovered = false; t.highlighted = false; } 
    	
    	for(final Sapling i : Handler.saplings) i.discovered = false;
    	for(final Tree i : Handler.trees) i.discovered = false;
    	for(final Flower i : Handler.flowers) i.discovered = false;
    	for(final TallGrass i : Handler.grass) i.discovered = false;
    	for(final Boulder i : Handler.boulders) i.discovered = false;
    	for(final Mob i : Handler.mobs) i.discovered = false;
    }
    
}