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

public class StoryManager : MonoBehaviour {

	string[] scientistStory = {
		"Dr. Hordernia?s science log, Palonian star date 478. My crew and I have begun our exploration into unknown mass 00957.8 at data coordinates 35.0116� N, 135.7680� E star system Authronous. We have departed on a journey to hopefully understand the vortex that inhabits our neighbouring galaxy far closer than any scientist has before us. May these logs serve as a catalog of our findings, and a future for research into the vortex."
,"Science Log, Palonian Star Date 491. As we approach, our sense of time is beginning to warp. The gravity vortex not only seems to be drawing in objects in space, but also the time around the mass. I would not have noticed, if not for the message we received from the science department back home. They have had no contact for several days, despite only a few hours passing here. This is a highly interesting finding, and I am sure it will not be the last. "
,"Science Log, Palonian Star Date 494.During our exploration and study of the gravity vortex we came across a faint signal orbiting around the mass. Understandably we are highly concerned. No Palonian has had the technology to venture this close to a gravity of this magnitude. It is possible we were not the only team sent to study the mass... but surely we would have seen them? Or heard them? I have made the executive decision to see if we can find this other ship closer to the black hole. "
,"Science Log, Palonian Star Date 497? Despite orders from command control, the Palonian spacecraft Alpha 38.C under my command is heading into the black hole to assist the Kantven medbay. Many of the crew have begun to experience painful headaches and loss of consciousness on the upper decks. Our medical officer is unsure what is occurring, but his best guess is that the extreme gravity is beginning to affect the composition of the team?s atoms. Transmissions coming from the lost ship have stopped. I am beginning to wonder if this is was all in vein. "
,"Science Log, Palonian Star Date 502? The last transmission received from the lost spaceship is confirmed to be a trap. The ship has revealed itself as a bandit hijacking vessel. First Officer Dessari is planning to pull us out of the gravity vortex for a return home. A majority of the crew is now too sick to assist with the ship. We are endeavoring to escape before the hostile ship finds us; we are in no fit state to fight them off. Requesting assistance to 35.0116� N, 135.7680� E Authronous West quarter. "
,"Science Log, Palonian Star Date ??? Time has seemed to stop on the ship, I am unsure of what day it is, or if any time has passed at all. We have lost over half of the crew to radiation poisoning with assistance yet to arrive. Myself and my crew are not expecting to make it home. The ship's thrusters are slowly beginning to fail, despite our engineers best efforts. This ship was never intended to get this close to the black hole. I got carried away with my search for life, and it has cost me not only the mission, but the lives of my crew. "
,"As we near the centre of the vortex I can feel the ship start to collapse in on itself. The bandits before us are likely dust now, ground into nothing from the sheer gravitational force of the black hole. I guess they exact same will happen to me and my crew; what's left of them anyway. Back home people liked to joke about what was inside that huge vortex that made the Authronous system function. I always assumed there was nothing there, but as I near it, I start to wonder if I was wrong. "
	};

	string[] pirateStory = {
		"Hello? Hello? This is Gazvor... of the Kantven Royal Space Fleet. We are in dire need of assistance... We have entered a mass of dark space...our ship systems are unable to handle the gravitational pull! If you are reaching this message please send help! ...We don?t know how long we..."
,"This is a nonviolent distress call for assistance. We are in dire need of shield repairs. We were ambushed and have serious structural damage to our hull. I repeat, this is a nonviolent distress call for a Kantven Medical Ship, we have women and children aboard. Without assistance we will likely perish. Please send assistance immediately! �"
,"This is Gazvor of the Kantven Royal Space Fleet. We are in dire need of assistance, without repairs to our shields we will be unable to escape! We do not know our location, only that we are close to the inside of the circular mass.There are 14 planets orbiting the mass, none of which are inhabitable. We failed to land on its closest planet for repairs. Please help...we are so far from home."
,"Daskven Base? Come in Daskven Base. This is Gazvor of party 12KL. We have seen no sign of the Palonian science craft. We have been hovering around the black hole for several days. Any chance you have picked up on their location? Gazvor Out. "
,"The order to hold position has failed to reap any real rewards, Daskven Base. The crew refuse to agree to another few days at this location. Whilst I, understand your reasons for wanting the technology aboard the ship, it appears that I was assigned a lazy crew who have no interest in 8th Age politics. Requesting permission to return to Daskven. Gazvor Out. "
,"We have detected transmissions from the science craft. We are planning to engage further inside the sun. We speculate it is a small observation vessel with minimal security. Delay our arrival to Daskven by 480 hours. We will return soon with the new tech. Gazvor out. "
,"The ship has gone deep into the black hole, 12KL will attempt to follow them. They activated their distress signal and appear to be unable to exit the gravity field. It seems that their ship is not as powerful or as advanced as the first transmission implied. Should we pursue? Gazvor Out. "
,"Orders received, ready to engage. Their ship might be garbage but they?re more useful alive then dead. We?ll be in and out in time for dinner. Gazvor Out. "
	};

