using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour {

	public Transform playerControllerT;

	public int points;
	public float ropeBoundsRadius;

	public float ropeRadius;
	public float ropeSpringiness;
	public float ropeSpringDamper;
	public float ropeSegmentDrag;

	public float ropeSmoothness;

	public Transform ropeEndTop;

	public Material ropeMaterial;
	public Material megaRopeMaterial;

	// Use this for initialization
	IEnumerator Start () {
		Vector3[] controlPoints = SplineGenerator.GenerateSplinePositions (points, ropeBoundsRadius, ropeBoundsRadius * 0.3f, ropeBoundsRadius * 0.6f);

		for (int i=0; i<10; i++) {
			controlPoints = SplineGenerator.SmoothSplinePositions(controlPoints, 0.5f);
			for(int r=0; r<2; r++)
			{
				controlPoints = SplineGenerator.RelaxSplinePositions(controlPoints, 0.5f);
			}
		}

//		CurvySpline spline = SplineGenerator.GenerateSplineFromPositions (controlPoints);
//		SplinePathMeshBuilder meshBuilder = spline.gameObject.AddComponent<SplinePathMeshBuilder> ();
//		meshBuilder.Spline = spline;
//		meshBuilder.FastInterpolation = true;
//		meshBuilder.CapWidth = ropeRadius;
//		spline.renderer.material = ropeMaterial;
//		Ropify ropify = spline.gameObject.AddComponent<Ropify> ();
//		ropify.spring = ropeSpringiness;
//		ropify.damper = ropeSpringDamper;
//		ropify.segmentDrag = ropeSegmentDrag;
//		Transform firstSegment = spline.transform.GetChild (0);
//		playerControllerT.position = firstSegment.position;
//		firstSegment.gameObject.AddComponent<Follow> ().target = playerControllerT;

//		Vector3[] controlPoints = SplineGenerator.GenerateSplinePositions2(points, ropeBoundsRadius, ropeBoundsRadius * 0.6f);

		//MegaRope
		MegaShape megaShape = new GameObject ("MegaRopeShape").AddComponent<MegaShape> ();
		megaShape.splines.Clear ();
		MegaSpline megaSpline = megaShape.NewSpline ();
		for ( int i = 0; i < controlPoints.Length; ++i )
		{
			Vector3 p = controlPoints[i];
			megaSpline.AddKnot(p, p, p);
		}

		megaShape.CalcLength ();
		megaShape.AutoCurve ();

		MegaRope megaRope = new GameObject ("MegaRope").AddComponent<MegaRope> ();
		megaRope.startShape = megaShape;
		megaRope.radius = 0.05f;
		megaRope.strandedMesher.strands = 1;
		megaRope.strandedMesher.sides = 6;
		megaRope.strandedMesher.SegsPerUnit = 10;
		megaRope.strandedMesher.TwistPerUnit = 1;
		megaRope.strandedMesher.uvtiley = 10;
		megaRope.airdrag = 0.1f;
		megaRope.timeStep = 0.05f;

		megaRope.DisplayDebug = false;
		megaRope.DoCollide = true;
		megaRope.SelfCollide = true;
		megaRope.layer = -1;
		megaRope.gravity = Vector3.zero;
		megaRope.top = ropeEndTop;

		yield return null;

		megaRope.renderer.sharedMaterial = megaRopeMaterial;
		playerControllerT.position = megaRope.masses [0].pos + Vector3.up * 0.15f;
		ropeEndTop.localPosition = Vector3.down * 0.15f;
//		megaRope.Mass = 0.001f;
//		megaRope.DampingRatio = 0f;
//		megaRope.spring = 10f;

		//megaRope.airdrag = 0f;
		//megaRope.stiffspring

		//megaRope.stiffsprings = true;
	}
	
}
