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

public class ZoneInfo : MonoBehaviour
{
    public int id;
    public Zone zone;
    public ScrollRect scrollRect;
    public Text text;
    public Dictionary<uint, GameObject> users = new Dictionary<uint, GameObject>();

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void AddWorker(User user)
    {
        if (!users.ContainsKey(user.id))
        {
            var user_name = Instantiate(text);
            user_name.fontSize = 12;
            user_name.transform.parent = scrollRect.content.transform;
            user_name.text = user.name;
            users[user.id] = user_name.gameObject;
        }
    }

    public void DeleteWorker(uint user_id)
    {
        if (users.ContainsKey(user_id))
        {
            var go = users[user_id];
            Destroy(go);
            users.Remove(user_id);
        }
    }

    public void UserDetectPosition(User user, Structure user_pos)
    {
        var coord = zone.Coordinates();
        if (coord[0] < user_pos.coord_x && coord[2] > user_pos.coord_x && coord[1] < user_pos.coord_y && coord[3] > user_pos.coord_y)
        {
            AddWorker(user);
        }
        else DeleteWorker(user.id);
    }
}
