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

public class StoryItem : MonoBehaviour {

	public float rotationSpeed = 45.0f;
	public float orbitDistance = 50.0f;

	public float orbitPos = 0;

	public string message;

	public bool isCurrent = false;

	// Use this for initialization
	void Start () {
	}

	public void SetOrbitColor(float segmentPercent)
	{
		var particles = GetComponentInChildren<ParticleSystem>().main;
		particles.startColor = Color.HSVToRGB(segmentPercent, 0.8f, 1.0f);
	}

	float calcOrbitSpeed(float orbitDistance)
	{
		return 20.0f / (orbitDistance-4.0f);
	}

	public void SetCurrentSegment(bool currentSegment)
	{
		if (currentSegment)
		{
			isCurrent = true;
			GetComponentInChildren<ParticleSystem>().Play();
		}
		else
		{
			isCurrent = false;
			GetComponentInChildren<ParticleSystem>().Stop();
		}
	}
	
	// Update is called once per frame
	void Update () {
		
		Gameplay gameplay = gameObject.GetComponentInParent<Gameplay>();

		orbitPos += calcOrbitSpeed(orbitDistance) * Time.deltaTime * gameplay.timeMultipler;

		transform.rotation = Quaternion.Euler(0, 0, transform.rotation.eulerAngles.z + rotationSpeed * Time.deltaTime * gameplay.timeMultipler);
		transform.position = new Vector3(-Mathf.Sin(orbitPos) * orbitDistance, Mathf.Cos(orbitPos) * orbitDistance, 0);

		if (!isCurrent)
			return;

		Vector3 playerCollide = gameplay.playerShip.transform.position - transform.position;

		if (playerCollide.magnitude < 5.0f)
		{
			gameplay.storyManager.NextStoryItem();
		}
	}
}
