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

public class LoadLevels : MonoBehaviour
{
    private int[] shipLevels;
    private int[] moonLevels;

    public GameObject shipLevelsTemplate;
    public GameObject moonLevelsTemplate;

    public void LoadShip() 
    {
        shipLevels = new Levels(SaveSystem.LoadLevels()).GetShipProgress();
        moonLevels = new Levels(SaveSystem.LoadLevels()).GetMoonProgress();
        LoadAllLevelTemplates();
    }

    private void LoadAllLevelTemplates() 
    {
        int counter = 0;
        foreach(Transform child in shipLevelsTemplate.transform) 
        {
            ResetAndSetCorrectImage(shipLevels[counter], child.gameObject);
            counter++;
        }

        counter = 0;
        foreach(Transform child in moonLevelsTemplate.transform) 
        {
            ResetAndSetCorrectImage(moonLevels[counter], child.gameObject);
            counter++;
        }

    }

    private void ResetAndSetCorrectImage(int stateNumber, GameObject level) 
    {
        List<GameObject> states =  new List<GameObject>();
        foreach(Transform child in level.transform) 
        {
            states.Add(child.gameObject);
        }

        int counter = 0;
        foreach(GameObject state in states) 
        {
            if (state.name != "Text")
                state.SetActive(false);
            if (counter == stateNumber)
                state.SetActive(true);
            counter++;
        }

        if (stateNumber == 0) 
        {
            level.GetComponent<Button>().interactable = false;
        }
    }

}
