using UnityEngine;
using System.Collections;


public class ThunderGeneratorSimple : ThunderGenerator {
	
	
	public Transform StartPoint;
    public Transform EndPoint;
	
	public float MovementSpeed 	   = 30.0f;
	public float MovementAmplitude = 1.0f;
	
	Vector3 StartPos;
	Vector3 EndPos;
	
    public ThunderGeneratorSimple() : base()
    {
    }
	
	// initi thunder path
	public override void InitThunders()
	{
		#if UNITY_EDITOR
			// verification
	        if ( oThunder == null )
	        {
	            Debug.LogError("No oThunder call SetThunder(..)?");
	            return;
	        }
			
			if ( oThunderStrikeArray == null )
	        {
	            Debug.LogError("No oThunder call SetThunder(..)?");
	            return;
	        }
		#endif	
		
		StartPos = StartPoint.position;
		EndPos   = EndPoint.position;
		
		ConstructPath();
	}
	
	void ConstructPath()
	{
		for ( int i = 0; i < oThunderStrikeArray.Length; i++)
        {
			ThunderStrike oCurrentThunderStrinke = oThunderStrikeArray[i];
			
			// trace the path between each point (straight line)
	        for (int j = 0; j < oCurrentThunderStrinke.m_vPathPointArray.Length; j++)
	        {
	            float fd = ((float)j) / (float)(oCurrentThunderStrinke.m_vPathPointArray.Length - 1);
	
	            oCurrentThunderStrinke.m_vOriginPathPointArray[j] = new Vector3();
	            oCurrentThunderStrinke.m_vOriginPathPointArray[j] = StartPos * (1.0f - fd) + EndPos * fd;
	
	            oCurrentThunderStrinke.m_vPathPointArray[j] = oCurrentThunderStrinke.m_vOriginPathPointArray[j];
	        }
		}
		
		StartPos = StartPoint.position;
		EndPos   = EndPoint.position;
		
		// set light range
		oThunder.oLight.range = Vector3.Distance( StartPos, EndPos ) * 2.0f;
		
	}
	
	
	// update thunder
	public override void UpdateThunders()
	{
		#if UNITY_EDITOR
			// verification
	        if ( oThunder == null )
	        {
	            Debug.LogError("No oThunder call SetThunder(..)?");
	            return;
	        }
			
			if ( oThunderStrikeArray == null )
	        {
	            Debug.LogError("No oThunder call SetThunder(..)?");
	            return;
	        }
		#endif	
		
		// if start or end point move -> reconstruct the path
		if ( ( StartPos != StartPoint.position ) || ( EndPos != EndPoint.position ) )
		{
			ConstructPath();
		}

        float timex = Time.time * MovementSpeed * 0.1365143f ;
        float timey = Time.time * MovementSpeed * 1.21688f ;
        float timez = Time.time * MovementSpeed * 2.5564f ;

		
		for ( int i = 0; i < oThunderStrikeArray.Length; i++ )
        {
			ThunderStrike oCurrentThunderStrinke = oThunderStrikeArray[i];
			
			float oCurrentPositionSum;
			
			int iPathlength = oCurrentThunderStrinke.m_vPathPointArray.Length;
			
			for ( int j = 0; j < iPathlength; j++ )
	        {
				// sum of coordinates change for each vertex -> use it with perlin noise to calculate an offest
				oCurrentPositionSum = i + oCurrentThunderStrinke.m_vOriginPathPointArray[j].x + oCurrentThunderStrinke.m_vOriginPathPointArray[j].y +oCurrentThunderStrinke.m_vOriginPathPointArray[j].z;
				
				Vector3 Offset = new Vector3(
				    oPerlinNoise.Noise(timex + oCurrentPositionSum),
	                oPerlinNoise.Noise(timey + oCurrentPositionSum),
	                oPerlinNoise.Noise(timez + oCurrentPositionSum));
				
				// more movement in the center of the electrical line than on start and end.
				// 0.0 < ((float)j / (float)( iPathlength - 1 )) < 1.0
				// Mathf.Abs( ((float)j / (float)( iPathlength - 1 )) - 0.5 ) -> value of 0.5 in the center and and 1.0f in extremity
				//
				float coeff = 0.5f - ( Mathf.Abs( ((float)j / (float)( iPathlength - 1 )) - 0.5f ));
				
				// calculate the final position = origin position + perturbation
	            oCurrentThunderStrinke.m_vPathPointArray[j] = oCurrentThunderStrinke.m_vOriginPathPointArray[j] + Offset * MovementAmplitude * coeff;
				
	        }
		}
		
		// update light
		oThunder.oLight.intensity = oThunder.fLightIntensity * ( 0.5f + 3.0f * oPerlinNoise.Noise( Time.time * MovementSpeed ) );
		
		// Ask to update vertice arrayon the next Thunder Update
		oThunder.bUpdateVertices = true;
	}

}