using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEngine;
using UnityEngine.TextCore.Text;

public class SearchTrigger : MonoBehaviour
{
    public Character owner;

    public List<BeSearchTrigger> trigObjList;

    private void Awake()
    {
        owner = GetComponentInParent<Character>();
    }

    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 bool HasTargetType(List<TargetType> targetTypes, TargetType targetType)
    {
        for (int i = 0; i < targetTypes.Count; i++)
        {
            if (targetTypes[i] == targetType)
            {
                return true;
            }
        }
        return false;
    }

    public List<Character> GetAllTargets(List<TargetType> targetTypes, bool canHitFly)
    {
        List<Character> list = new List<Character>();
        for (int i = 0; i < trigObjList.Count; i++)
        {
            if (trigObjList[i].owner.tag == "Demonic" && HasTargetType(targetTypes, TargetType.Demonic)
                || trigObjList[i].owner.tag == "Tower" && HasTargetType(targetTypes, TargetType.Tower)
                || trigObjList[i].owner.tag == "Player" && HasTargetType(targetTypes, TargetType.Player)
                || trigObjList[i].owner.tag == "Enemy" && HasTargetType(targetTypes, TargetType.Enemy)
                || trigObjList[i].owner.tag == "EnemyTower" && HasTargetType(targetTypes, TargetType.EnemyTower))
            {
                Character character = trigObjList[i].owner;
                if (!character.gameObject.activeInHierarchy || character.isDie)
                {
                    continue;
                }
                if (owner == character)
                {
                    continue;
                }
                if (trigObjList[i].owner.tag == "Demonic")
                {
                    Demonic demonic = trigObjList[i].owner as Demonic;
                    if (demonic.canFly && !canHitFly)
                    {
                        continue;
                    }
                }
                if (trigObjList[i].owner.tag == "Enemy")
                {
                    Enemy enemy = trigObjList[i].owner as Enemy;
                    if (enemy.canFly && !canHitFly)
                    {
                        continue;
                    }
                }
            }
            else
            {
                continue;
            }
            list.Add(trigObjList[i].owner);
        }
        return list;
    }

    public Character GetMinDisTarget(List<TargetType> targetTypes, bool canHitFly)
    {
        List<Character> list = GetAllTargets(targetTypes, canHitFly);

        Character minDisChar = null;
        for (int i = 0; i < list.Count; i++)
        {
            Character character = list[i];
            if (minDisChar == null || Mathf.Abs(minDisChar.transform.position.x - owner.transform.position.x) > Mathf.Abs(character.transform.position.x - owner.transform.position.x))
            {
                minDisChar = character;
            }
        }
        return minDisChar;
    }

    public Character GetMinHPCharacter(List<TargetType> targetTypes, bool canHitFly)
    {
        List<Character> list = GetAllTargets(targetTypes, canHitFly);

        Character minHPChar = null;
        for (int i = 0; i < list.Count; i++)
        {
            Character character = list[i];
            if (minHPChar == null || minHPChar.hp > character.hp)
            {
                minHPChar = character;
            }
        }
        return minHPChar;
    }
}
