using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Detection : MonoBehaviour
{

    [SerializeField]
    private Entity _entityLinked;

    //If it's a projectile damage > 0
    private int _projectileDamage = 0;

    void OnTriggerEnter2D(Collider2D other)
    {
        //Projectiles detection + damage deal
        if(_projectileDamage > 0 && other.gameObject.GetComponent<Entity>() == _entityLinked) {

            other.gameObject.GetComponent<Entity>().Hp -= _projectileDamage;
            //Kill if no hp
            if(other.gameObject.GetComponent<Entity>().Hp <= 0) {
                Destroy(other.gameObject);
            }

        }
    }

    void OnTriggerStay2D(Collider2D other) {

        if(_entityLinked.Enemy == null) {
            //Detect the enemy and inform the Ally
            if (other.gameObject.tag == "Opponent" && _entityLinked is Ally) {
                _entityLinked.IsEnemyDetected = true;
                _entityLinked.Enemy = other.gameObject.GetComponent<Entity>();
            }

            //Detect the enemy and inform the Opponent
            if (other.gameObject.tag == "Ally" && _entityLinked is Opponent) {
                _entityLinked.IsEnemyDetected = true;
                _entityLinked.Enemy = other.gameObject.GetComponent<Entity>();
            }
        }
                    
    }

    void OnTriggerExit2D(Collider2D other)
    {
        if(_projectileDamage == 0) {
            if ((other.gameObject.tag == "Opponent" && _entityLinked is Ally) || (other.gameObject.tag == "Ally" && _entityLinked is Opponent)) {
                _entityLinked.IsEnemyDetected = false;
            }
        }
        
    }

    //Getter and Setter
    public Entity EntityLinked
    {
        get { return _entityLinked; }
        set { _entityLinked = value; }
    }

    public int ProjectileDamage
    {
        get { return _projectileDamage; }
        set { _projectileDamage = value; }
    }
    
}
