UI/DecorationItemPanel.razor
@using Sandbox;
@using Sandbox.UI;
@using HC3.UI
@inherits Panel
@namespace HC3

<root>
	<div class=@(BuildingPlacer.Instance?.PlacingResource == Decoration ? "item selected" : "item")>
		<img class="image" [email protected] onclick=@(() => StartPlacing()) />
		@if (Decoration.GetTintComponent().IsValid())
		{
			var deco = Decoration.GetTintComponent();
			<div class="color-deco">
				<label class="yes">palette</label>
				<div class="tint-swatch">
					@if (deco.PrimaryEnabled)
					{
						<div class="color-swatch @(SelectedTintChannel == TintChannel.Primary ? "selected" : "")" style=@($"background-color: {NewRedColor.Hex}") onclick=@(() => SetSelectedTint(TintChannel.Primary))></div>
					}
					@if (deco.SecondaryEnabled)
					{
						<div class="color-swatch @(SelectedTintChannel == TintChannel.Secondary ? "selected" : "")" style=@($"background-color: {NewGreenColor.Hex}") onclick=@(() => SetSelectedTint(TintChannel.Secondary))></div>
					}
					@if (deco.AccentEnabled)
					{
						<div class="color-swatch @(SelectedTintChannel == TintChannel.Accent ? "selected" : "")" style=@($"background-color: {NewBlueColor.Hex}") onclick=@(() => SetSelectedTint(TintChannel.Accent))></div>
					}
					@if (deco.DetailEnabled)
					{
						<div class="color-swatch @(SelectedTintChannel == TintChannel.Detail ? "selected" : "")" style=@($"background-color: {NewAlphaColor.Hex}") onclick=@(() => SetSelectedTint(TintChannel.Detail))></div>
					}
				</div>
			</div>
		}
		<div class="info">

			<label class="name">@Decoration.Title</label>
			<label class="cost">@(GameUtils.Currency)@(Decoration.Cost)</label>
		</div>
	</div>
</root>

