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

public class SearchTrigger : MonoBehaviour
{
    public GameObject owner;

    public List<BeSearchTrigger> trigObjList;

    private void Awake()
    {
        if (GetComponentInParent<PlayerController>())
        {
            owner = GetComponentInParent<PlayerController>().gameObject;
        }
        else if (GetComponentInParent<Demonic>())
        {
            owner = GetComponentInParent<Demonic>().gameObject;
        }
        else if (GetComponentInParent<Tower>())
        {
            owner = GetComponentInParent<Tower>().gameObject;
        }
        else if (GetComponentInParent<Enemy>())
        {
            owner = GetComponentInParent<Enemy>().gameObject;
        }
        else if (GetComponentInParent<EnemyTower>())
        {
            owner = GetComponentInParent<EnemyTower>().gameObject;
        }
    }

    private void FixedUpdate()
    {
        for (int i = 0; i < trigObjList.Count; i++)
        {
            if (trigObjList[i] == null || !trigObjList[i].enabled || !trigObjList[i].gameObject.activeInHierarchy)
            {
                trigObjList.RemoveAt(i);
                i--;
            }
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.GetComponent<BeSearchTrigger>())
        {
            trigObjList.Add(other.GetComponent<BeSearchTrigger>());
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.GetComponent<BeSearchTrigger>())
        {
            trigObjList.Remove(other.GetComponent<BeSearchTrigger>());
        }
    }

    public Demonic GetMinDisDemonic(bool canHitFly, out float dis)
    {
        Demonic minDisDemonic = null;
        dis = 99999;
        for (int i = 0; i < trigObjList.Count; i++)
        {
            if (trigObjList[i].owner.tag == "Demonic")
            {
                Demonic demonic = trigObjList[i].GetComponentInParent<Demonic>();
                if (owner.tag == "Demonic" && owner.GetComponent<Demonic>() == demonic)
                {
                    continue;
                }
                if (demonic.canFly && !canHitFly)
                {
                    continue;
                }
                if (minDisDemonic == null || Mathf.Abs(minDisDemonic.transform.position.x - owner.transform.position.x) > Mathf.Abs(demonic.transform.position.x - owner.transform.position.x))
                {
                    minDisDemonic = demonic;
                    dis = Mathf.Abs(demonic.transform.position.x - owner.transform.position.x);
                }
            }
        }
        return minDisDemonic;
    }

    public Demonic GetMinHPDemonic(bool canHitFly)
    {
        Demonic minHPDemonic = null;
        for (int i = 0; i < trigObjList.Count; i++)
        {
            if (trigObjList[i].owner.tag == "Demonic")
            {
                Demonic demonic = trigObjList[i].GetComponentInParent<Demonic>();
                if (owner.tag == "Demonic" && owner.GetComponent<Demonic>() == demonic)
                {
                    continue;
                }
                if (demonic.canFly && !canHitFly)
                {
                    continue;
                }
                if (minHPDemonic == null || minHPDemonic.hp > demonic.hp)
                {
                    minHPDemonic = demonic;
                }
            }
        }
        return minHPDemonic;
    }

    public Tower GetMinDisTower(out float dis)
    {
        Tower minDisTower = null;
        dis = 99999;
        for (int i = 0; i < trigObjList.Count; i++)
        {
            if (trigObjList[i].owner.tag == "Tower")
            {
                Tower tower = trigObjList[i].GetComponentInParent<Tower>();
                if (owner.tag == "Tower" && owner.GetComponent<Tower>() == tower)
                {
                    continue;
                }
                if (minDisTower == null || Mathf.Abs(minDisTower.transform.position.x - owner.transform.position.x) > Mathf.Abs(tower.transform.position.x - owner.transform.position.x))
                {
                    minDisTower = tower;
                    dis = Mathf.Abs(tower.transform.position.x - owner.transform.position.x);
                }
            }
        }
        return minDisTower;
    }

    public PlayerController GetPlayer(out float dis)
    {
        dis = 99999;
        for (int i = 0; i < trigObjList.Count; i++)
        {
            if (trigObjList[i].owner.tag == "Player")
            {
                PlayerController player = trigObjList[i].GetComponentInParent<PlayerController>();
                dis = Mathf.Abs(player.transform.position.x - owner.transform.position.x);
                return player;
            }
        }
        return null;
    }


    public Enemy GetMinDisEnemy(bool canHitFly)
    {
        Enemy minDisEnemy = null;
        for (int i = 0; i < trigObjList.Count; i++)
        {
            if (trigObjList[i].owner.tag == "Enemy")
            {
                Enemy enemy = trigObjList[i].GetComponentInParent<Enemy>();
                if (owner.tag == "Enemy" && owner.GetComponent<Enemy>() == enemy)
                {
                    continue;
                }
                if (enemy.canFly && !canHitFly)
                {
                    continue;
                }
                if (minDisEnemy == null || Mathf.Abs(minDisEnemy.transform.position.x - owner.transform.position.x) > Mathf.Abs(enemy.transform.position.x - owner.transform.position.x))
                {
                    minDisEnemy = enemy;
                }
            }
        }
        return minDisEnemy;
    }

    public Enemy GetMinHPEnemy(bool canHitFly)
    {
        Enemy minHPEnemy = null;
        for (int i = 0; i < trigObjList.Count; i++)
        {
            if (trigObjList[i].owner.tag == "Enemy")
            {
                Enemy enemy = trigObjList[i].GetComponentInParent<Enemy>();
                if (owner.tag == "Enemy" && owner.GetComponent<Enemy>() == enemy)
                {
                    continue;
                }
                if (enemy.canFly && !canHitFly)
                {
                    continue;
                }
                if (minHPEnemy == null || minHPEnemy.hp > enemy.hp)
                {
                    minHPEnemy = enemy;
                }
            }
        }
        return minHPEnemy;
    }

    public EnemyTower GetMinDisEnemyTower()
    {
        EnemyTower minDisEnemyTower = null;
        for (int i = 0; i < trigObjList.Count; i++)
        {
            if (trigObjList[i].owner.tag == "EnemyTower")
            {
                EnemyTower enemyTower = trigObjList[i].GetComponentInParent<EnemyTower>();
                if (owner.tag == "EnemyTower" && owner.GetComponent<EnemyTower>() == enemyTower)
                {
                    continue;
                }
                if (minDisEnemyTower == null || Mathf.Abs(minDisEnemyTower.transform.position.x - owner.transform.position.x) > Mathf.Abs(enemyTower.transform.position.x - owner.transform.position.x))
                {
                    minDisEnemyTower = enemyTower;
                }
            }
        }
        return minDisEnemyTower;
    }
}
