using System.Collections;
using System.Collections.Generic;
using HurricaneVR.Framework.Weapons.Guns;
using NodeCanvas.Framework;
using RootMotion.FinalIK;
using UnityEngine;

public class NPCCombatController : MonoBehaviour
{
    [SerializeField] private Animator animator;
    private static readonly int shoot = Animator.StringToHash("Attack");

    [SerializeField] private float cooldownTimeShoot = 2f;
    private float nextTime;

    [SerializeField] private HVRGunBase gun;
    [SerializeField] private AimIK aim;

    private Transform oldTransform;
    private PlayerTargetPosition playerTargetPosition;

    public void UnAttachWeapon()
    {
        gun.gameObject.transform.parent = null;
        gun.gameObject.AddComponent<Rigidbody>();
    }
    
    public Status Attack(GameObject target)
    {
        nextTime += Time.deltaTime;

        if (oldTransform == null || oldTransform != target.transform)
        {
            oldTransform = target.transform;
            playerTargetPosition = target.GetComponent<PlayerTargetPosition>();
            if (playerTargetPosition != null)
            {
                aim.solver.target = playerTargetPosition.GetTarget();
            }
            else
            {
                aim.solver.target = target.transform;   
            }
        }

        if (nextTime >= cooldownTimeShoot)
        {
            nextTime = 0;
            animator.SetTrigger(shoot);
            RangeAttack(target);
        }

        return Status.Success;
    }

    private void RangeAttack(GameObject target)
    {
        gun.CallShoot();
    }
}
