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

public class Member : MonoBehaviour
{
    public static readonly Dictionary<string, Member> AllMembers;
    
	[Header("Boy")]
    public string Name;
    
	[Header("Settings")]
	public float MoveTime = .1f;
    
	[Header("Runtime")]
    public Vector3 InitialSpawnPos;

    protected Team myTeam;
    protected Player myPlayer;

	Rigidbody rb;
	Vector3 targetPos;
	Vector3 velocity;

	static Member()
	{
		AllMembers = new Dictionary<string, Member>();
	}

    void Awake()
    {
		rb = GetComponent<Rigidbody>();
    }

    public void Init(string name, Team t, Player p)
    {
        Name = name;
        myTeam = t;
        myPlayer = p;

        GetComponentInChildren<Renderer>().material.color = p.myColor;
        AllMembers.Add(name, this);

		targetPos = transform.position;
		velocity = Vector3.zero;

		GetComponentInChildren<Text>().text = name;
    }

	void FixedUpdate()
	{
		var newPos = Vector3.SmoothDamp(rb.position, targetPos, ref velocity, .1f, 2f, Time.fixedDeltaTime);

		rb.MovePosition(newPos);
	}

    public void Move(Vector2 direction)
    {
		targetPos += new Vector3(direction.x, 0f, direction.y);;

		if(targetPos.x > 3)
		{
			targetPos.x = 3.5f;
		}
		else if(targetPos.x < -3)
		{
			targetPos.x = -3.5f;
		}

		if(targetPos.z > 6)
		{
			targetPos.z = 6.5f;
		}
		else if(targetPos.z < -6)
		{
			targetPos.z = -6.5f;
		}
    }
}
