package world;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;

import _texture.loader.Textures;
import engine.Game;
import engine.Handler;
import fonts.fonts;
import hud.inventory.InvenHelper;
import interfaces.Entity;
import interfaces.GameObject;
import player.Player;
import utils.Point2f;
import utils.Utils;

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

public class Tree extends Entity implements GameObject {

	private int type;
	public float chopRadius;
	private long chopTimer;
	public Rectangle bounds;

	public Tree(int x, int y) {
		this.pos = new Point2f(x, y);
		this.width = 64; // These are according to the sprite size;
		this.height = 64;
		this.center = new Point2f((int) (x + width / 2), (int) (y + height / 1.1));
		this.radius = width / 15f;
		this.chopRadius = radius*3f;
		this.type = Utils.rand.nextInt(3) + 1; // randomly assign the tree one of the 3 sprites, (change with seasons)
		this.health = Utils.rand.nextInt(7) + 5; // randomly give tree a certain health
		this.chopTimer = System.currentTimeMillis();
		bounds = new Rectangle(x, y, width, height);
		Handler.trees.add(this);
	} public Tree(float x, float y){ new Tree((int)x, (int)y);};

	public void tick() {
		if(!getDiscoveryStatus()) return;
		
			if (playerCollided && Player.using && alive) {
				if (InvenHelper.isAxe(Player.equipmentSlots[3].itemId)) getChopped(Player.equipmentSlots[3].itemId);
			}

		switch (Game.rotation) { /* re-adjust center points */
			case 1: { center.x = (int) (pos.x + width / 2); center.y = (int) (pos.y + height / 1.025); break; }
			case 2: { center.x = (int) (pos.x + width / 1.225); center.y = (int) (pos.y + height / 1.175); break; }
			case 3: { center.x = (int) (pos.x + width / 1.0375); center.y = (int) (pos.y + height / 1.825); break; }
			case 4: { center.x = (int) (pos.x + width / 1.16); center.y = (int) (pos.y + height / 4.9); break; }
			case 5: { center.x = (int) (pos.x + width / 1.85); center.y = (int) (pos.y + height / 18); break; }
			case 6: { center.x = (int) (pos.x + width / 5); center.y = (int) (pos.y + height / 6); break; }
			case 7: { center.x = (int) (pos.x + width / 20); center.y = (int) (pos.y + height / 1.95); break; }
			case 8: { center.x = (int) (pos.x + width / 5.35); center.y = (int) (pos.y + height / 1.2); break; }
		}
		
		if (alive && health <= 0) { /* place logs in a random direction around tree */
			alive = false;
			DroppedItem.dropRandomAmountOfItemsWithOffset(InvenHelper.LOGS, 1, 3, 25, center.x, center.y, 100);
		}
	}

	private void getChopped(int axeType) {
		int chopDamage = 0;
		switch (axeType) {
			case InvenHelper.CRUDE_AXE: chopDamage = 1000; break;
			case 1337: chopDamage = 500; break;
		}

		if (System.currentTimeMillis() - chopTimer > chopDamage) {
			health--;
			Player.equipmentSlots[3].durability -= 2.5f;
			// TODO: play get hit animation
			chopTimer = System.currentTimeMillis();
		}
	}

	private Graphics2D copy;
	private AffineTransform transform = new AffineTransform();
	public void render(Graphics2D g2d) {
		if(!discovered) return;
		
		if (Game.drawCollisionBounds) {
			g2d.setColor(Color.GREEN);
			g2d.drawOval((int) (center.x - radius), (int) (center.y - radius), (int) radius * 2, (int) radius * 2);
			g2d.setColor(Color.MAGENTA);
			g2d.drawOval((int) (center.x - chopRadius), (int) (center.y - chopRadius), (int) chopRadius * 2, (int) chopRadius * 2);
			g2d.setColor(Color.yellow);
			g2d.draw(bounds);
		}
		if (Game.drawResourceNumbers) {
			g2d.setColor(Color.white);
			g2d.setFont(fonts.ponderosa20);
			g2d.drawString(Integer.toString(health), center.x, center.y);
		}
		
		
		transform.setToRotation(-Utils._45 * (Game.rotation - 1), pos.x + width/2, pos.y + height/2);
		copy = (Graphics2D) g2d.create();

		copy.transform(transform);

		if (alive) copy.drawImage(Textures.treeSheet[type], (int) pos.x, (int) pos.y, null);
		else copy.drawImage(Textures.treeSheet[0], (int) pos.x, (int) pos.y, null);
		
		copy.dispose();
	}
}
