using Base.Common;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DashEffect : MonoBehaviour
{
    public bool canHit;
    public bool isEnemy;
    public AttackInfo attackInfo;
    public List<Character> beHitTriggers = new List<Character>();
    public float offset;
    public GameObject rushEffect;
    public float targetY;
    public GameObject aim;
    private void Awake()
    {
        if (isEnemy)
        {
            attackInfo = GetComponentInParent<Enemy>().attack1Infos[0];
        }
        else
        {
            attackInfo = GetComponentInParent<Demonic>().attack1Infos[0];
        }
        
    }
    private void Update()
    {
        if (canHit)
        {
            for (int i = 0; i < beHitTriggers.Count; i++)
            {
                DashAttack(beHitTriggers[i]);
            }
            beHitTriggers = new List<Character>();
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        
        if(!isEnemy && other.gameObject.layer == 8 && other.name == "BodyCollider")
        {
            Character character = other.GetComponentInParent<Character>();
            if (!beHitTriggers.Exists(t => t == character))
            {
                beHitTriggers.Add(character);
            }
        }
        if (isEnemy && (other.gameObject.layer == 6|| other.gameObject.layer == 7)
            && other.name == "BodyCollider")
        {
            Character character = other.GetComponentInParent<Character>();
            if(!beHitTriggers.Exists(t => t == character))
            {
                beHitTriggers.Add(character);
            }
        }
    }
    private void OnTriggerExit(Collider other)
    {
        if (!isEnemy && other.gameObject.layer == 8 && other.name == "BodyCollider")
        {
            Character character = other.GetComponentInParent<Character>();
            if (beHitTriggers.Exists(t => t == character))
            {
                beHitTriggers.Remove(character);
            }
            
        }
        if (isEnemy && (other.gameObject.layer == 6 || other.gameObject.layer == 7)
            && other.name == "BodyCollider")
        {
            Character character = other.GetComponentInParent<Character>();
            if (beHitTriggers.Exists(t => t == character))
            {
                beHitTriggers.Remove(character);
            }

        }
    }
    public void DashAttack(Character character)
    {
        if(character.state == CharacterState.Die)
        {
            return;
        }
        character.BeHit(attackInfo.damage,attackInfo.force * attackInfo.attackDir * offset, 
            attackInfo.changeHurt,attackInfo.repelValue);
        GameObject effect = Instantiate(rushEffect);
        effect.transform.position = new Vector3(character.transform.position.x,
            transform.position.y + targetY, 0);
        effect.transform.position =
            Vector3.Lerp(character.transform.position + Vector3.up *targetY,
            aim.transform.position, 0.5f);
    }
}
