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

public class CharacterMovement : MonoBehaviour {

    public float Speed;
    GameObject localPlayer;
    public List<GameObject> Players = new List<GameObject>();
    List<bool> Nearby = new List<bool>();

    public float DistanceToWin;

    Camera cam;

    public AudioSource Cheer;

    private void Start()
    {
        localPlayer = Multiplayer.PlayersManager.Instance.LocalPlayer;
        Players.Add(localPlayer);
        Nearby.Add(false);
        foreach (GameObject curPlayer in Multiplayer.PlayersManager.Instance.RemotePlayers.Values)
        {
            Players.Add(curPlayer);
            Nearby.Add(false);
        }
        cam = localPlayer.GetComponentInChildren<Camera>();
          
    }

    public bool CheckNearby()
    {
        for (int i = 0; i < Players.Count; i++)
        {
                    bool found = false;
            if ((localPlayer.transform.position - Players[i].transform.position).magnitude < DistanceToWin)
            {
                Greet();
                found = true;
            }
            else
            {
                Player playerScript = Players[i].GetComponent<Player>();
                foreach (GameObject dummy in playerScript.dummies)
                {
                    if ((localPlayer.transform.position - dummy.transform.position).magnitude < DistanceToWin)
                    {
                        Greet();
                        found = true;
                    }
                }
            }
            Nearby[i] = found;
        }
        return CheckWin();
    }

    bool CheckWin()
    {
        for (int i = 0; i < Nearby.Count; i++)
        {
            if (Nearby[i] == false)
            {
                return false;
            }
        }
        return true;
    }

    public void Greet()
    {
        if (Cheer != null)
        {
            Cheer.Play();
        }
        //Trigger animation
    }
}