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

public class PlayerController : MonoBehaviour
{
    private enum PlayerState
    {
        NONE,
        PLAYING,
        DEAD
    }

    private PlayerState _state;

    private PlayerState State
    {
        get { return _state; }
        set
        {
            _state = value;

            switch (_state)
            {
                case PlayerState.DEAD:
                    GameOver();
                    break;
            }
        }
    }

    public static PlayerController instance;

    public Rigidbody2D body;
    public SpriteRenderer spriteRenderer;
    public CannonController cannon;
    public Animator animator;
    public Floater floater;
    public Vector2 jumpForce;
    public float jumpGravityScale;

    private bool isAirborne;


    void Awake()
    {
        instance = this;
    }

    void Update()
    {
        cannon.Orient();

        if (!isAirborne && Input.GetButton("Jump"))
        {
            Jump();
        }

        if (Input.GetButton("Fire1") && cannon.CanFire && State != PlayerState.DEAD)
        {
            cannon.Fire();
        }
    }

    private void Jump()
    {
        var direction = Input.GetAxis("Horizontal") < 0 ? -1f : 1f;
        floater.enableFloating = false;
        jumpForce.x = direction * jumpForce.x;
        body.AddForce(jumpForce);
        jumpForce.x = Mathf.Abs(jumpForce.x);
        body.gravityScale = jumpGravityScale;
        isAirborne = true;
    }

    //    void OnCollisionEnter2D(Collision2D collision)
    //    {
    //        Debug.Log("PLAYER COLLISION");
    //
    //        if (collision.gameObject.CompareTag("Enemy"))
    //        {
    //            Debug.Log("LOSE!");
    //        }
    //    }

    void OnTriggerEnter2D(Collider2D collider)
    {
        Debug.Log("PLAYER TRIGGER");

        if (collider.CompareTag("Wave") && isAirborne)
        {
            body.gravityScale = 0f;
            body.velocity = Vector2.zero;
            isAirborne = false;
            floater.enableFloating = true;
        }

        if (collider.CompareTag("Enemy"))
        {
            if (State != PlayerState.DEAD)
            {
                State = PlayerState.DEAD;
                spriteRenderer.sortingLayerName = "Water";
                spriteRenderer.sortingOrder = 1;
                animator.ResetTrigger("Idle");
                animator.ResetTrigger("Death");
                animator.SetTrigger("Death");
                GameOver();
            }
        }
    }

    public void GameOver()
    {
        Debug.Log("GAMEOVER");
        floater.controllable = false;
        AudioManager.Instance.PlayGameOver();
        GameManager.Instance.gameOverCanvas.gameObject.SetActive(true);
    }

    //    void Float()
    //    {
    //        var rayDistance = 1f;
    //
    //        Debug.DrawRay(transform.position, Vector2.down * rayDistance, Color.green);
    //
    //        RaycastHit2D surface = Physics2D.Raycast(transform.position, Vector2.down, rayDistance, wavesLayer);
    //        if (surface.collider != null)
    //        {
    //            Debug.Log("Raycast detected: " + surface.transform.gameObject.name);
    //
    //            float floatHeight = 3f;
    //            float liftForce = 5f;
    //            float damping = 0.5f;
    //            float distance = Mathf.Abs(transform.position.y - surface.point.y);
    //            float heightError = floatHeight - distance;
    //            float force = liftForce * heightError - body.velocity.y * damping;
    //
    //            Debug.Log("Force: " + Vector3.up * force);
    //
    //            body.AddForce(Vector3.up * force);
    //
    //            //body.position = new Vector2(body.position.x, surface.point.y + 3f);
    //        }
    //    }
}
