using UnityEngine;
using System.Collections;

public class QuadSkybox : MonoBehaviour
{
	public Material material;
	public float speed;
	public Vector2 scale;

	private MeshRenderer renderer;
	private MeshFilter meshFilter;
	private Mesh mesh;

	private int step = 0;
	private Vector2[] uv;

	// Use this for initialization
	void Start ()
	{
		
		this.scale = new Vector2 (Camera.main.orthographicSize * Screen.width / Screen.height, Camera.main.orthographicSize);

		this.renderer = this.gameObject.AddComponent<MeshRenderer> ();
		this.renderer.material = this.material;
		this.renderer.receiveShadows = false;
		this.renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
		this.renderer.useLightProbes = false;
		this.renderer.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;

		this.meshFilter = this.gameObject.AddComponent<MeshFilter> ();

		this.mesh = new Mesh ();
		this.mesh.vertices = new Vector3[] {
			new Vector3 (-2f * this.scale.x, -1f * this.scale.y, 0f),
			new Vector3 (0f * this.scale.x, -1f * this.scale.y, 0f),
			new Vector3 (-2f * this.scale.x, 1f * this.scale.y, 0f),
			new Vector3 (0f * this.scale.x, 1f * this.scale.y, 0f),
			new Vector3 (0f * this.scale.x, -1f * this.scale.y, 0f),
			new Vector3 (2f * this.scale.x, -1f * this.scale.y, 0f),
			new Vector3 (0f * this.scale.x, 1f * this.scale.y, 0f),
			new Vector3 (2f * this.scale.x, 1f * this.scale.y, 0f)
		};
		this.mesh.triangles = new int[] {
			0, 2, 3,
			0, 3, 1,
			4, 6, 7,
			4, 7, 5
		};

		this.uv = new Vector2[8];
		this.UpdateUVs ();

		this.meshFilter.mesh = this.mesh;

		this.transform.localPosition = new Vector3 (this.scale.x * 1f, 0f, this.transform.localPosition.z);
	}
	
	// Update is called once per frame
	void Update ()
	{
		this.transform.localPosition -= new Vector3 (this.speed * Time.deltaTime, 0f, 0f);
		if (this.transform.localPosition.x < -this.scale.x) {
			this.transform.localPosition += new Vector3 (this.scale.x * 2f, 0f, 0f);
			this.step = (this.step + 1) % 4;
			this.UpdateUVs ();
		}
	}

	public void ChangeMaterial (Material material)
	{
		this.material = material;
		if (this.renderer != null) {
			this.renderer.sharedMaterial = material;
		}
	}

	private void UpdateUVs ()
	{
		switch (step) {
		case 0:
			this.uv [0] = new Vector2 (0f, 0.5f);
			this.uv [1] = new Vector2 (0.5f, 0.5f);
			this.uv [2] = new Vector2 (0f, 1f);
			this.uv [3] = new Vector2 (0.5f, 1f);
			this.uv [4] = new Vector2 (0.5f, 0.5f);
			this.uv [5] = new Vector2 (1f, 0.5f);
			this.uv [6] = new Vector2 (0.5f, 1f);
			this.uv [7] = new Vector2 (1f, 1f);
			break;
		case 1:
			this.uv [0] = new Vector2 (0.5f, 0.5f);
			this.uv [1] = new Vector2 (1f, 0.5f);
			this.uv [2] = new Vector2 (0.5f, 1f);
			this.uv [3] = new Vector2 (1f, 1f);
			this.uv [4] = new Vector2 (0f, 0f);
			this.uv [5] = new Vector2 (0.5f, 0f);
			this.uv [6] = new Vector2 (0f, 0.5f);
			this.uv [7] = new Vector2 (0.5f, 0.5f);
			break;
		case 2:
			this.uv [0] = new Vector2 (0f, 0f);
			this.uv [1] = new Vector2 (0.5f, 0f);
			this.uv [2] = new Vector2 (0f, 0.5f);
			this.uv [3] = new Vector2 (0.5f, 0.5f);
			this.uv [4] = new Vector2 (0.5f, 0f);
			this.uv [5] = new Vector2 (1f, 0f);
			this.uv [6] = new Vector2 (0.5f, 0.5f);
			this.uv [7] = new Vector2 (1f, 0.5f);
			break;
		case 3:
			this.uv [0] = new Vector2 (0.5f, 0f);
			this.uv [1] = new Vector2 (1f, 0f);
			this.uv [2] = new Vector2 (0.5f, 0.5f);
			this.uv [3] = new Vector2 (1f, 0.5f);
			this.uv [4] = new Vector2 (0f, 0.5f);
			this.uv [5] = new Vector2 (0.5f, 0.5f);
			this.uv [6] = new Vector2 (0f, 1f);
			this.uv [7] = new Vector2 (0.5f, 1f);
			break;
		}

		this.mesh.uv = this.uv;
	}
}
