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

[RequireComponent(typeof(Collider))]
public class player_hitbox : MonoBehaviour, IDamageable
{
    public shotType hitboxType;
    netPlayer zombieScript;
    void Start()
    {
        zombieScript = GetComponentInParent<netPlayer>();
    }

    public LivingEntity GetEntity()
    {
        return null;
    }

    public void TakeDamage(HealthEventData damageData)
    {
        //transform.root.GetComponent<zombieV2>().
        if(zombieScript == null) { Debug.LogError("No playerScriptFound!"); return; } //incase if the zombie is dead already
        zombieScript.hitboxDamage(damageData,hitboxType);
        if (zombieScript.effectIdx == zombieScript.BloodFX.Length) zombieScript.effectIdx = 0;
        float angle = Mathf.Atan2(damageData.HitNormal.x, damageData.HitNormal.z) * Mathf.Rad2Deg + 180;
        var instance = Instantiate(zombieScript.BloodFX[zombieScript.effectIdx], damageData.HitPoint, Quaternion.Euler(0, angle + 90, 0));
        zombieScript.effectIdx++;

        var settings = instance.GetComponent<BFX_BloodSettings>();
        //settings.FreezeDecalDisappearance = InfiniteDecal;

        var attachBloodInstance = Instantiate(zombieScript.BloodAttach, transform);
        var bloodT = attachBloodInstance.transform;
        bloodT.position = damageData.HitPoint;
        bloodT.localRotation = Quaternion.identity;
        bloodT.localScale = Vector3.one * Random.Range(0.75f, 1.2f);
        bloodT.LookAt(damageData.HitPoint + damageData.HitNormal, damageData.HitDirection);
        bloodT.Rotate(90, 0, 0);
        Debug.Log(hitboxType.ToString());
    }

    public void OnCollisionEnter(Collision col)
    {
        Rigidbody rb = col.transform.GetComponentInParent<Rigidbody>();
        if (rb != null)
        {
            if (rb.tag == "Player")
            {
                Debug.Log($"{rb.name} Hit zombie {zombieScript.transform.name} with speed of {rb.velocity.magnitude}");
                if (col.transform.GetComponent<Rigidbody>().velocity.magnitude > 0)
                {
                        if (zombieScript.effectIdx == zombieScript.BloodFX.Length) zombieScript.effectIdx = 0;
                        float angle = Mathf.Atan2(col.impulse.normalized.x, col.impulse.normalized.z) * Mathf.Rad2Deg + 180;
                        var instance = Instantiate(zombieScript.BloodFX[zombieScript.effectIdx], col.GetContact(0).point, Quaternion.Euler(0, angle + 90, 0));
                        zombieScript.effectIdx++;

                        var settings = instance.GetComponent<BFX_BloodSettings>();
                        //settings.FreezeDecalDisappearance = InfiniteDecal;

                        var attachBloodInstance = Instantiate(zombieScript.BloodAttach, transform);
                        var bloodT = attachBloodInstance.transform;
                        bloodT.position = col.GetContact(0).point;
                        bloodT.localRotation = Quaternion.identity;
                        bloodT.localScale = Vector3.one * Random.Range(0.75f, 1.2f);
                        bloodT.LookAt(col.GetContact(0).point + col.GetContact(0).normal, col.impulse);
                        bloodT.Rotate(90, 0, 0);
                        Debug.Log(hitboxType.ToString());
                    
                   // GetComponent<Rigidbody>().AddForce(col.impulse*5);
                    HealthEventData damageData = new HealthEventData(-col.impulse.magnitude, DamageType.Hit);
                    TakeDamage(damageData);
                }
            }
        }
        else
        {
//            Debug.Log("Contact from " + col.transform.name + " with " + col.impulse);
        }
    }

}