UI/Inspector/DecorationInspector.razor
@using Sandbox
@using Sandbox.UI

@namespace HC3.UI
@inherits Window

@if (!Building.IsValid())
{
	Close();
	return;
}

<root class="inspector">
	<tabview>
		@if (Properties.Any())
		{
			<tab Icon="pencil" Title="Operation">			
				<InspectorSheet Properties=@Properties/>
			</tab>
		}
		@if (Building.IsValid())
		{
			var deco = Building.GetTintComponent();
			if (deco.IsValid())
			{
				<tab Icon="paintcan" Title="Appearance">
					<div class="row">
						<label>Color</label>
						<div class="color-palette">
							<div class="tint-swatch">
								@if (deco.PrimaryEnabled)
								{
									<div class="color-swatch @(SelectedTintChannel == TintChannel.Primary ? "selected" : "")" style=@($"background-color: {deco.PrimaryColor.Hex()}") onclick=@(() => SetSelectedTint(TintChannel.Primary))></div>
								}
								@if (deco.SecondaryEnabled)
								{
									<div class="color-swatch @(SelectedTintChannel == TintChannel.Secondary ? "selected" : "")" style=@($"background-color: {deco.SecondaryColor.Hex()}") onclick=@(() => SetSelectedTint(TintChannel.Secondary))></div>
								}
								@if (deco.AccentEnabled)
								{
									<div class="color-swatch @(SelectedTintChannel == TintChannel.Accent ? "selected" : "")" style=@($"background-color: {deco.AccentColor.Hex()}") onclick=@(() => SetSelectedTint(TintChannel.Accent))></div>
								}
								@if (deco.DetailEnabled)
								{
									<div class="color-swatch @(SelectedTintChannel == TintChannel.Detail ? "selected" : "")" style=@($"background-color: {deco.DetailColor.Hex()}") onclick=@(() => SetSelectedTint(TintChannel.Detail))></div>
								}
							</div>
						</div>
					</div>
				</tab>
			}
		}
	</tabview>

	<div class="footer">
        <div class="button" onclick=@(() => Destroy()) tooltip="Demolish"><i>delete</i></div>
        <div class="button" onclick=@(() => Duplicate()) tooltip="Duplicate"><i>content_copy</i></div>
    </div>
</root>

@code
{
	public IPlacementObject Building { get; set; }

	public override string Title => Building?.Title;
	public override object Key => Building;	

	private SerializedObject SerializedObject;
	private IEnumerable<SerializedProperty> Properties;

	TintChannel? SelectedTintChannel = TintChannel.Primary;

	ColorPicker colorPicker;

	void SetSelectedTint(TintChannel channel)
	{
		SelectedTintChannel = channel;

		if (colorPicker == null || !colorPicker.IsValid())
		{
			colorPicker = new ColorPicker(ColorFromChannel(), OnColorPicked);
			OpenSubwindow(colorPicker);
		}
		else
		{
			colorPicker.SetColor(ColorFromChannel());
			OpenSubwindow(colorPicker);
		}
	}

	protected override void OnParametersSet()
	{
		base.OnParametersSet();

		SerializedObject = TypeLibrary.GetSerializedObject( Building );
		Properties = SerializedObject.Where(x => x.HasAttribute<InspectableAttribute>());
	}

	private void Destroy()
	{
		ParkManager.Instance.DestroyObject( Building as Component );
		Close();
	}
	
	private Color ColorFromChannel()
	{
		var deco = Building.GetTintComponent();
		if ( !deco.IsValid() ) return Color.White;
		
		switch ( SelectedTintChannel.Value )
		{
			case TintChannel.Primary:
				return deco.PrimaryColor;
			case TintChannel.Secondary:
				return deco.SecondaryColor;
			case TintChannel.Accent:
				return deco.AccentColor;
			case TintChannel.Detail:
				return deco.DetailColor;
		}

		return Color.White;
	}

	void OnColorPicked( Color color )
	{
		if ( !SelectedTintChannel.HasValue || !Building.IsValid() )
			return;
		
		var deco = Building.GetTintComponent();
		if ( !deco.IsValid() ) return;
			
		if ( SelectedTintChannel.Value == TintChannel.Primary )
		{
			if ( deco.PrimaryColor != color )
				deco.SetPrimaryColor( color );
		}
		else if ( SelectedTintChannel.Value == TintChannel.Secondary )
		{
			if ( deco.SecondaryColor != color )
				deco.SetSecondaryColor( color );
		}
		else if ( SelectedTintChannel.Value == TintChannel.Accent )
		{
			if ( deco.AccentColor != color )
				deco.SetAccentColor( color );
		}
		else if ( SelectedTintChannel.Value == TintChannel.Detail )
		{
			if ( deco.DetailColor != color )
				deco.SetDetailColor( color );
		}
	}

    void Duplicate()
    {
        BuildingPlacer.Instance.StartPlacing( Building );
    }

    protected override int BuildHash() => System.HashCode.Combine(Time.Now);
}