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

public class BloodSucker : MonoBehaviour {

    [SerializeField] float suckDuration = 1f;
    [SerializeField] float suckRange = 1f;
    [SerializeField] float healthGainFromSuck = 30f;
    [SerializeField] ParticleSystem bloodParticles;

    Entity currentTarget;
    VampireEntity vampireEntity;
    bool isSucking;
    float currentSuckTimer;

    void Awake() {
        vampireEntity = GetComponent<VampireEntity>();
        bloodParticles.Stop(true, ParticleSystemStopBehavior.StopEmitting);
    }

    void FixedUpdate() {
        if (currentTarget == null) {
            SearchSuckTarget();
        }

        if (currentTarget != null) {
            if (Vector3.Distance(currentTarget.transform.position, transform.position) > suckRange) {
                SetTarget(null);
            } else {
                if (isSucking) {
                    PerformSuck(Time.fixedDeltaTime);
                    // } else {
                    //     HighlightTarget();
                }
            }
        }
    }

    void Update() {
        if (currentTarget != null && currentTarget.bloodTokens <= 0) {
            SetTarget(null);
        }
    }

    public void ToggleSuck(InputAction.CallbackContext context) {
        if (context.performed) {
            if (currentTarget != null) {
                SetIsSucking(true);
                currentSuckTimer = suckDuration;
            }
        } else if (context.canceled) {
            SetIsSucking(false);
        }
    }

    void SearchSuckTarget() {
        foreach (Collider2D coll in Physics2D.OverlapCircleAll(transform.position, suckRange)) {
            Entity entity = coll.GetComponent<Entity>();
            if (entity != null && entity.gameObject != gameObject) {
                if (!entity.IsAlive() && entity.bloodTokens > 0) {
                    SetTarget(entity);
                }
            }
        }
    }

    void SetTarget(Entity newTarget) {
        if(newTarget != currentTarget) {
            SetIsSucking(false);
        }
        if (currentTarget != null) {
            UnHighlightTarget();
        }

        currentTarget = newTarget;

        if (currentTarget != null) {
            // print("new target : " + currentTarget.name);
            HighlightTarget();
        }
    }

    void PerformSuck(float deltaTime) {
        if(currentTarget.bloodTokens <= 0){
            SetIsSucking(false);
            SetTarget(null);
        }
        bloodParticles.gameObject.transform.rotation = Quaternion.FromToRotation(transform.right, currentTarget.transform.position - transform.position);
        bloodParticles.gameObject.transform.localScale = Vector3.one * (Vector3.Distance(currentTarget.transform.position, transform.position) + 1f) / 4f;

        currentSuckTimer -= deltaTime;
        if (currentSuckTimer < 0f) {
            currentTarget.bloodTokens -= 1;
            vampireEntity.HealDamage(healthGainFromSuck);
            // print("One token sucked");
            if (currentTarget.bloodTokens == 0) {
                SetIsSucking(false);
                SetTarget(null);
                // TODO : Dispawn target
            } else {
                currentSuckTimer = suckDuration;
            }
        }
    }

    void SetIsSucking(bool value) {
        isSucking = value;
        if(isSucking) {
            bloodParticles.Play();
            currentTarget.OnSuck(true);
        } else {
            bloodParticles.Stop(true, ParticleSystemStopBehavior.StopEmitting);
            if(!(currentTarget is null))currentTarget.OnSuck(false);
        }
    }

    void HighlightTarget() {
        currentTarget.EnableHalo();
    }

    void UnHighlightTarget() {
        currentTarget.DisableHalo();
    }

}
