// Copyright (c) Microsoft Corporation. 
// Licensed under the MIT license.

#pragma once

#include "CoreMinimal.h"
#include "Widgets/DeclarativeSyntaxSupport.h"
#include "Widgets/SCompoundWidget.h"
#include "Widgets/Views/STableViewBase.h"
#include "Widgets/Views/STableRow.h"

using FSkeletalMeshItemPtr = TSharedPtr<class FSkeletalMeshItem>;

class FSkeletalMeshItem
{
public:
	explicit FSkeletalMeshItem(TSoftObjectPtr<class USkeletalMesh> InSkeletalMesh);

	TSoftObjectPtr<class USkeletalMesh> GetSkeletalMesh() const { return SkeletalMesh; };
	FName GetObjectPath() const { return SkeletalMesh.ToSoftObjectPath().GetAssetPathName(); };
	int32 GetNumMorphTargets() const { return NumMorphTargets; };

private:
	TSoftObjectPtr<class USkeletalMesh> SkeletalMesh = nullptr;
	int32 NumMorphTargets;
};

class SAllowMorphTargetsMeshView : public SCompoundWidget
{
	SLATE_BEGIN_ARGS(SAllowMorphTargetsMeshView)
	{
	}

	SLATE_END_ARGS()

	/** Widget constructor */
	void Construct(const FArguments& Args, const TSharedRef<class SAllowMorphTargetsWidget> Owner);

	/** Destructor */
	~SAllowMorphTargetsMeshView();

	void Refresh();

	FSkeletalMeshItemPtr GetSelectedSkeletalMesh() const;
	void OnSelectionChanged(FSkeletalMeshItemPtr Item, ESelectInfo::Type SelectInfo);
private:
	TSharedRef<ITableRow> OnGenerateRow(FSkeletalMeshItemPtr Item, const TSharedRef<STableViewBase>& OwnerTable);

	EActiveTimerReturnType TriggerRefreshSkeletalMeshList(double InCurrentTime, float InDeltaTime);
	void RebuildSkeletalMeshList();
	void OnPropertyChanged(UObject* ObjectBeingModified, FPropertyChangedEvent& PropertyChangedEvent);

private:
	TSharedPtr<SListView<FSkeletalMeshItemPtr>> SkeletalMeshListView;
	TArray<FSkeletalMeshItemPtr> SkeletalMeshListItems;

	TWeakPtr<class SAllowMorphTargetsWidget> OwnerWidget;

	/** Whether the active timer to refresh the list is registered */
	bool bIsActiveTimerRegistered;

	FDelegateHandle OnPropertyChangedDelegateHandle;
};