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

public class MemoryEntry
{
    public int up;
    public int down;
    public string button;

    public override string ToString()
    {
        return (button + " at " + down + ", to " + up);
    }

    public bool Equals(MemoryEntry other)
    {
        if (other == null) return false;
        return (this.button.Equals(other.button));
    }


}

public class player : MonoBehaviour 
{
    public GameObject Prefab;
	private float inputDirection;				// x value of the movevector
	public static float verticalVelocity;				// y value of the movevector
	private Vector3 moveVector;
	private Vector3 lastMotion;
	private CharacterController controller;
	public float gravity = 30.0f; 
	public float jumpForce = 10.0f;
	public float speed = 5.0f;
	private bool secondjumpAvail = false; 
	private Animator anim;
	public static Vector3 playerPos;
    private player instance;
    private int numberOfDudes = 1;

    
    private int Frame = 0;
    
    private int aDown = 0;
    private int aUp = 0;
    private int dDown = 0;
    private int dUp = 0;
    private int wDown = 0;
    private int wUp = 0;
    private int things = 0;

    public List<MemoryEntry> Memory = new List<MemoryEntry>();

    // Use this for initialization
    void Start (){
		controller = GetComponent<CharacterController> ();
		anim = GetComponent<Animator> ();
		instance = this;
	}
	
	// Update is called once per frame
	void Update () {

        if(Frame == 600)
        {
            GameObject PlayerClone = (GameObject)Instantiate(Prefab, new Vector3(16, -13, numberOfDudes), transform.rotation);
            //PlayerClone.GetComponent<CloneScript>().CloneMemory = GetComponent<Player>().Memory;
            PlayerClone.GetComponent<PrefabScript>().MemoryClone.AddRange(Memory);
            PlayerClone.GetComponent<PrefabScript>().zPos = numberOfDudes;
            Memory.Clear();
            numberOfDudes++;
            transform.position = new Vector3(16, -13, 0);
            Frame = 0;
        }
        remembering();

        if (Input.GetKey("a") || Input.GetKey("d"))
        {
            inputDirection = 0;
            

            moveVector = Vector3.zero;
            inputDirection = Input.GetAxis("Horizontal") * speed;

        }else
        {
            inputDirection = 0;
            moveVector = new Vector3(0, 0, 0);
        }
        playerPos = transform.position;

        if (Iscontrollergrounded ()) {
			verticalVelocity = 0;

			if (Input.GetKeyDown (KeyCode.W) && !Input.GetKeyDown("a") && !Input.GetKeyDown("d")) {
				//	Debug.Log ("Spacebar!");
				//jumping & doublejump
				verticalVelocity = jumpForce;
				secondjumpAvail = true; 
			}
			moveVector.x = inputDirection;
		} else {
			if (Input.GetKeyDown (KeyCode.W)) {
				if (secondjumpAvail) {
					//	Debug.Log ("Spacebar!");
					//jumping & doublejump
					verticalVelocity = jumpForce;
					secondjumpAvail = false;
				}
			}
			verticalVelocity -= gravity * Time.deltaTime;
			moveVector.x = lastMotion.x;
		} 
			
		//	Debug.Log (inputDirection);

		moveVector.y = verticalVelocity;
		moveVector = new Vector3 (inputDirection, verticalVelocity, 0);
		//Debug.Log (moveVector);
		controller.Move (moveVector * Time.deltaTime);
		lastMotion = moveVector;
	}



	private bool Iscontrollergrounded()
	{
		Vector3 leftRayStart;
		Vector3 rightRayStart;

		leftRayStart = controller.bounds.center;
		rightRayStart = controller.bounds.center;

		leftRayStart.x -= controller.bounds.extents.x; 
		rightRayStart.x += controller.bounds.extents.x;

		if (Physics.Raycast (leftRayStart, Vector3.down, (controller.height / 2) + 0.1f))
			return true;

		if (Physics.Raycast (rightRayStart, Vector3.down, (controller.height / 2) + 0.1f))
			return true;


		return false;
		 


	}




		private void OnCotrollerColliderHit (ControllerColliderHit hit)
	{
		if (controller.collisionFlags == CollisionFlags.Sides) {
			if (Input.GetKeyDown (KeyCode.Space)) {
				moveVector = hit.normal * speed;
				moveVector.y = jumpForce;
				secondjumpAvail = true;
			}
		}
		//collecting stuff
		switch (hit.gameObject.tag) 
		{
		case "Coin":
			Destroy(hit.gameObject);
			break; 
		case "JumpPad":
			verticalVelocity = jumpForce * 2;
			break;
		default:
			break;
		}
	}

	void OnCollisionEnter (Collision other)
		{
		if(other.gameObject.CompareTag ("MovingPlatform"))
			{
			Debug.Log ("OnCollisionEnter");
			gameObject.transform.parent = other.gameObject.transform;
			}
		}		


		void OnCollisionExit (Collision other)
	{
		if (other.gameObject.tag == "MovingPlatform") {
			Debug.Log ("OnCollisionExit");
			gameObject.transform.parent = null;
		}
	}

    /*
	void OnTriggerEnter (Collision other)
	{
		if (other.gameObject.CompareTag("deathTrigger"))
		{
			UnityEngine.SceneManagement.SceneManager.LoadScene("main_02");
		}
		}
		*/

    void remembering()
    {

        // Memory.Add(new Vector3(transform.position.x, transform.position.y, transform.position.z));
        if (Input.GetKeyDown("a"))
        {
            aDown = Frame;
        }
        if (Input.GetKeyUp("a"))
        {
            aUp = Frame;
            Memory.Add(new MemoryEntry() { button = "a", up = aUp, down = aDown });

        }

        if (Input.GetKeyDown("d"))
        {
            dDown = Frame;
        }
        if (Input.GetKeyUp("d"))
        {
            dUp = Frame;
            Memory.Add(new MemoryEntry() { button = "d", up = dUp, down = dDown });

        }

        if (Input.GetKeyDown("w"))
        {
            wDown = Frame;
        }
        if (Input.GetKeyUp("w") && !Input.GetKeyDown("a") && !Input.GetKeyDown("d"))
        {
            wUp = Frame;
            Memory.Add(new MemoryEntry() { button = "w", up = wUp, down = wDown });

        }
        Frame++;

    }


}