using UnityEngine;

public enum CharacterType { Playable, NPC, Enemy, None }

public class CharacterIdentity : MonoBehaviour
{
    [SerializeField] private string _characterCode;
    public string CharacterCode { get { return _characterCode; } private set { _characterCode = value; } }
    public bool IsControlling { get; private set; } = false;
    public bool IsDefaultControl { get; set;}

    public CharacterType GetCharacterType()
    {
        string CharacterTypeStr = null;

        if (DataManager.Instance.PlayableCharacterData.TryGetValue(_characterCode, out var c1)) CharacterTypeStr = c1.CharacterType;
        if (DataManager.Instance.EnemyCharacterData.TryGetValue(_characterCode, out var c2)) CharacterTypeStr = c2.CharacterType;
        if (DataManager.Instance.NpcCharacterData.TryGetValue(_characterCode, out var c3)) CharacterTypeStr = c3.CharacterType;

        if (CharacterTypeStr == "PLAYABLE")
            return CharacterType.Playable;

        if (CharacterTypeStr == "ENEMY")
            return CharacterType.Enemy;

        if (CharacterTypeStr == "NPC")
            return CharacterType.NPC;

        return CharacterType.None;
    }

    public void SynchronizeControll()
    {
        if (GetCharacterType() != CharacterType.Playable) return;

        // 정렬없이 씬의 모든 PlayerCharacterController 검색
        PlayerCharacterController[] pccs = Object.FindObjectsByType<PlayerCharacterController>(FindObjectsSortMode.None);

        //모든 컨트롤을 끄고 자기 자신만 킴으로써 IsControlling은 한개체만 true임
        foreach (PlayerCharacterController pcc in pccs)
        {
            pcc.gameObject.GetComponent<CharacterIdentity>().IsControlling = false;
        }
        this.IsControlling = true;
    }
}
