#include "PlayerCombatEnhancement.h"
#include "CoreMinimal.h"
#include "Engine/World.h"
#include "GameFramework/PlayerController.h"
#include "GameFramework/Character.h"
#include "Kismet/GameplayStatics.h"
#include "Components/SkeletalMeshComponent.h"
#include "Animation/AnimInstance.h"

static UPlayerCombatEnhancement* CombatEnhancerInstance = nullptr;

UPlayerCombatEnhancement::UPlayerCombatEnhancement()
{
    PrimaryComponentTick.bCanEverTick = true;
    bAutoActivate = true;
    CurrentEnhancementLevel = 0.0f;
    MaxEnhancementLevel = 1000.0f;
    EnhancementDecayRate = 5.0f;
    EnhancementIncreasePerShot = 200.0f;
    BaseHitBoxScale = 1.0f;
    MaxHitBoxScale = 10.0f;
    DetectionRadius = 5000.0f;
}

void UPlayerCombatEnhancement::BeginPlay()
{
    Super::BeginPlay();
    CombatEnhancerInstance = this;
    PlayerCharacter = Cast<ACharacter>(GetOwner());
    if (PlayerCharacter)
    {
        PlayerController = Cast<APlayerController>(PlayerCharacter->GetController());
    }
}

void UPlayerCombatEnhancement::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
    Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
    
    if (CurrentEnhancementLevel > 0.0f)
    {
        CurrentEnhancementLevel = FMath::Max(0.0f, CurrentEnhancementLevel - EnhancementDecayRate * DeltaTime);
        UpdateEnemyHitBoxes();
    }
}

void UPlayerCombatEnhancement::OnWeaponFired()
{
    if (!PlayerCharacter) return;
    
    CurrentEnhancementLevel = FMath::Min(MaxEnhancementLevel, CurrentEnhancementLevel + EnhancementIncreasePerShot);
    
    TArray<AActor*> FoundEnemies;
    UGameplayStatics::GetAllActorsOfClass(GetWorld(), ACharacter::StaticClass(), FoundEnemies);
    
    for (AActor* EnemyActor : FoundEnemies)
    {
        ACharacter* EnemyCharacter = Cast<ACharacter>(EnemyActor);
        if (EnemyCharacter && EnemyCharacter != PlayerCharacter)
        {
            float Distance = FVector::Dist(PlayerCharacter->GetActorLocation(), EnemyCharacter->GetActorLocation());
            if (Distance <= DetectionRadius)
            {
                EnhanceEnemyHitBox(EnemyCharacter);
            }
        }
    }
}

void UPlayerCombatEnhancement::EnhanceEnemyHitBox(ACharacter* EnemyCharacter)
{
    if (!EnemyCharacter) return;
    
    USkeletalMeshComponent* Mesh = EnemyCharacter->GetMesh();
    if (Mesh)
    {
        float EnhancementFactor = FMath::Clamp(CurrentEnhancementLevel / MaxEnhancementLevel, 0.0f, 1.0f);
        float CurrentScale = BaseHitBoxScale + (MaxHitBoxScale - BaseHitBoxScale) * EnhancementFactor;
        Mesh->SetRelativeScale3D(FVector(CurrentScale, CurrentScale, CurrentScale));
    }
}

void UPlayerCombatEnhancement::UpdateEnemyHitBoxes()
{
    if (!PlayerCharacter) return;
    
    TArray<AActor*> FoundEnemies;
    UGameplayStatics::GetAllActorsOfClass(GetWorld(), ACharacter::StaticClass(), FoundEnemies);
    
    for (AActor* EnemyActor : FoundEnemies)
    {
        ACharacter* EnemyCharacter = Cast<ACharacter>(EnemyActor);
        if (EnemyCharacter && EnemyCharacter != PlayerCharacter)
        {
            float Distance = FVector::Dist(PlayerCharacter->GetActorLocation(), EnemyCharacter->GetActorLocation());
            if (Distance <= DetectionRadius)
            {
                EnhanceEnemyHitBox(EnemyCharacter);
            }
        }
    }
}

UPlayerCombatEnhancement* UPlayerCombatEnhancement::GetInstance()
{
    return CombatEnhancerInstance;
}