using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.UI;
using static UnityEditor.Progress;

public class RawImageSpawner : MonoBehaviour
{

    
    public RawImage image;
    public float positionX = 0;
    public float positionY = 0;
    public int width = 640;
    public int height = 480;
    public Dictionary<int, RawImage> connectedPlayers = new Dictionary<int, RawImage>();
    public Canvas canvas;
    public RectTransform rectTransform;
    RawImage myImage;
    private int key = 0;
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
       
        if (Input.GetKeyDown(KeyCode.Space))
        {
            myImage = Instantiate(image);
            myImage.transform.SetParent(rectTransform, false);
            
            connectedPlayers.Add(key++, myImage );
            foreach (var item in connectedPlayers)
            {
                Debug.Log($"The Keys-> {item.Key} the Values -> {item.Value.name}");
                
            }
           
           
        }
        if (Input.GetKeyDown(KeyCode.C))
        {
            //Debug.Log($"connectedPlayers -> {connectedPlayers.Count}");
            foreach (var item in connectedPlayers)
            {
                //myImage = item.Value;
                Debug.Log($"The remaining Keys-> {item.Key} the Values -> {item.Value.name}");


                // Destroy(item.Value.gameObject);
                //Debug.Log("Null");
            }

        }

        if (Input.GetKeyDown(KeyCode.R))
        {
            //if (connectedPlayers.Count > 0)
            {
                //Debug.Log($" The last key ->{ connectedPlayers.Keys.Last()}");
                //connectedPlayers.Remove(connectedPlayers.Keys.Last());
                connectedPlayers.Remove(6);
                //Debug.Log($"The remaining keys -> {connectedPlayers.Keys}");
                if (!connectedPlayers.ContainsKey(6))
                {
                    Destroy(myImage.gameObject);
                    Debug.Log($"connectedPlayers does not contain 6");

                    
                }
                
            }
            
        }
        
        
    }
}