package world;

import java.awt.Color;
import java.awt.Graphics2D;
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 Boulder extends Entity implements GameObject {
	
	private static final int MAX_HEALTH = 13;
	
	private int type, rocksAwarded;
	public float mineRadius;
	private long pickTimer;

	public Boulder(int x, int y) {
		this.pos = new Point2f(x, y);
		if (Utils.rand.nextBoolean()) {
			this.type = (Utils.rand.nextInt(4)) + 4;
			if (this.type == 4) {
				this.width = 49;
				this.height = 36;
			} else if (this.type == 5) {
				this.width = 52;
				this.height = 34;
			} else if (this.type == 6) {
				this.width = 40;
				this.height = 42;
			} else if (this.type == 7) {
				this.width = 53;
				this.height = 31;
			}
			
			rocksAwarded = Utils.rand.nextInt(5) + 3; health = MAX_HEALTH;
		} else { this.type = 0; rocksAwarded = Utils.rand.nextInt(3) + 1; health = 8;}
			
		this.center = new Point2f(this.pos.x + this.width / 2, this.pos.y + this.height / 2);
		this.radius = this.width / 2;
		this.mineRadius = this.radius*1.7f;
		this.alive = true;
		this.pickTimer = System.currentTimeMillis();
		Handler.boulders.add(this);
	}

	public void tick() {
		if(!checkIfAlive() || !getDiscoveryStatus()) return;
		
		if (playerCollided && Player.using) {
			if (InvenHelper.isPickaxe(Player.equipmentSlots[3].itemId)) getPicked(Player.equipmentSlots[3].itemId);
		}
		
		switch(health){
			case MAX_HEALTH: return;
			case 8: { type = 0; width = 28; height = 18; break; }
			case 6: { type = 1; width = 27; height = 17; break; }
			case 4: { type = 2; width = 18; height = 17; break; }
			case 2: { type = 3; width = 15; height = 18; break; }
			case 0: { 
				DroppedItem.dropItemsWithOffset(6, rocksAwarded, 25, center.x, center.y, 100);
				Handler.boulders.remove(this);
			}
		}
		
		center.setLocation(pos.x + width / 2, pos.y + height / 2);
		radius = width / 2; mineRadius = radius*1.7f;
	}
	
	private void getPicked(int axeType) {
		int chopDamage = 0;
		switch (axeType) {
			case 5: chopDamage = 1000; break;
			case 1337: chopDamage = 500; break;
		}

		if (System.currentTimeMillis() - pickTimer > chopDamage) {
			health = Utils.clamp(--health, 0, 13);
			Player.equipmentSlots[3].durability -= 2.5f;
			pickTimer = System.currentTimeMillis();
		}
	}

	private Graphics2D copy;
	private AffineTransform transform = new AffineTransform();
	public void render(Graphics2D g2d) {
		if(!discovered) return;
		if(!alive) return;
		copy = (Graphics2D) g2d.create(); transform.setToRotation(-Utils._45 * (Game.rotation - 1), pos.x + width / 2, pos.y + height / 2); copy.transform(transform);

		copy.drawImage(Textures.boulderSheet[type], (int)pos.x, (int)pos.y, null);
		
		
		if (Game.drawCollisionBounds) {
			g2d.setColor(Color.MAGENTA);
			g2d.drawOval((int) (center.x - radius), (int) (center.y - radius), (int) radius * 2, (int) radius * 2);
			g2d.setColor(Color.yellow);
			g2d.drawOval((int) (center.x - mineRadius), (int) (center.y - mineRadius), (int) mineRadius * 2, (int) mineRadius * 2);
		}
		
		if (Game.drawResourceNumbers) {
			g2d.setColor(Color.white);
			g2d.setFont(fonts.ponderosa20);
			g2d.drawString(Integer.toString(health), center.x, center.y);
		}
		
		copy.dispose();
	}

}
