#nullable enable
using System.Collections;
using System.Collections.Generic;
using NaughtyAttributes;
using UnityEngine;
using UnityEngine.InputSystem;

public class MinionThrower : MonoBehaviour {
    [SerializeField]
    [Required]
    GameFlowManager gameFlowManager = null!;

    public Entity[] minionPrefabs;
    public GameObject aimArrow;

    bool isInThrowMode;
    Vector2 throwDirection = Vector2.right;
    MinionBar minionBar;
    VampireEntity vampireEntity;
    PlayerMovement movement;
    float currentCooldownTimer;
    float currentInitialCooldown;

    void Awake() {
        vampireEntity = GetComponent<VampireEntity>();
        movement = GetComponent<PlayerMovement>();
        minionBar = FindObjectOfType<MinionBar>();
        aimArrow.SetActive(false);
    }

    void Start() {
        foreach (Entity minion in minionPrefabs) {
            minionBar.AddMinionType(minion);
        }
    }

    void FixedUpdate() {
        if (currentCooldownTimer > 0f) {
            currentCooldownTimer -= Time.fixedDeltaTime;
            minionBar.UpdateReload(currentCooldownTimer / currentInitialCooldown);
        }
    }

    public void ToggleThrowMode(InputAction.CallbackContext context) {
        if (!gameFlowManager.CanDoAction)
            return;

        if (context.performed) {
            isInThrowMode = true;
            aimArrow.SetActive(true);
        } else if (context.canceled) {
            PerformThrow();
            isInThrowMode = false;
            aimArrow.SetActive(false);
        }
    }

    public void AimThrow(InputAction.CallbackContext context) {
        throwDirection = context.ReadValue<Vector2>().normalized;
        if (vampireEntity.playerMovement.facingRight) {
            aimArrow.transform.rotation = Quaternion.FromToRotation(Vector2.right, throwDirection);
        } else {
            aimArrow.transform.rotation = Quaternion.FromToRotation(Vector2.left, throwDirection);
        }
    }

    void PerformThrow() {
        if (!isInThrowMode || currentCooldownTimer > 0f) {
            return;
        }

        float minionHealthCost = 10f; // TODO
        if (minionHealthCost >= vampireEntity.Health) {
            return;
        }
        vampireEntity.TakeDamage(minionHealthCost, vampireEntity);

        currentInitialCooldown = 2f; // TODO
        currentCooldownTimer = currentInitialCooldown;
        minionBar.UpdateReload(currentCooldownTimer / currentInitialCooldown);

        var newMinion = Instantiate(minionBar.GetCurrentMinion().gameObject, transform.position + new Vector3(throwDirection.x, throwDirection.y, 0f) * 1f, Quaternion.identity)
            .GetComponent<Monster>();
        if (movement.GetSafeZoneIfImmobile() is { } safeZone) {
            newMinion.thrownFromSafeZone = true;
            newMinion.thrownTargetPosition = safeZone.GetOutsidePosition(throwDirection);
        }

        newMinion.direction = throwDirection;
        newMinion.gameFlowManager = vampireEntity.gameFlowManager;
    }

}
