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

public class StarsManager : MonoBehaviour
{
    public static StarsManager Instance;

    public GameObject starModel;

    public Star finalPlanet;

    List<Star> stars;

    float spawnTimer = -1f;
    float stayTimer = -1f;
    float lastSpawnDate;
    int currentStarIndex;

    const float spawnsDuration = 1f;
    const float initialSpawnDelay = 0.02f;
    const float finalSpawnDelay = 0.0005f;
    const float stayDuration = 1.5f;
    bool respawningStars;

    public static readonly float spawnMaxX = 125f;
    public static readonly float spawnMaxY = 30f;
    public static readonly float spawnMaxZ = 20f;

    void Awake()
    {
        Instance = this;
    }

	// Use this for initialization
	void Start ()
    {
        stars = new List<Star>();

        for(int i = 0; i < 60; i++)
        {
            GameObject newStar = (GameObject) Instantiate(starModel, transform);
            Star s = newStar.GetComponent<Star>();
            s.Init();
            stars.Add(s);
        }

        Star model = starModel.GetComponent<Star>();
        model.Init();
        stars.Add(model);

        GameEventManager.OnHyperspaceJump += OnHyperspaceJump;
    }

    void OnDestroy()
    {
        GameEventManager.OnHyperspaceJump -= OnHyperspaceJump;
    }
    
	// Update is called once per frame
	void Update ()
    {
        if(spawnTimer >= 0)
        {
            float delayBetweenTwoStars = initialSpawnDelay + (finalSpawnDelay - initialSpawnDelay) * (1f - spawnTimer / spawnsDuration);
            spawnTimer -= Time.deltaTime;
            float remainingTime = spawnTimer + stayDuration;
            while(Time.time - lastSpawnDate >= delayBetweenTwoStars)
            {
                lastSpawnDate += delayBetweenTwoStars;
                stars[currentStarIndex].TriggerTrail(remainingTime);
                currentStarIndex = (currentStarIndex + 1) % stars.Count;
            }
            if(spawnTimer < 0)
            {
                stayTimer = stayDuration;
                for(int i = 0; i < 30; i++)
                {
                    lastSpawnDate += delayBetweenTwoStars;
                    stars[currentStarIndex].TriggerTrail(remainingTime);
                    currentStarIndex = (currentStarIndex + 1) % stars.Count;
                }
            }
        }
        else if(stayTimer > 0)
        {
            stayTimer -= Time.deltaTime;
            if(stayTimer <= 0)
            {
                respawningStars = true;
                currentStarIndex = 0;
                lastSpawnDate = Time.time;
            }
        }
        if(respawningStars)
        {
            while(lastSpawnDate <= Time.time)
            {
                stars[currentStarIndex].Init(true);
                lastSpawnDate += Random.Range(0.001f, 0.01f);
                currentStarIndex++;
                if(currentStarIndex == stars.Count)
                {
                    currentStarIndex = 0;
                    respawningStars = false;
                    lastSpawnDate = Time.time + 2f;

                    if(StoryManager.Instance.RemainingJumpsAmount == 0)
                    {
                        finalPlanet.TriggerSpawn();
                    }

                    GameEventManager.JumpEnd();
                }
            }
        }
	}

    public bool IsJumping()
    {
        return respawningStars || stayTimer > 0 || spawnTimer >= 0;
    }

    void OnHyperspaceJump()
    {
        //for(int i = 0; i < stars.Count; i++)
        //{
        //    stars[i].TriggerTrail();
        //}
        spawnTimer = spawnsDuration;
        lastSpawnDate = Time.time - initialSpawnDelay;
    }

    public float GetShakeAmplitude()
    {
        if(spawnTimer >= 0)
        {
            return 1f - spawnTimer / spawnsDuration;
        }
        else if(stayTimer > 0)
        {
            return 1.2f;
        }
        return 0f;
    }


}
