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

public class TargetPlanet : MonoBehaviour
{
  #region Private Fields

    private SpriteRenderer incompleteSprite;
    private SpriteRenderer completeSprite;
    private static readonly int RequiredCrewMax = 5;
    private static readonly int RequiredCrewMin = 2;
    private static readonly float MaxDistanceDifficulty = 16;

    private UnityEngine.Random rand = new Random();

    private bool TransitionedToComplete = false;

    private GameLogic gameLogic;
    private PlayerActor homePlanet;

    #endregion

    public List<Crew.CrewType> RequiredCrew = new List<Crew.CrewType>();
    public List<Sprite> PlanetSprites = new List<Sprite>();

    #region MonoBehehaviour Callbacks

    public int GetRequiredCrewMax()
    {
        if (!homePlanet)
        {
            return RequiredCrewMax;
        }
        else
        {
            // Determine requirements by distance.
            float dist = (homePlanet.transform.position - this.transform.position).magnitude;
            int crew = Mathf.FloorToInt((dist / MaxDistanceDifficulty) * (gameLogic.PlanetRequiredCrewMax - gameLogic.PlanetRequiredCrewMin)) + gameLogic.PlanetRequiredCrewMin;

            return Mathf.Clamp(crew, gameLogic.PlanetRequiredCrewMin, gameLogic.PlanetRequiredCrewMax);
        }
    }

  // Start is called before the first frame update
  void Start()
  {
        gameLogic = FindObjectOfType<GameLogic>();
        homePlanet = FindObjectOfType<PlayerActor>();

        System.Array crewTypes = System.Enum.GetValues(typeof(Crew.CrewType));
        float targetRequiredCrew = GetRequiredCrewMax();
        while (RequiredCrew.Count < targetRequiredCrew)
        {
            Crew.CrewType ct = (Crew.CrewType)crewTypes.GetValue(Random.Range(0, crewTypes.Length));
            RequiredCrew.Add(ct);
        }

        SpriteRenderer[] childSpriteRenderers = this.gameObject.GetComponentsInChildren<SpriteRenderer>();
        foreach (SpriteRenderer sr in childSpriteRenderers)
        {
            if (sr.gameObject.name == "Incomplete Sprite")
            {
                this.incompleteSprite = sr;
            }
            else if (sr.gameObject.name == "Complete Sprite")
            {
                this.completeSprite = sr;
            }
        }
    }

    // Update is called once per frame
    void LateUpdate()
    {
        if (this.IsComplete())
        {
            this.incompleteSprite.gameObject.SetActive(false);
            this.completeSprite.gameObject.SetActive(true);

            CrewSpawner spawner = FindObjectOfType<CrewSpawner>();
            foreach (Crew crew in this.gameObject.GetComponentsInChildren<Crew>())
            {
                Destroy(crew.gameObject);
                spawner.SpawnCrew(crew.CType);
            }
            
            if (!TransitionedToComplete)
            {
                TransitionedToComplete = true;
                FindObjectOfType<GameLogic>().IncDefeatTimer();
            }

        }
    else
    {
      this.incompleteSprite.gameObject.SetActive(true);
      this.completeSprite.gameObject.SetActive(false);
    }
  }

    #endregion

    #region Public Methods

    public void RandomizePlanetSprite()
    {
        GetComponent<SpriteRenderer>().sprite = PlanetSprites[Random.Range(0, PlanetSprites.Count)];
    }
  public bool IsComplete()
  {
    // Mark as complete if at least one crew from each player is here
    // GameConfig config = FindObjectOfType<GameConfig>();
    // int numPlayers = config.NumberOfPlayers;
    // for (int i = 0; i < numPlayers; i++)
    // {
    //   Crew[] myCrew = this.gameObject.GetComponentsInChildren<Crew>();
    //   bool foundPlayerCrew = false;
    //   foreach (Crew crew in myCrew)
    //   {
    //     if (crew.PlayerNum == i) {
    //       foundPlayerCrew = true;
    //     }
    //   }
    //   if (!foundPlayerCrew) {
    //     return false;
    //   }
    // }
    if (this.RequiredCrew.Count > 0)
    {
      return false;
    }
    return true;
  }

  #endregion
}
