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

public class PlayerMoviment : PhysicsObject
{
    public float maxSpeed = 7;
    public float syncJumpAcceptable = 0.3f;
    public float jumpTakeOffSpeed = 7;
    public int playerIndex;
    public int otherPlayerIndex;
    public bool canMove = false;
    private SpriteRenderer spriteRenderer;
    private Animator animator;
    bool canDoubleJump;
    float jumpStart = 0f;
    // Use this for initialization
    void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        animator = GetComponent<Animator>();
    }
    void FaceRight() {
        Quaternion newRotation = new Quaternion();
        newRotation.eulerAngles = new Vector3(0, 0, 0);
        transform.rotation = newRotation;

        //spriteRenderer.flipX = !spriteRenderer.flipX;
    }
    void FaceLeft() {
        Quaternion newRotation = new Quaternion();
        newRotation.eulerAngles = new Vector3(0, 180, 0);
        transform.rotation = newRotation;

        //spriteRenderer.flipX = !spriteRenderer.flipX;
    }
    protected override void ComputeVelocity()
    {
        if (canMove) {
            float playerAxis = Input.GetAxis("Horizontal" + playerIndex.ToString());
            float player2Axis = Input.GetAxis("Horizontal" + otherPlayerIndex.ToString());

            Vector2 move = Vector2.zero;

            move.x = playerAxis + player2Axis;
            if (isUpsideDown) { move.x = -playerAxis - player2Axis; }
            if (Input.GetButtonDown("Jump" + playerIndex.ToString()) && grounded)
            {
                jumpStart = Time.timeSinceLevelLoad;
                animator.SetTrigger("Jump");
                velocity.y = jumpTakeOffSpeed;
                canDoubleJump = true;
            }
            else if (Input.GetButtonUp("Jump" + playerIndex.ToString()))
            {
                if (velocity.y > 0)
                {
                    velocity.y = velocity.y * 0.5f;
                }
            }
            else if (Input.GetButtonDown("Jump" + otherPlayerIndex.ToString()) && (Time.timeSinceLevelLoad - jumpStart) < syncJumpAcceptable)
            {
                Debug.Log("Entrou");
                velocity.y = jumpTakeOffSpeed * 2;
                canDoubleJump = false;
            }
            //bool flipSprite = (transform.rotation.eulerAngles.y==0 ? (move.x > 0.01f) : (move.x < 0.01f));
            if (Mathf.Abs(velocity.x) > 0.1f)
            {
                if (transform.rotation.eulerAngles.y == 0 && move.x < -0.01f)
                {
                    FaceLeft();
                }
                else if ((transform.rotation.eulerAngles.y == 180 && move.x > 0.01f))
                {
                    FaceRight();
                }
                animator.SetBool("isIdle", false);
            }
            else
            {
                animator.SetBool("isIdle", true);
            }

            animator.SetBool("isGrounded", grounded);
            animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed);
            targetVelocity = move * maxSpeed;
        }
 
    }
}