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

public class SprintLinkTrigger : MonoBehaviour
{
    public GameObject enemyRopePrefab;
    public GameObject playerRopePrefab;
    public Dictionary<Enemy, bool> linkedEnemyDic;
    public List<Enemy> linkedEnemy;

    private void Awake()
    {
        linkedEnemyDic = new Dictionary<Enemy, bool>();
        linkedEnemy = new List<Enemy>();
    }

    private void OnTriggerEnter(Collider other)
    {
        BeSearchTrigger beSearchTrigger = other.GetComponent<BeSearchTrigger>();
        if (!beSearchTrigger)
        {
            return;
        }
        Enemy enemy = other.GetComponentInParent<Enemy>();
        if (!enemy || linkedEnemyDic.ContainsKey(enemy))
        {
            return;
        }
        if (linkedEnemy.Count > 0)
        {
            LinkEnemy(enemy, linkedEnemy[linkedEnemy.Count - 1]);
        }
        LinkPlayer(enemy);
    }

    public void LinkEnemy(Enemy enemy1, Enemy enemy2)
    {
        GameObject ropeObj = PoolManager.Instantiate(enemyRopePrefab);
        CharacterRope rope = ropeObj.GetComponent<CharacterRope>();
        rope.BeLink(enemy1, enemy2);
    }

    public void LinkPlayer(Enemy enemy)
    {
        linkedEnemyDic.Add(enemy, true);
        linkedEnemy.Add(enemy);
        PlayersInput.instance[0].playerRope.BeLink(enemy);
    }
}
