using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.TextCore.Text;

public class ClownHorn : MonoBehaviour
{
    public UnityEvent shot;
    public AudioSource audio;
    public UnityEvent dashed;
    public int damage = 0;
    public Transform shootPoint = null;
    public PlayerMovement_CharController playerController;
    public float reloadTime = 1;
    public float dashReload = 1;
    public float dashTime = 0.25f;
    public float dashSpeed = 1f;

    private float _shotTimer = 0;
    private float _dashTimer = 0;

    void Update()
    {
        if(Input.GetButtonDown("Fire1") && Time.time > _shotTimer){
            _shotTimer = Time.time + reloadTime;
            shot.Invoke();
            if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out var hit, 20)){
                if(hit.transform.CompareTag("Enemy")){
                    hit.transform.GetComponent<Health>().Damage(damage);
                }
            }
        }
        else if(Input.GetButtonDown("Fire2") && Time.time > _dashTimer){
            _dashTimer = Time.time + dashReload;
            //StartCoroutine(Dash());
            dashed.Invoke();
            playerController.Dash(-Camera.main.transform.forward, dashSpeed, dashTime);
        }
    }

    private IEnumerator Dash()
    {
        Vector3 dashDireciton = -Camera.main.transform.forward;
        //playerController.enabled = false;
        float endTime = Time.time + dashTime;
        while(Time.time < endTime){
            playerController.controller.Move(dashDireciton*dashSpeed*Time.deltaTime);
            yield return null;
        }
        //playerController.enabled = true;
    }
}
