using UnityEngine;
using System.Collections;

public class CharacterMovementController : MonoBehaviour 
{
    private const float MOVEMENT_THRESHOLD = 0.1f;

    private const float MOVEMENT_UNITY_X = 1.0f;
    private const float MOVEMENT_UNITY_Z = 1.0f;
    private const bool FIXED_MOVEMENT = true;

    private Interactable mInteractableInRange = null;
    private tk2dSpriteAnimator mAnimator;
    private tk2dSprite mSprite;
    private bool mVerticalAxisPressed = false, mHorizontalAxisPressed = false;

    private void Awake()
    {
        InputRedirecter.Create();
        mAnimator = GetComponent<tk2dSpriteAnimator>();
        mSprite = GetComponent<tk2dSprite>();
        CheckAnim();
        InputRedirecter.Instance.AxisMoved += HandleAxisMoved;
        InputRedirecter.Instance.InteractionPressed += HandleInteractionPressed;
        InputRedirecter.Instance.VerticalAxisKeyDown += HandleVerticalAxisKeyDown;
        InputRedirecter.Instance.VerticalAxisKeyUp += HandleVerticalAxisKeyUp;
        InputRedirecter.Instance.HorizontalAxisKeyDown += HandleHorizontalAxisKeyDown;
        InputRedirecter.Instance.HorizontalAxisKeyUp += HandleHorizontalAxisKeyUp;
        InputRedirecter.Instance.EnterPressed += HandleEnterPressed;
    }

    private void OnDestroy()
    {
        InputRedirecter.Instance.AxisMoved -= HandleAxisMoved;
        InputRedirecter.Instance.InteractionPressed -= HandleInteractionPressed;
        InputRedirecter.Instance.VerticalAxisKeyDown -= HandleVerticalAxisKeyDown;
        InputRedirecter.Instance.VerticalAxisKeyUp -= HandleVerticalAxisKeyUp;
        InputRedirecter.Instance.HorizontalAxisKeyDown -= HandleHorizontalAxisKeyDown;
        InputRedirecter.Instance.HorizontalAxisKeyUp -= HandleHorizontalAxisKeyUp;
        InputRedirecter.Instance.EnterPressed -= HandleEnterPressed;
    }

    private void HandleEnterPressed(object sender, System.EventArgs e)
    {
        if(mInteractableInRange != null && !DialogCaller.Instance.GetDialogOpened() && !ChoiceScreenCaller.Instance.GetLayerOpened() && mControlsLayerOpened == null)
        {
            SoundEffectsPlayer.Instance.Play("sfxprisao");
            DialogCaller.Instance.Call(typeof(EndingDialog), Enums.eDialogModes.NextOk);
        }
    }

    private void HandleHorizontalAxisKeyUp(object sender, InputRedirecter.AxisKeyEventArgs e)
    {
        mHorizontalAxisPressed = false;
        CheckAnim();
    }

    private void HandleHorizontalAxisKeyDown(object sender, InputRedirecter.AxisKeyEventArgs e)
    {
        mHorizontalAxisPressed = true;
        CheckAnim();
        mSprite.FlipX = e.Direction < 0;
    }

    private void HandleVerticalAxisKeyUp(object sender, InputRedirecter.AxisKeyEventArgs e)
    {
        mVerticalAxisPressed = false;
        CheckAnim();
    }

    private void HandleVerticalAxisKeyDown(object sender, InputRedirecter.AxisKeyEventArgs e)
    {
        mVerticalAxisPressed = true;
        CheckAnim();
    }

    private void CheckAnim()
    {
        if (mVerticalAxisPressed || mHorizontalAxisPressed)
        {
            if (!mAnimator.IsPlaying("Run"))
            {
                mAnimator.Play("Run");
                SoundEffectsPlayer.Instance.Play("sfxpassos", true);
            }
        }
        else
        {
            if (!mAnimator.IsPlaying("Idle"))
            {
                mAnimator.Play("Idle");
                SoundEffectsPlayer.Instance.Stop("sfxpassos");
            }
        }
    }

