// Copyright (c) 2024 Codemongous LLC. All Rights Reserved.

#include "SSourcifyImageTexture.h"

#include "Engine/Texture2D.h"
#include "Widgets/Layout/SBox.h"
#include "Widgets/Images/SImage.h"

using namespace Sourcify;

void SImageTexture::Construct(const FArguments& InArgs, UTexture2D* InTexture)
{
	if (!InTexture)
	{
		ChildSlot
		[
			SNullWidget::NullWidget
		];

		return;
	}

	Texture = TStrongObjectPtr<UTexture2D>(InTexture);
	Brush.SetResourceObject(InTexture);
	Brush.ImageSize = FVector2f(Texture->GetPlatformData()->SizeX, Texture->GetPlatformData()->SizeY);

	float AspectRatio = 1.f;
	if (Texture->GetPlatformData()->SizeY > 0)
	{
		AspectRatio = Texture->GetPlatformData()->SizeX / static_cast<float>(Texture->GetPlatformData()->SizeY);
	}

	ChildSlot
	[
		SNew(SBox)
		.MinAspectRatio(AspectRatio)
		.MaxAspectRatio(AspectRatio)
		.MinDesiredWidth(InArgs._MinDesiredWidth)
		.MinDesiredHeight(InArgs._MinDesiredHeight)
		.MaxDesiredWidth(InArgs._MaxDesiredWidth)
		.MaxDesiredHeight(InArgs._MaxDesiredHeight)
		[
			SNew(SImage).Image(&Brush)
		]
	];
}
