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 hud.inventory.InvenHelper;
import hud.inventory.Inventory;
import interfaces.Colors;
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 Flower extends Entity implements GameObject  {

	private final int PICK_UP_DELAY = 45;
	
	private int type;
	private int pickUpTimer;

	public Flower(int x, int y) {
		this.pos = new Point2f(x, y);
		this.width = 23; // These are according to the sprite size;
		this.height = 8; // " "
		this.center = new Point2f((int) (pos.x + width / 2), (int) (pos.y + height / 2));
		this.radius = width / 1.2f;
		this.type = Utils.rand.nextInt(15);
		Handler.flowers.add(this);
	}

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

		if (alive) copy.drawImage(Textures.flowerSheet[type], (int) pos.x, (int) pos.y, null);
		else copy.drawImage(Textures.flowerSheet[15], (int) pos.x, (int) pos.y, null);
	
		if (Game.drawCollisionBounds && alive) {
			copy.setColor(Color.YELLOW);
			copy.drawOval((int) (center.x - radius), (int) (center.y - radius), (int) radius * 2, (int) radius * 2);
		}
		
		if(pickUpTimer > 0){
			copy.setColor(Colors.offBlack);
			copy.fillRect((int)pos.x, (int)pos.y - 14, 20, 4);
			copy.setColor(Color.green);
			copy.fillRect((int)pos.x + 1, (int)pos.y - 13, (int)(pickUpTimer / 2.3275), 2);
		}
		
		
		copy.dispose();
	}

	public void tick() {
		if(!checkIfAlive() || !getDiscoveryStatus()) return;
		
		if (Player.using && alive && playerCollided && !Inventory.isFull(InvenHelper.PICKED_FLOWERS, 1)) {
			if (++pickUpTimer >= PICK_UP_DELAY) {
			alive = false;
			Inventory.addItem(2, 1, 100);
			pickUpTimer = 0; }
		} else pickUpTimer = 0;
	}

}
