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

public class PlayersScrollPanelScript : MonoBehaviour
{
    public GameObject PlayerSettingPrefab;

    private int playerCount = 0;
    // Start is called before the first frame update
    void Start()
    {
        updateScrollViewCount(4);
    }

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

    public void updateScrollViewCount(float value) {
        playerCount = GetComponentsInChildren<PlayerSetting>().Length;
        if (playerCount < value) {
            for (int i = playerCount; i < value; i++) {
                GameObject go = Instantiate(PlayerSettingPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
                PlayerSetting ps = go.GetComponent<PlayerSetting>();
                ps.SetPlayerText(i + 1);
                ps.SetPlayerName(i + 1);
                
                ps.transform.SetParent(GameObject.Find("NewLocalGamePlayersContent").transform, false);
                ps.transform.SetSiblingIndex(i + 1);
            }
        } else if (playerCount > value) {
            for (int i = playerCount; i > value; i--) {
                PlayerSetting toDestroy = GameObject.Find("NewLocalGamePlayersContent").transform.GetChild(i - 1).GetComponent<PlayerSetting>();
                Destroy(toDestroy.gameObject);
            }
        }
        gameObject.GetComponent<ScrollRect>().verticalNormalizedPosition = 0f;
        playerCount = GetComponentsInChildren<PlayerSetting>().Length;
    }
}