	public StoryItem storyItem;

	private int currentSegment = 0;
	private int numSegments = 0;

	public float spawnRate = 0.1f;


	// Use this for initialization
	void Start () {
		
	}
	
	public void Restart(string charName)
	{
		Gameplay gameplay = gameObject.GetComponentInParent<Gameplay>();
		var oldItems = gameplay.GetComponentsInChildren<StoryItem>();

		for (int i=0; i < oldItems.Length; i++)
			Destroy(oldItems[i].gameObject);

		currentSegment = 0;

		if (charName == "pirate")
		{
			numSegments = pirateStory.Length;
			for (int i=0; i < pirateStory.Length; i++)
				{
					var newStoryItem = Instantiate(storyItem, new Vector3(0,0,0), new Quaternion(), gameplay.transform);
					newStoryItem.message = pirateStory[i];
					newStoryItem.orbitDistance = CreateStartDistance(i);
					newStoryItem.SetOrbitColor((float)i / (float)(numSegments-1));
					newStoryItem.SetCurrentSegment(i == 0);
					newStoryItem.orbitPos = Random.value * 360.0f;
				}
		}
		else
		{
			numSegments = scientistStory.Length;	
			for (int i=0; i < scientistStory.Length; i++)
				{
					var newStoryItem = Instantiate(storyItem, new Vector3(0,0,0), new Quaternion(), gameplay.transform);
					newStoryItem.message = scientistStory[i];
					newStoryItem.orbitDistance = CreateStartDistance(i);
					newStoryItem.SetOrbitColor((float)i / (float)(numSegments-1));
					newStoryItem.SetCurrentSegment(i == 0);
					newStoryItem.orbitPos = Random.value * 360.0f;
				}		
		}
	}

	int getExpression()
	{
		float storyPos = (float)currentSegment / (float)(numSegments-1);
		if (storyPos < .3f)
			return 0;
		if (storyPos < .7f)
			return 1;
		return 2;
	}

	public bool CheckLastMessage()
	{
		if (currentSegment + 1 != numSegments)
			return false;

		Gameplay gameplay = gameObject.GetComponentInParent<Gameplay>();
		StoryItem storyItem = gameplay.GetComponentsInChildren<StoryItem>()[currentSegment];
		gameplay.DisplayMessage(storyItem.message, getExpression());

		return true;		
	}

	public void NextStoryItem()
	{
		Gameplay gameplay = gameObject.GetComponentInParent<Gameplay>();
		gameplay.DisplayMessage(gameplay.GetComponentsInChildren<StoryItem>()[currentSegment].message, getExpression());
		gameplay.GetComponentsInChildren<StoryItem>()[currentSegment].SetCurrentSegment(false);
		currentSegment++;
		if (currentSegment < numSegments)
			gameplay.GetComponentsInChildren<StoryItem>()[currentSegment].SetCurrentSegment(true);
	}

	public float CreateStartDistance(int i)
	{
		float maxD = GetComponentInParent<Gameplay>().playerShip.maxDistance * 0.5f;
		return maxD * (1.0f - ((float)i / (float)(numSegments-1)));
	}

	// Update is called once per frame
	void Update () {

	}


}
