using System;
using System.Collections;
using System.Collections.Generic;
using System.Resources;
using UnityEngine;
using UnityEngine.Events;

public class PlayerUltimate : MonoBehaviour
{
    public float ultRadius;
    public int ultDamage;
    public float bookHideDelay = 1.7f;
    public LayerMask enemyLayerMask;
    public UnityEvent Ulted;  
    public Animator leftHand = null;
    public Animator book = null;
    public int ultPointsRequired = 0;
    private int ultPoints = 0;
    private bool canUlt = false;

    // Update is called once per frame
    void Update()
    {
        if(ultPoints >= ultPointsRequired && !canUlt){
            canUlt = true;
            ShowBook();
        }

        if(Input.GetKeyDown(KeyCode.Q) && canUlt){
            canUlt = false;
            ultPoints -= ultPointsRequired;
            Ult();
        }
    }

    private void Ult()
    {
        Ulted.Invoke();
        book.SetTrigger("Open");
        var enemies = Physics.OverlapSphere(transform.position, ultRadius, enemyLayerMask);
        if(enemies != null){
            for(int i = 0; i < enemies.Length; i++){
                enemies[i].GetComponent<Health>().Damage(ultDamage);
                GetComponent<Health>().Damage(-1);
            }
        }
        Invoke(nameof(HideBook), bookHideDelay);
    }

    private void ShowBook()
    {
        leftHand.SetTrigger("Show");
    }

    private void HideBook(){
        leftHand.SetTrigger("Hide");
        book.SetTrigger("Close");
    }
    public void AddPoint(){
        ultPoints++;
    }

    void OnDrawGizmos(){
        Gizmos.DrawWireSphere(transform.position, ultRadius);
    }
}
