using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System.Collections;

[ExecuteInEditMode]
[AddComponentMenu("Lux/Setup Lux 2.0")]
public class SetupLux_2_0 : MonoBehaviour {

	// WIND
	public Vector3 Lux_WindDirection;
	[Range(0.0f,1.0f)]
	public float Lux_WindStrength = 0.5f;

	// WETNESS
	public Vector4 Lux_WaterFloodlevel = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
	[Range(0.0f,1.0f)]
	public float Lux_RainIntensity = 0.0f;
	public Texture2D Lux_RainRipples;
	public Vector2 Lux_RippleWindSpeed = new Vector2(0.5f, 0.25f);
	public float Lux_RippleTiling = 4.0f;
	public float Lux_RippleAnimSpeed = 1.0f;
	public float Lux_WaterBumpDistance = 20.0f;
	

	void Awake () {
		checkDeferred();
	}
	
	void Update () {
		#if UNITY_EDITOR
			checkDeferred();
		#endif
		UpdateLuxWindSettings();
		UpdateLuxRainSettings();
	}


//	//////////////////////////////////////

	void checkDeferred() {
		if (Camera.main.renderingPath == RenderingPath.DeferredShading) {
				Shader.EnableKeyword("_LUX_DEFERRED");
		}
		else {
			Shader.DisableKeyword("_LUX_DEFERRED");	
		}	
	}

	void UpdateLuxWindSettings () {
		Lux_WindDirection = transform.up;
		Shader.SetGlobalVector("_Lux_Wind", new Vector4(Lux_WindDirection.x, Lux_WindDirection.y, Lux_WindDirection.z , Lux_WindStrength));
	}

	void UpdateLuxRainSettings () {
		Shader.SetGlobalVector("_Lux_WaterFloodlevel", Lux_WaterFloodlevel);
		Shader.SetGlobalFloat("_Lux_RainIntensity", Lux_RainIntensity);
		if(Lux_RainRipples) {
			Shader.SetGlobalTexture("_Lux_RainRipples", Lux_RainRipples);
		}
		Shader.SetGlobalVector("_Lux_RippleWindSpeed", new Vector4(Lux_WindDirection.x * Lux_RippleWindSpeed.x, Lux_WindDirection.z * Lux_RippleWindSpeed.x, Lux_WindDirection.x * Lux_RippleWindSpeed.y, Lux_WindDirection.z * Lux_RippleWindSpeed.y));
		Shader.SetGlobalFloat("_Lux_RippleTiling", Lux_RippleTiling);
		Shader.SetGlobalFloat("_Lux_RippleAnimSpeed", Lux_RippleAnimSpeed);	
		Shader.SetGlobalFloat("_Lux_WaterBumpDistance", Lux_WaterBumpDistance);	
	}

//	////////////////////////////////

	#if UNITY_EDITOR
	void OnDrawGizmosSelected()
    {
    //	Draw Wind Direction Handle
    	float hsize = HandleUtility.GetHandleSize(transform.position);
    	Handles.color = Color.green;
    	Quaternion rotation = Quaternion.LookRotation(transform.up * (-1.0f));
    	Handles.ArrowCap(0, transform.position, rotation, hsize * 1.5f);
    }
    #endif
}
