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

public class CharacterListController : MonoBehaviour
{
    public GameObject togglePrefab;
    public StringSharedField playerAccountName;

    public Transform listParent;
    public Button confirmButton;

    GameObject characterToggleGO;
    CharacterToggleInstance characterToggle;

    PlayerAccountData playerAccount;
    CharacterData characterData;

    private void Awake()
    {
        playerAccount = PlayerDataHandler.Instance.LoadPlayerAccountData(playerAccountName.Value);
    }

    private void Start()
    {
        if (playerAccount != null)
        {
            if (playerAccount.characterNames.Count > 0)
            {
                GenerateCharacterListFromAccountData(playerAccount);
                confirmButton.interactable = true;
            }
            else
                confirmButton.interactable = false;
        }
        else
        {
            playerAccount = new PlayerAccountData(playerAccountName.Value);
            PlayerDataHandler.Instance.SavePlayerAccountData(playerAccountName.Value, playerAccount);
            confirmButton.interactable = false;
        }

    }


    private void GenerateCharacterListFromAccountData(PlayerAccountData accountData)
    {
        foreach (string characterName in accountData.characterNames)
        {
            characterData = PlayerDataHandler.Instance.LoadCharacterData(playerAccountName.Value, characterName);
            Debug.Log("CD " + characterData);
            Debug.Log("CD " + (characterData == null).ToString());
            Debug.Log("CD " + characterData.characterClass);
            Debug.Log("CD " + characterData.characterName);
            Debug.Log("CD " + characterData.playerOwnerID);

            characterToggleGO = Instantiate(togglePrefab, listParent);
            characterToggle = characterToggleGO.GetComponent<CharacterToggleInstance>();

            characterToggle.InitializeCharacterToggle(characterData);

        }
    }
}
