using Godot;
using System;

public partial class IQEnemy : RigidBody2D
{	
	// Misc
	float GlobalTimer;
	
	// Enemy Stats
	public int IQ;
	public float HP;
	public float MaxHP;
	float RegenerationAmmount;
	float RegenerationInterval;
	
	// Enemy Conditions
	float CurrentYVelocity;
	float PreviousYVelocity;
	public bool IsGrounded;
	
	// Enemy Abilities
	bool AbilityJump = false;
	bool AbilityMove = false;
	bool AbilityAttack = false;
	bool AbilityDash = false;
	bool AbilityCloak = false;
	bool AbilityTeleport = false;
	bool AbilityRegenerate = false;
	
	public override void _Ready()
	{	
		// Read & Set Abilities
		if (HasMeta("AbilityJump")) AbilityJump = (bool)GetMeta("AbilityJump");
		if (HasMeta("AbilityMove")) AbilityMove = (bool)GetMeta("AbilityMove");
		if (HasMeta("AbilityAttack")) AbilityAttack = (bool)GetMeta("AbilityAttack");
		if (HasMeta("AbilityDash")) AbilityDash = (bool)GetMeta("AbilityDash");
		if (HasMeta("AbilityCloak")) AbilityCloak = (bool)GetMeta("AbilityCloak");
		if (HasMeta("AbilityTeleport")) AbilityTeleport = (bool)GetMeta("AbilityTeleport");
		if (HasMeta("AbilityRegenerate")) AbilityRegenerate = (bool)GetMeta("AbilityRegenerate");
		if (HasMeta("RegenerationAmmount")) RegenerationAmmount = (float)GetMeta("RegenerationAmmount");
			else RegenerationAmmount = 1.0f;
		if (HasMeta("RegenerationInterval")) RegenerationInterval = (float)GetMeta("RegenerationInterval");
			else RegenerationInterval = 1.0f;
			
		// Read and set stats
		if (HasMeta("MaxHP")) MaxHP = (float)GetMeta("MaxHP");
		
		// Set up ground detection
		GetNode<Area2D>("GroundDetector").AreaEntered += OnGroundEntered;
		GetNode<Area2D>("GroundDetector").AreaExited += OnGroundExited;
		
		IsGrounded = true;
		
		// Apply IQ based permenant modifiers
		IQ = (int)GetMeta("IQ"); // Read IQ
		
		if (IQ >= 100) MaxHP += 20;
		HP = MaxHP;
		if (IQ >= 100 && HasMeta("RegenerationInterval")) RegenerationInterval *= 0.9f;
		if (IQ >= 100 && HasMeta("RegenerationAmmount")) RegenerationAmmount *= 1.05f; // 5% increase
	}
	
	public override void _Process(double delta)
	{
		// Update Timer
		GlobalTimer = GetTree().Root.GetNode<CommonData>("CommonData").GlobalTimer;
		
		// Show current hp for testing purposes
		if (HasMeta("CurrentHP")) SetMeta("CurrentHP", HP);
		
		// Regenerate Health
		if (AbilityRegenerate){
			if (GlobalTimer % RegenerationInterval == 0){Regenerate(RegenerationAmmount);}
		}
		
	}
	
	void Flip(string facing){
		if (facing == "RIGHT") Scale = new Vector2(1,1);
		if (facing =="LEFT") Scale = new Vector2(-1,1);
	}
	
	public void Regenerate(float ammount){
		if (HP < MaxHP) HP += ammount;
		if (HP > MaxHP) HP = MaxHP; // Catch errors
	}
	
	public void Damage(float ammount){
		HP -= ammount;
		if (HP <= 0) Kill();
	}
	
	public void Kill(){
		QueueFree(); // Change to proper death w/ corpse
	}
	
	// Ground Detection
	public void OnGroundEntered(Area2D area){if (area.CollisionLayer == 0) IsGrounded = true;}
	public void OnGroundExited(Area2D area){if (area.CollisionLayer == 0) IsGrounded = false;}
}