@code
{
	DecorationType decorationType = DecorationType.Prop;

	Color StartingRedColor => Decoration.GetTintComponent().IsValid() ? Decoration.GetTintComponent().PrimaryColor : Color.White;
	Color StartingGreenColor => Decoration.GetTintComponent().IsValid() ? Decoration.GetTintComponent().SecondaryColor : Color.White;
	Color StartingBlueColor => Decoration.GetTintComponent().IsValid() ? Decoration.GetTintComponent().AccentColor : Color.White;
	Color StartingAlphaColor => Decoration.GetTintComponent().IsValid() ? Decoration.GetTintComponent().DetailColor : Color.White;

	Color NewRedColor = Color.White;
	Color NewGreenColor = Color.White;
	Color NewBlueColor = Color.White;
	Color NewAlphaColor = Color.White;

	private Decoration _decoration;

	public Decoration Decoration
	{
		get => _decoration;
		set
		{
			if (value == _decoration) return;
			_decoration = value;

			if (_decoration.IsValid())
			{
				var deco = _decoration.GetTintComponent();
				NewRedColor = deco.IsValid() ? deco.PrimaryColor : Color.White;
				NewGreenColor = deco.IsValid() ? deco.SecondaryColor : Color.White;
				NewBlueColor = deco.IsValid() ? deco.AccentColor : Color.White;
				NewAlphaColor = deco.IsValid() ? deco.DetailColor : Color.White;
			}
		}
	}


	protected override void OnAfterTreeRender(bool firstTime)
	{
		base.OnAfterTreeRender(firstTime);

		if(firstTime)
		{
			if (!Decoration.IsValid()) return;
			// Set the initial color values
			NewRedColor = Decoration.GetTintComponent().IsValid() ? Decoration.GetTintComponent().PrimaryColor : Color.White;
			NewGreenColor = Decoration.GetTintComponent().IsValid() ? Decoration.GetTintComponent().SecondaryColor : Color.White;
			NewBlueColor = Decoration.GetTintComponent().IsValid() ? Decoration.GetTintComponent().AccentColor : Color.White;
			NewAlphaColor = Decoration.GetTintComponent().IsValid() ? Decoration.GetTintComponent().DetailColor : Color.White;
		}
	}

	void StartPlacing()
	{
		if (!Decoration.IsValid()) return;

		BuildingPlacer.Instance.StartPlacing( Decoration );

		// Reset selection
		SelectedColor = Color.White;
		SelectedTintChannel = TintChannel.Primary;

		Log.Info($"Placing {Decoration.Title}");

		// Apply tint colors from the selected decoration
		var deco = Decoration.GetTintComponent();
		if (deco.IsValid())
		{
			SelectedColor = NewRedColor;

			if (deco.PrimaryEnabled)
			{
				BuildingPlacer.Instance?.SetDecorationColor(NewRedColor, TintChannel.Primary);
			}
			else
			{
				BuildingPlacer.Instance?.SetDecorationColor(Color.White, TintChannel.Primary);
			}

			if (deco.SecondaryEnabled)
			{
				BuildingPlacer.Instance?.SetDecorationColor(NewGreenColor, TintChannel.Secondary);
			}
			else
			{
				BuildingPlacer.Instance?.SetDecorationColor(Color.White, TintChannel.Secondary);
			}

			if (deco.AccentEnabled)
			{
				BuildingPlacer.Instance?.SetDecorationColor(NewBlueColor, TintChannel.Accent);
			}

			else
			{
				BuildingPlacer.Instance?.SetDecorationColor(Color.White, TintChannel.Accent);

			}
			if (deco.DetailEnabled)
			{
				BuildingPlacer.Instance?.SetDecorationColor(NewAlphaColor, TintChannel.Detail);
			}
			else
			{
				BuildingPlacer.Instance?.SetDecorationColor(Color.White, TintChannel.Detail);

			}
		}

		//CloseCanvas("FeaturesCanvas");
	}

	protected override int BuildHash() => System.HashCode.Combine(base.BuildHash(), BuildingPlacer.Instance?.IsDestroying, BuildingPlacer.Instance.PlacingResource, SelectedColor, NewRedColor, NewGreenColor, NewBlueColor, NewAlphaColor);

	void SetType(DecorationType type)
	{
		decorationType = type;
	}

	TintChannel? SelectedTintChannel = TintChannel.Primary;
	Color SelectedColor = Color.White;
	ColorPicker colorPicker;

	void SetSelectedTint(TintChannel channel)
	{
		SelectedTintChannel = channel;

		var deco = BuildingPlacer.Instance.PlacingResource.GetTintComponent();
		if ( deco.IsValid() )
		{
			var color = channel switch
			{
				TintChannel.Primary => NewRedColor,
				TintChannel.Secondary => NewGreenColor,
				TintChannel.Accent => NewBlueColor,
				TintChannel.Detail => NewAlphaColor,
				_ => Color.White
			};

			if(!colorPicker.IsValid())
				colorPicker = new ColorPicker(color, OnColorPicked);

			colorPicker.SetColor(color);
			WindowManager.Instance.Open(colorPicker);
			colorPicker.SetPosition(Screen.Size / 2, Window.ScreenAlignment.Center);
		}
	}

	void OnColorPicked(Color color)
	{
		SelectedColor = color;

		if ( !SelectedTintChannel.HasValue || !BuildingPlacer.Instance.PlacingResource.IsValid() )
			return;

		// Apply to the selected tint channel
		switch ( SelectedTintChannel.Value )
		{
			case TintChannel.Primary:
				NewRedColor = color;
				break;
			case TintChannel.Secondary:
				NewGreenColor = color;
				break;
			case TintChannel.Accent:
				NewBlueColor = color;
				break;
			case TintChannel.Detail:
				NewAlphaColor = color;
				break;
		}

		// Send to the placer
		BuildingPlacer.Instance?.SetDecorationColor( color, SelectedTintChannel.Value );

		// Optionally trigger a re-render (if your framework needs it)
		StateHasChanged(); // uncomment if needed in your UI framework
	}
}