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

public class PlanetRequirementDisplay : MonoBehaviour
{
  public Sprite DogIcon;
  public Sprite CatIcon;
  public Sprite FrogIcon;
  public Sprite RabbitIcon;

  // Start is called before the first frame update
  void Start()
  {

  }

  // Update is called once per frame
  void Update()
  {
    TargetPlanet tp = this.GetComponentInParent<TargetPlanet>();

    int currentCount = this.GetComponentsInChildren<Image>().Length;
    int diff = currentCount - tp.RequiredCrew.Count;

    Image[] images;
    if (diff > 0)
    {
      images = this.GetComponentsInChildren<Image>();
      for (int i = 0; i < diff; i++)
      {
        Destroy(images[images.Length - 1 - i].gameObject);
      }
    }
    else if (diff < 0)
    {
      for (int i = 0; i > diff; i--)
      {
        GameObject go = new GameObject();
        Image sr = go.AddComponent<Image>();
        go.AddComponent<CanvasRenderer>();

        go.transform.SetParent(this.gameObject.transform, false);

        RectTransform rt = ((RectTransform)go.transform);
        rt.sizeDelta = new Vector2(10f, 10f);
      }
    }

    images = this.GetComponentsInChildren<Image>();
    for (int index = 0; index < tp.RequiredCrew.Count; index++)
    {
      Crew.CrewType ct = tp.RequiredCrew[index];
      switch (ct)
      {
        case Crew.CrewType.Dog:
          images[index].sprite = DogIcon;
          break;
        case Crew.CrewType.Cat:
          images[index].sprite = CatIcon;
          break;
        case Crew.CrewType.Frog:
          images[index].sprite = FrogIcon;
          break;
        //case Crew.CrewType.Bunny:
        //  images[index].sprite = RabbitIcon;
        //  break;
      }
    }
  }
}
