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

public class PrefabScript : 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;
    public float zPos = 0.0f;
    

    private int Frame = 0;
    private int actions = 0;


    private int aActions;
    private int aFrames;
    private int dActions;
    private int dFrames;
    private int wFrames;

    private bool w;
    private bool a;
    private bool d;

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

    void Start()
    {
        print(MemoryClone.Count);
        controller = GetComponent<CharacterController>();
        anim = GetComponent<Animator>();
        

    }

    void Update()
    {
        LookForChange();

        Frame++;

        
        
        Reset();
       

        //playerPos = transform.position;

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

       

        //	Debug.Log (inputDirection);

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

    private void Reset()
    {
        if (Frame == 600)
        {
            transform.position = new Vector3(16, -13, zPos);
            Frame = 0;
            actions = 0;

        }
    }

    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, Vector2.down, (controller.height / 2) + 0.1f))
            return true;

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


        return false;



    }
    
    private void Move()
    {
        if (a)
        {
            moveVector = Vector3.zero;
            inputDirection = -1 * speed;
            print("im trying to moove left");
        }
        else
        {
            
        }
        if (d)
        {
            moveVector = Vector3.zero;
            inputDirection = 1 * speed;
            print("im trying to moove right");
        }
        else
        {
            
        }
        playerPos = transform.position;

        if (Iscontrollergrounded())
        {
            verticalVelocity = 0;

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

    private void LookForChange()
    {
        if (aFrames != 0)
        {
            aFrames--;

        }
        else if (aFrames == 0 && a)
        {
            inputDirection = 0;
            a = false;
        }


        if (dFrames != 0)
        {
            dFrames--;
        }
        else if (dFrames == 0 && d)
        {
            inputDirection = 0;
            d = false;
        }
        if (wFrames != 0)
        {
            wFrames--;
        }
        else if (wFrames == 0 && w)
        {
        
            w = false;
        }

        try
        {
            if (Frame == MemoryClone[actions].down && actions < MemoryClone.Count)
            {
                if (MemoryClone[actions].button == "a")
                {
                    aFrames = MemoryClone[actions].up - MemoryClone[actions].down;
                    a = true;
                }
                if (MemoryClone[actions].button == "d")
                {
                    dFrames = MemoryClone[actions].up - MemoryClone[actions].down;
                    d = true;
                }
                if (MemoryClone[actions].button == "w")
                {
                    wFrames = MemoryClone[actions].up - MemoryClone[actions].down;
                    w = true;
                }
                actions++;
            }
        }
        catch { print("Im Out of Actions"); }
    }

}
