using UnityEngine;
using System.Collections;
using System;
using AssemblyCSharp;

public class MovmentControl : MonoBehaviour
{
	public enum PlayersLights {Red,Green,Blue};
    public PlayersLights avaterColor;
    public float moveSpeed; //the amount the chac moves per frame
    public float jumpHeight; //the amount the chac moves up after jumping
    public float moveAcc;
    public KeyCode moveLeftKey;
    public KeyCode moveRightKey;
    public KeyCode jumpKey;
    public Rigidbody2D myRigid;
    float distToGround;

	private IPlayerController control;

	private Animator myAnim;
	// Use this for initialization
	void Start ()
    {
        myRigid = GetComponent<Rigidbody2D>();
        distToGround = GetComponent<Collider2D>().bounds.extents.y;
		myAnim = GetComponentInChildren<Animator> ();
#if UNITY_ANDROID
		control = new TouchControl ();
#else
		control = new KeyboardController (moveLeftKey, moveRightKey, jumpKey);
        try
        {

		    GameObject.Find ("LeftButton").SetActive(false);
		    GameObject.Find ("RightButton").SetActive(false);
		    GameObject.Find ("JumpButton").SetActive(false);
        }
        catch
        { }
		#endif

    }
	
	// Update is called once per frame
	void FixedUpdate ()
    {
        // arabic fix
#if !UNITY_ANDROID
		control = new KeyboardController (moveLeftKey, moveRightKey, jumpKey);
#endif
        Test_Move();
	}
    public void Test_Move()
    {
        bool moved = false;
		if (control.getRight () && !control.getLeft ()) {
			moved = true;
			MoveRight ();
		} else if(!control.getRight() && control.getLeft())
        {
            moved = true;
            MoveLeft();
        }
      if (!moved)
        {
            SpeedDown();
        }
		if(control.getJump())
        {
            Jump();
        }

		if (myRigid.velocity.y < -10) 
		{
			myAnim.Play ("PlayerFall");
            if (myRigid.velocity.x > 0)
            {
                myAnim.GetComponent<SpriteRenderer>().flipX = true;
            }
            else if (myRigid.velocity.x < 0)
            {
                myAnim.GetComponent<SpriteRenderer>().flipX = false;
            }
        }
        else if (!myAnim.GetCurrentAnimatorStateInfo(0).IsName("PlayerLand") && !myAnim.GetCurrentAnimatorStateInfo(0).IsName("PlayerFall"))
        {
            myAnim.GetComponent<SpriteRenderer>().flipX = false;
            if (myAnim.GetCurrentAnimatorStateInfo(0).IsName("PlayerIdle") || myAnim.GetCurrentAnimatorStateInfo(0).IsName("PlayerWalkLeftLoop2") || myAnim.GetCurrentAnimatorStateInfo(0).IsName("PlayerWalkRightLoop") || myAnim.GetCurrentAnimatorStateInfo(0).IsName("PlayerStartWalkRight") || myAnim.GetCurrentAnimatorStateInfo(0).IsName("PlayerStartWalkLeft") || myAnim.GetCurrentAnimatorStateInfo(0).IsName("PlayerJumpLeftLoop") || myAnim.GetCurrentAnimatorStateInfo(0).IsName("PlayerJumpRightLoop") || myAnim.GetCurrentAnimatorStateInfo(0).IsName("PlayerJumpUpLoop"))
            {
                if (myRigid.velocity.x > 2)
                {
                    if (!myAnim.GetCurrentAnimatorStateInfo(0).IsName("PlayerWalkRightLoop") && !myAnim.GetCurrentAnimatorStateInfo(0).IsName("PlayerStartWalkRight"))
                    {
                        myAnim.Play("PlayerStartWalkRight");
                    }
                } else if (myRigid.velocity.x < -2)
                {
                    if (!myAnim.GetCurrentAnimatorStateInfo(0).IsName("PlayerWalkLeftLoop2") && !myAnim.GetCurrentAnimatorStateInfo(0).IsName("PlayerStartWalkLeft"))
                    {
                        myAnim.Play("PlayerStartWalkLeft");
                    }
                } else
                {
                    myAnim.Play("PlayerIdle");
                }
            }
        }

		/*if (myRigid.velocity.x > 0) {
			myAnim.GetComponent<SpriteRenderer> ().flipX = true;
		} else if (myRigid.velocity.x < 0) {
			myAnim.GetComponent<SpriteRenderer> ().flipX = false;
		}*/


    }
    public void MoveRight()
    {
        myRigid.velocity = new Vector2(Math.Min(myRigid.velocity.x + moveAcc * Time.deltaTime, moveSpeed), myRigid.velocity.y);
    }
    public void MoveLeft()
    {
        myRigid.velocity = new Vector2(Math.Max(myRigid.velocity.x - moveAcc * Time.deltaTime, -moveSpeed), myRigid.velocity.y);
    }
    public void SpeedDown()
    {
        if (0 != myRigid.velocity.x)
        {
            myRigid.velocity = new Vector2((Math.Max(0, Math.Abs(myRigid.velocity.x) - moveAcc * Time.deltaTime)) * (myRigid.velocity.x / Math.Abs(myRigid.velocity.x)), myRigid.velocity.y);
        }
        
    }

    private bool groundedWithXOffset(float xOffset)
    {
        var pos = transform.position;
        pos.x += xOffset * GetComponent<Collider2D>().bounds.extents.x;


        return Physics2D.Raycast(pos, -Vector3.up, distToGround + 0.1f, Physics2D.GetLayerCollisionMask(gameObject.layer));
    }

    bool IsGrounded()
    {
        return groundedWithXOffset(1) || groundedWithXOffset(-1);
    }

    public void Jump()
    {
        if (IsGrounded() && myRigid.velocity.y < 0.001f)
        {
            myRigid.velocity = new Vector2(myRigid.velocity.x, jumpHeight);
            if (myRigid.velocity.x > 2)
            {
                myAnim.Play("PlayerJumpRight");
            }
            else if (myRigid.velocity.x < -2)
            {
                myAnim.Play("PlayerJumpLeft");
            }
            else
            {
                myAnim.Play("PlayerJumpUp");
            }
        }
    }
}
