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

public class ShipCrewLoadout : MonoBehaviour
{
    public bool CanLaunch = false;
    public List<Crew.CrewType> LoadedCrewTypes = new List<Crew.CrewType>();

    public Sprite DogIcon;
    public Sprite CatIcon;
    public Sprite FrogIcon;
    public Sprite RabbitIcon;

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

    public void ToggleCrewType(Crew.CrewType ct)
    {
      if (this.LoadedCrewTypes.Contains(ct))
      {
        this.LoadedCrewTypes.Remove(ct);
      }
      else
      {
        this.LoadedCrewTypes.Add(ct);
      }
    }

    public void ClearLoadedCrew()
    {
      this.LoadedCrewTypes.Clear();
    }

    // Update is called once per frame
    void Update()
    {
      //this.GetComponent<Canvas>().gameObject.SetActive(this.CanLaunch);
      int currentCount = this.GetComponentsInChildren<Image>().Length;
      int diff = currentCount - LoadedCrewTypes.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 < LoadedCrewTypes.Count; index++)
      {
        Crew.CrewType ct = LoadedCrewTypes[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 + 1].sprite = RabbitIcon;
          //  break;
        }
      }
  }
}