    private void HandleInteractionPressed(object sender, System.EventArgs e)
    {
        if (!DialogCaller.Instance.GetDialogOpened() && !ChoiceScreenCaller.Instance.GetLayerOpened() && mControlsLayerOpened == null)
        {
            if (mInteractableInRange != null)
            {
                mInteractableInRange.Interact();
            }
        }
    }

    private void HandleAxisMoved(object sender, InputRedirecter.AxisMovedEventArgs e)
    {
        if (!DialogCaller.Instance.GetDialogOpened() && !ChoiceScreenCaller.Instance.GetLayerOpened() && mControlsLayerOpened == null)
        {
            float xMov = Mathf.Abs(e.HorizontalMovement) > MOVEMENT_THRESHOLD ? (FIXED_MOVEMENT ? Mathf.Sign(e.HorizontalMovement) : e.HorizontalMovement) : 0.0f;
            float zMov = Mathf.Abs(e.VerticalMovement) > MOVEMENT_THRESHOLD ? (FIXED_MOVEMENT ? Mathf.Sign(e.VerticalMovement) : e.VerticalMovement) : 0.0f;
            Move(xMov * Time.deltaTime, zMov * Time.deltaTime);
        }
    }

    /*private void Update()
    {
        if (Input.GetButtonDown("Interaction"))
        {
            if (mInteractableInRange != null)
            {
                mInteractableInRange.Interact();
            }
        }
    }*/

	/*private void FixedUpdate () 
    {
        float xMovementMultiplier = Mathf.Abs(Input.GetAxis("Horizontal")) > MOVEMENT_THRESHOLD ? (FIXED_MOVEMENT ? Mathf.Sign(Input.GetAxis("Horizontal")) : Input.GetAxis("Horizontal")) : (0.0f);
        float zMovementMultiplier = Mathf.Abs(Input.GetAxis("Vertical")) > MOVEMENT_THRESHOLD ? (FIXED_MOVEMENT ? Mathf.Sign(Input.GetAxis("Vertical")) : Input.GetAxis("Vertical")) : (0.0f);
        Move(xMovementMultiplier * Time.deltaTime, zMovementMultiplier * Time.deltaTime);      
	}*/

    private void Move(float xMovementMultiplier, float zMovementMultiplier)
    {
        //rigidbody.MovePosition(rigidbody.position + new Vector3(xMovementMultiplier * MOVEMENT_UNITY_X, 0.0f, zMovementMultiplier * MOVEMENT_UNITY_Z));
        transform.Translate(new Vector3(xMovementMultiplier * MOVEMENT_UNITY_X, 0.0f, zMovementMultiplier * MOVEMENT_UNITY_Z));
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.GetComponent<Interactable>() != null)
        {
            if (mInteractableInRange != null)
            {
                mInteractableInRange.SetCharacterRange(false);
            }
            mInteractableInRange = other.GetComponent<Interactable>();
            mInteractableInRange.SetCharacterRange(true);
        }
        else if (other.GetComponent<RoofTrigger>() != null)
        {
            other.GetComponent<RoofTrigger>().OnTriggerAreaEntered();
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.GetComponent<Interactable>() == mInteractableInRange && mInteractableInRange != null)
        {
            mInteractableInRange.SetCharacterRange(false);
            mInteractableInRange = null;
        }
    }

    GameObject mControlsLayerOpened = null;
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            if (!DialogCaller.Instance.GetDialogOpened() && !ChoiceScreenCaller.Instance.GetLayerOpened())
            {
                if (mControlsLayerOpened == null)
                {
                    mControlsLayerOpened = LayerManager.Instance.AddLayerOnTop(LayerManager.Layers.ControlsScreen, true, null, false);
                }
                else
                {
                    LayerManager.Instance.RemoveLayer(mControlsLayerOpened, false, true);
                    mControlsLayerOpened = null;
                }
            }
        }
    }
}
