﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterSelector : MonoBehaviour
{
    const string UNLOCK_TOKEN = "_Unlocked";

    public GameObject[] characterPrefabs;
    bool[] unlocked;

    private void Start()
    {
        unlocked = new bool[characterPrefabs.Length];
        UpdateLocks();
    }

    public void UpdateLocks()
    {
        for (int i = 0; i < characterPrefabs.Length; i++)
        {
            unlocked[i] = PlayerPrefs.GetInt(characterPrefabs[i].name + UNLOCK_TOKEN, 0) > 0;
        }
    }

    public void Unlock(GameObject prefab)
    {
        PlayerPrefs.SetInt(prefab.name + UNLOCK_TOKEN, 1);
        UpdateLocks();
    }

    public void Lock(GameObject prefab)
    {
        PlayerPrefs.SetInt(prefab.name + UNLOCK_TOKEN, 0);
        UpdateLocks();
    }

    private void OnGUI()
    {
        if (GUILayout.Button("Clear"))
        {
            foreach(var prefab in characterPrefabs)
            {
                Lock(prefab);
            }
        }

        for (int i = 0; i < characterPrefabs.Length; i++)
        {
            GUILayout.BeginHorizontal();
            {
                GUILayout.Label(characterPrefabs[i].name, GUILayout.Width(120));
                if (unlocked[i])
                {
                    if (GUILayout.Button("Select"))
                        Debug.Log("Selected: " + characterPrefabs[i]);
                }
                else
                {
                    GUILayout.Label("[LOCKED]");
                    if (GUILayout.Button("Buy"))
                    {
                        Unlock(characterPrefabs[i]);
                    }
                }

            }
            GUILayout.EndHorizontal();
        }
    }
}
