﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerController : MonoBehaviour
{

    public float stealingDuration;
    float auxDur;
    public bool isStealing;
    public bool itemNear;
    public float speedMove = 10;
    Vector3 movement;
    private Animator anim;
    Rigidbody2D playerRigidbody;
    public bool canHide;
    public bool isHiding;
    AudioSource stepSource;
    [SerializeField]
    float period;
    float auxPeriod;
    
    float hidePosition;
    float originalPosition;
    [SerializeField]
    float hideTranslocation;
    void Awake()
    {
        playerRigidbody = GetComponent<Rigidbody2D>();
        stepSource = GetComponent<AudioSource>();
        anim = GetComponent<Animator>();
        auxPeriod = period;
        hidePosition = transform.position.y + hideTranslocation;
        originalPosition = transform.position.y;
    }


    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        ///Stealing or Hiding
        if (moveVertical < 0f && canHide)
        {

            print("Hiding");
            anim.SetBool("hide", true);
            isHiding = true;
            //transform.position = new Vector3(transform.position.x, hidePosition, transform.position.z);
        }

        if(moveVertical > 0f && itemNear)
        {
            print("Stealing");
            stealingDuration -=Time.deltaTime;
            if(stealingDuration > 0)
            {
                isStealing = true;
            }
            else
            {
                isStealing = false;
                //FinishedPickingUpItem
                print("StealingComplete");
                stealingDuration = auxDur;
            }
        }
        ///

        Move(moveHorizontal, moveVertical);

        if (moveHorizontal != 0)
        {
            period -= Time.deltaTime;
            if(period <=0)
            {
                stepSource.Play();
                period = auxPeriod;
            }
            anim.SetBool("hide", false);
            //transform.position = new Vector3(transform.position.x, originalPosition, transform.position.z);
            anim.SetBool("walking", true);
            anim.SetFloat("SpeedX", moveHorizontal);
            
            
            if(moveHorizontal != 0 && isStealing)
            {
                isStealing = false;
                print("Stealing Canceled");
            }


            if (moveHorizontal > 0)
            {

                anim.SetFloat("SpeedX", 1f);

            }
            else if (moveHorizontal < 0)
            {

                anim.SetFloat("SpeedX", -1f);

            }
            else
            {

                anim.SetFloat("SpeedX", 0f);
            }

        }
        else
        {

            anim.SetBool("walking", false);

        }

       
    
    }


    /*void OnTriggerExit2D(Collider2D other){
        if(this.gameObject.tag.Equals("hideout") && other.gameObject.tag.Equals("Player")){

            anim.SetBool ("hide", false);
        }
    } PARA SAIR DA MESA FAÇA O SEGUINTE, SE ELE APERTAR UMDOS LADOS ENQUANTO ESCONDIDO, ELE SAI PELO LADO QUE PRESSIONOU*/


    void Move(float h, float v)
    {
        movement.Set(h, 0f, v);
        movement = movement.normalized * Time.deltaTime * speedMove;
        playerRigidbody.MovePosition(transform.position + movement);
    }


}