// Fill out your copyright notice in the Description page of Project Settings.

#include "AmmoSpawner.h"
#include "Kismet/GameplayStatics.h"
#include "../Actors/Ammo.h"
// Paper2D plugin headers.
#include "PaperFlipbook.h"
#include "PaperFlipbookComponent.h"
#include "../GameJamTemplate.h"
#include "../LevelHandler.h"


AAmmoSpawner::AAmmoSpawner():
	ObjectName(GetName())
{
	PrimaryActorTick.bCanEverTick = true;

	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));

	PaperFlipbook = CreateDefaultSubobject<UPaperFlipbookComponent>(TEXT("PaperFlipbook"));
	PaperFlipbook->SetupAttachment(RootComponent);
}

void AAmmoSpawner::EndingRound()
{
	IsRoundRunning = false;
	Reset();
}

bool AAmmoSpawner::GetIsRoundRunning()
{
	return IsRoundRunning;
}

int AAmmoSpawner::GetCurrentRoundIndex()
{
	return CurrentRound;
}

bool AAmmoSpawner::IsBlueAmmoEnabled()
{
	return LevelHandler->IsBlueAmmoEnabled();
}

bool AAmmoSpawner::IsRedSpreadEnabled()
{
	return LevelHandler->IsRedSpreadEnabled();
}

bool AAmmoSpawner::IsBlueSpreadEnabled()
{
	return LevelHandler->IsBlueSpreadEnabled();
}

void AAmmoSpawner::BeginPlay()
{
	Super::BeginPlay();

	LevelHandler = (ALevelHandler*)UGameplayStatics::GetActorOfClass(GetWorld(), ALevelHandler::StaticClass());

	if (LevelHandler == nullptr)
	{
		UE_LOG(LogGameJam, Error,
			TEXT("[%s] Could not find level handler in level."),
			*ObjectName);
	}

	MissingAmmoPair = 0;
	CurrentRound = 0;
}

void AAmmoSpawner::Reset()
{
	for (int i = 0; i < 3; i++)
	{
		AmmoHolder[i]->Destroy();
	}

	MissingAmmoPair = 0;
}

void AAmmoSpawner::SetupRound()
{
	for (int i = 0; i < 3; i++)
	{
		AmmoHolder[i] = GenerateAmmoPair(CurrentRound, i + 1);
	}
}

void AAmmoSpawner::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

AAmmo* AAmmoSpawner::GetAmmoItter(int Position)
{
	MissingAmmoPair = Position;
	AAmmo* PairToReturn = AmmoHolder[MissingAmmoPair-1];
	ReplenishAmmo();
	return PairToReturn;
}

void AAmmoSpawner::ReplenishAmmo()
{
	AAmmo* NewAmmoPair = GenerateAmmoPair(0, MissingAmmoPair);
	AmmoHolder[MissingAmmoPair-1] = NewAmmoPair;
}

