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

public class PlayerController : MonoBehaviour
{
    public IdleState IdleState;
    public RunningState RunningState;
    public DodgingState DodgingState;
    public DeadState DeadState;

    public Transform CatBoy;

    public float DodgeCoolDown;
    private float dodgeCoolDownTimer;

    private Collider[] usableColliders;


    // Start is called before the first frame update
    void Start()
    {
        usableColliders = new Collider[5];
    }

    // Update is called once per frame
    void Update()
    {


        if(DeadState.enabled)
            return;

        if(DodgingState.enabled == false)
            dodgeCoolDownTimer -= Time.deltaTime;

        if(Input.GetKeyDown(KeyCode.Mouse1) && (IdleState.enabled || RunningState.enabled) && dodgeCoolDownTimer < 0)
        {
            RaycastHit hit;
            var mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);

            if(Physics.Raycast(mouseRay, out hit, 300, 1<<9))
            {
                if(IdleState.enabled)
                    IdleState.enabled = false;
                if(RunningState.enabled)
                    RunningState.enabled = false;

                DodgingState.Direction = (hit.point - CatBoy.transform.position).normalized;
                DodgingState.enabled = true;
                dodgeCoolDownTimer = DodgeCoolDown;
            }
        }

        if(Input.GetKey(KeyCode.Mouse0) && IdleState.enabled && RunningState.enabled == false)
        {
            IdleState.enabled = false;
            RunningState.enabled = true;
        }
        
        if(Input.GetKey(KeyCode.Mouse0) && RunningState.enabled)
        {
            RaycastHit hit;
            var mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);

            if(Physics.Raycast(mouseRay, out hit, 300, 1<<9))
            {        
                RunningState.Target = hit.point;
            }
        }

        if(Input.GetKeyUp(KeyCode.Mouse0) && RunningState.enabled)
        {
            RunningState.enabled = false;
            IdleState.enabled = true;
        }

        


    }
}