AAmmo* AAmmoSpawner::GenerateAmmoPair(int Round, int Position)
{
	FVector SpawnLocation = GetActorLocation();
	switch (Position)
	{
	case 1:
	{
		SpawnLocation.Y -= 150.0;
		break;
	}
	case 3:
	{
		SpawnLocation.Y += 150.0;
		break;
	}
	default:
		break;
	}

	AAmmo* NewAmmoPair = GetWorld()->SpawnActor<AAmmo>(
		AmmoUClass, SpawnLocation, GetActorRotation());
	NewAmmoPair->SetActorScale3D(FVector(0.1,0.1,0.1));

	float LeftColorPicker = FMath::RandRange(0, 100);
	float RightColorPicker = FMath::RandRange(0, 100);

	float LeftSpreadPicker = FMath::RandRange(0, 100);
	float RightSpreadPicker = FMath::RandRange(0, 100);

	bool BlueAmmoEnabled = LevelHandler->IsBlueAmmoEnabled();

	bool RedSpreadEnabled = LevelHandler->IsRedSpreadEnabled();
	bool BlueSpreadEnabled = LevelHandler->IsBlueSpreadEnabled();

	if (BlueAmmoEnabled)
	{
		NewAmmoPair->GetLeftRound()->SetRoundColor(LeftColorPicker < 50 ? EAmmoColour::Red : EAmmoColour::Blue);
		NewAmmoPair->GetRightRound()->SetRoundColor(RightColorPicker < 50 ? EAmmoColour::Red : EAmmoColour::Blue);
	}
	else
	{
		NewAmmoPair->GetLeftRound()->SetRoundColor(EAmmoColour::Red);
		NewAmmoPair->GetLeftPaperFlipbook()->SetFlipbook(RedAmmo);
		NewAmmoPair->GetRightRound()->SetRoundColor(EAmmoColour::Red);
		NewAmmoPair->GetRightPaperFlipbook()->SetFlipbook(RedAmmo);
	}

	if (RedSpreadEnabled)
	{
		if (NewAmmoPair->GetLeftRound()->GetRoundColor() == EAmmoColour::Red)
		{
			NewAmmoPair->GetLeftRound()->SetSpread(LeftSpreadPicker < 30 ? true : false);
			if (NewAmmoPair->GetLeftRound()->GetSpread())
				NewAmmoPair->GetLeftPaperFlipbook()->SetFlipbook(RedSpread);
			else
				NewAmmoPair->GetLeftPaperFlipbook()->SetFlipbook(RedAmmo);
		}
		if (NewAmmoPair->GetRightRound()->GetRoundColor() == EAmmoColour::Red)
		{
			NewAmmoPair->GetRightRound()->SetSpread(RightSpreadPicker < 30 ? true : false);
			if (NewAmmoPair->GetRightRound()->GetSpread())
				NewAmmoPair->GetRightPaperFlipbook()->SetFlipbook(RedSpread);
			else
				NewAmmoPair->GetRightPaperFlipbook()->SetFlipbook(RedAmmo);
		}
	}
	else
	{
		if (NewAmmoPair->GetLeftRound()->GetRoundColor() == EAmmoColour::Red)
			NewAmmoPair->GetLeftPaperFlipbook()->SetFlipbook(RedAmmo);
		if (NewAmmoPair->GetRightRound()->GetRoundColor() == EAmmoColour::Red)
			NewAmmoPair->GetRightPaperFlipbook()->SetFlipbook(RedAmmo);
	}

	if (BlueSpreadEnabled)
	{
		if (NewAmmoPair->GetLeftRound()->GetRoundColor() == EAmmoColour::Blue)
		{
			NewAmmoPair->GetLeftRound()->SetSpread(LeftSpreadPicker < 30 ? true : false);
			if (NewAmmoPair->GetLeftRound()->GetSpread())
				NewAmmoPair->GetLeftPaperFlipbook()->SetFlipbook(BlueSpread);
			else
				NewAmmoPair->GetLeftPaperFlipbook()->SetFlipbook(BlueAmmo);
		}
		if (NewAmmoPair->GetRightRound()->GetRoundColor() == EAmmoColour::Blue)
		{
			NewAmmoPair->GetRightRound()->SetSpread(RightSpreadPicker < 30 ? true : false);
			if (NewAmmoPair->GetRightRound()->GetSpread())
				NewAmmoPair->GetRightPaperFlipbook()->SetFlipbook(BlueSpread);
			else
				NewAmmoPair->GetRightPaperFlipbook()->SetFlipbook(BlueAmmo);
		}
	}
	else
	{
		if (NewAmmoPair->GetLeftRound()->GetRoundColor() == EAmmoColour::Blue)
			NewAmmoPair->GetLeftPaperFlipbook()->SetFlipbook(BlueAmmo);
		if (NewAmmoPair->GetRightRound()->GetRoundColor() == EAmmoColour::Blue)
			NewAmmoPair->GetRightPaperFlipbook()->SetFlipbook(BlueAmmo);
	}

	if (NewAmmoPair->GetLeftRound()->GetRoundColor() == EAmmoColour::Red)
	{
		if (NewAmmoPair->GetRightRound()->GetRoundColor() == EAmmoColour::Blue)
		{
			UE_LOG(LogGameJam, Log,
				TEXT("[%s] Spawning a new AmmoPair { '%s' } - ('%f' '%f')"),
				*ObjectName,
				*ToStringAmmoPair(NewAmmoPair),
				LeftColorPicker, RightColorPicker
			);
		}
		else
		{
			UE_LOG(LogGameJam, Log,
				TEXT("[%s] Spawning a new AmmoPair { '%s' } - ('%f' '%f')"),
				*ObjectName,
				*ToStringAmmoPair(NewAmmoPair),
				LeftColorPicker, RightColorPicker
			);
		}
	}
	else
	{
		if (NewAmmoPair->GetRightRound()->GetRoundColor() == EAmmoColour::Red)
		{
			UE_LOG(LogGameJam, Log,
				TEXT("[%s] Spawning a new AmmoPair { '%s' } - ('%f' '%f')"),
				*ObjectName,
				*ToStringAmmoPair(NewAmmoPair),
				LeftColorPicker, RightColorPicker
			);
		}
		else
		{
			UE_LOG(LogGameJam, Log,
				TEXT("[%s] Spawning a new AmmoPair { '%s' } - ('%f' '%f')"),
				*ObjectName,
				*ToStringAmmoPair(NewAmmoPair),
				LeftColorPicker, RightColorPicker
			);
		}
	}

	return NewAmmoPair;
}

FString AAmmoSpawner::ToStringAmmoPair(AAmmo* AmmoToPrint)
{
	return AmmoToPrint->ToString() + " " + FString::FromInt(MissingAmmoPair);
}

AAmmo* AAmmoSpawner::GetAmmoHolderByIndex(int i)
{
	return AmmoHolder[i-1];
}

void AAmmoSpawner::StartingRoundByIndex(int RoundIndex)
{
	CurrentRound = RoundIndex;
	IsRoundRunning = true;
	SetupRound();
}
