UI/Inspector/BuildingInspector.razor
@using Sandbox
@using Sandbox.UI
@using HC3
@using HC3.Rides

@namespace HC3.UI
@inherits Window

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

<root>
	<tabview>
        @if ( InspectorSheet.ShouldShow( SerializedObject ) )
        {
            <tab Icon="cog" Title="Operation">
                <InspectorSheet Properties=@SerializedObject />
            </tab>
        }

        @if (Building.GetTintComponent().IsValid() || Building is BasicRide)
        {
            <tab Icon="paintcan" Title="Appearance">
                @if (Building.GetTintComponent().IsValid())
                {
                    <div class="row">
                        <label>Color</label>
                        <div class="color-palette">
                            @if (Building.IsValid())
                            {
                                <div class="tint-swatch">
                                    @if (Building.GetTintComponent().PrimaryEnabled)
                                    {
                                        <div class="color-swatch @(SelectedBuildingTintChannel == TintChannel.Primary ? "selected" : "")" style=@($"background-color: {Building.GetTintComponent().PrimaryColor.Hex()}") onclick=@(() => SetSelectedBuildingTint(TintChannel.Primary))></div>
                                    }
                                    @if (Building.GetTintComponent().SecondaryEnabled)
                                    {
                                        <div class="color-swatch @(SelectedBuildingTintChannel == TintChannel.Secondary ? "selected" : "")" style=@($"background-color: {Building.GetTintComponent().SecondaryColor.Hex()}") onclick=@(() => SetSelectedBuildingTint(TintChannel.Secondary))></div>
                                    }
                                    @if (Building.GetTintComponent().AccentEnabled)
                                    {
                                        <div class="color-swatch @(SelectedBuildingTintChannel == TintChannel.Accent ? "selected" : "")" style=@($"background-color: {Building.GetTintComponent().AccentColor.Hex()}") onclick=@(() => SetSelectedBuildingTint(TintChannel.Accent))></div>
                                    }
                                    @if (Building.GetTintComponent().DetailEnabled)
                                    {
                                        <div class="color-swatch @(SelectedBuildingTintChannel == TintChannel.Detail ? "selected" : "")" style=@($"background-color: {Building.GetTintComponent().DetailColor.Hex()}") onclick=@(() => SetSelectedBuildingTint(TintChannel.Detail))></div>
                                    }
                                </div>
                            }

                        </div>
                    </div>
                }
                @if (Building is BasicRide ride)
                {
                    @if (ride.GetQueueTintComponent().IsValid())
                    {
                        var queueTint = ride.GetQueueTintComponent();
                        <div class="row">
                            <label>Queue</label>
                            <div class="color-palette">
                                <div class="tint-swatch">
                                    @if (queueTint.PrimaryEnabled)
                                    {
                                        <div class="color-swatch @(SelectedQueueTintChannel == TintChannel.Primary ? "selected" : "")" style=@($"background-color: {queueTint.PrimaryColor.Hex()}") onclick=@(() => SetSelectedQueueTint(TintChannel.Primary))></div>
                                    }
                                    @if (queueTint.SecondaryEnabled)
                                    {
                                        <div class="color-swatch @(SelectedQueueTintChannel == TintChannel.Secondary ? "selected" : "")" style=@($"background-color: {queueTint.SecondaryColor.Hex()}") onclick=@(() => SetSelectedQueueTint(TintChannel.Secondary))></div>
                                    }
                                    @if (queueTint.AccentEnabled)
                                    {
                                        <div class="color-swatch @(SelectedQueueTintChannel == TintChannel.Accent ? "selected" : "")" style=@($"background-color: {queueTint.AccentColor.Hex()}") onclick=@(() => SetSelectedQueueTint(TintChannel.Accent))></div>
                                    }
                                    @if (queueTint.DetailEnabled)
                                    {
                                        <div class="color-swatch @(SelectedQueueTintChannel == TintChannel.Detail ? "selected" : "")" style=@($"background-color: {queueTint.DetailColor.Hex()}") onclick=@(() => SetSelectedQueueTint(TintChannel.Detail))></div>
                                    }
                                </div>
                            </div>
                        </div>
                    }

                }
            </tab>
        }
		<tab Icon="tab_go" Title="Admission">
			<div class="row">
				<label>Admission Fee</label>
				<TextEntry Numeric=@(true) Prefix="@GameUtils.Currency" Text=@(Building.GuestCost.ToString()) OnTextEdited=@((string x) => { Building.GuestCost = int.Parse(x); }) />
			</div>
		</tab>
		@if ( Ride is not null )
		{
			<tab Icon="sound" Title="Music">
				<div class="row">
					<label>Music</label>					
					<DropDown Options=@(ResourceUtils.GetOptions<MusicTrack>()) [email protected]?.ResourcePath [email protected] />
				</div>
			</tab>
		}
		@if (DebugMode.Enabled)
		{
			<tab Icon="bug" Title="Debug">
				<div class="row">
					@if (Building is BasicRide ride)
					{
						<button [email protected]>Break down</button>
					}
			</div>
		</tab>
		}
	</tabview>

	<div class="errors">
		@foreach (var error in Building.Errors)
		{
			<div class="error">
				<i>error</i>
				<label>@error.Message</label>
			</div>
		}
	</div>

	<column>
		<label class="subtitle">@Building.GetStatus()</label>
		<label class="subtitle">@Building.GetUseString()</label>
	</column>

	<div class="footer">
		<div class="button" onclick=@(() => Destroy()) tooltip="Demolish"><i>delete</i></div>
		<div class="button" onclick=@(() => Rename()) tooltip="Rename"><i>label</i></div>
		@if ( Building is TrackRide )
		{
			<div class="button" onclick=@(() => EditTrack()) tooltip="Edit Track"><i>build</i></div>
		}
        @if ( Building is BasicRide )
        {
            <div class="button" onclick=@(() => BuildEntrance()) tooltip="Edit Entrance"><i>login</i></div>
            <div class="button" onclick=@(() => BuildExit()) tooltip="Edit Exit"><i>logout</i></div>
        }

		<div class="button" onclick=@(() => FrameTo()) tooltip="Go to"><i>zoom_in_map</i></div>
		<DropDown Value:[email protected]/>
	</div>
</root>

@code
{
    public Building Building { get; set; }
    public BasicRide Ride => Building as BasicRide;

    public override string Title => string.IsNullOrEmpty( Building?.CustomTitle ) ? Building?.Title : Building?.CustomTitle;
    public override string Icon => Building?.BuildingType.GetAttributeOfType<IconAttribute>()?.Value ?? null;
    public override object Key => Building;

    private SerializedObject SerializedObject;

    TintChannel? SelectedBuildingTintChannel = TintChannel.Primary;
    TintChannel? SelectedQueueTintChannel = TintChannel.Primary;

    ColorPicker colorPicker;

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

        SerializedObject = TypeLibrary.GetSerializedObject(Building);
    }

    private void Destroy()
    {
        ParkManager.Instance.DestroyObject( Building );
        Close();
    }

	private void BuildEntrance()
	{
		BuildingPlacer.Instance.StartPlacingEntrance((BasicRide)Building);
	}

	private void BuildExit()
	{
		BuildingPlacer.Instance.StartPlacingExit((BasicRide)Building);
	}

	private void EditTrack()
	{
		TrackBuilder.Instance.StartBuilding((TrackRide)Building);
	}

	private void Rename()
	{
		var window = new RenameDialog($"Rename {Building.Title}", Building.CustomTitle, (s) => Building.SetBuildingTitle( s ) );
		OpenSubwindow( window );
	}

	private void FrameTo()
	{
		CameraPanning.Instance.FrameOn( Building.GridObject.GetWorldBounds() );
	}

	void SetSelectedBuildingTint(TintChannel channel)
	{
		SelectedBuildingTintChannel = channel;

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

	void SetSelectedQueueTint(TintChannel channel)
	{
		SelectedQueueTintChannel = channel;

		if (colorPicker == null || !colorPicker.IsValid())
		{
			colorPicker = new ColorPicker(ColorFromQueueChannel(), OnColorPickedQueue);
			OpenSubwindow(colorPicker);
		}
		else
		{
			colorPicker.OnSubmit = OnColorPickedQueue;
			colorPicker.SetColor(ColorFromQueueChannel());
			OpenSubwindow(colorPicker);
		}
	}

	Color ColorFromBuildingChannel()
	{
		var deco = Building.GetTintComponent();
		if (!deco.IsValid()) return Color.White;

		return SelectedBuildingTintChannel switch
		{
			TintChannel.Primary => deco.PrimaryColor,
			TintChannel.Secondary => deco.SecondaryColor,
			TintChannel.Accent => deco.AccentColor,
			TintChannel.Detail => deco.DetailColor,
			_ => Color.White
		};
	}

	Color ColorFromQueueChannel()
	{
		if (Building is not BasicRide ride || !ride.GetQueueTintComponent().IsValid()) return Color.White;
		var queue = ride.GetQueueTintComponent();

		return SelectedQueueTintChannel switch
		{
			TintChannel.Primary => queue.PrimaryColor,
			TintChannel.Secondary => queue.SecondaryColor,
			TintChannel.Accent => queue.AccentColor,
			TintChannel.Detail => queue.DetailColor,
			_ => Color.White
		};
	}

	void OnColorPickedQueue(Color color)
	{
		if (!SelectedQueueTintChannel.HasValue || !Building.IsValid())
			return;
		if (Building is BasicRide ride)
		{
			if (ride.GetQueueTintComponent().IsValid())
			{
				if (SelectedQueueTintChannel.Value == TintChannel.Primary)
				{
					ride.UpdateQueueColor(color, TintChannel.Primary);
				}
				else if (SelectedQueueTintChannel.Value == TintChannel.Secondary)
				{
					ride.UpdateQueueColor(color, TintChannel.Secondary);
				}
				else if (SelectedQueueTintChannel.Value == TintChannel.Accent)
				{
					ride.UpdateQueueColor(color, TintChannel.Accent);
				}
				else if (SelectedQueueTintChannel.Value == TintChannel.Detail)
				{
					ride.UpdateQueueColor(color, TintChannel.Detail);
				}
			}
		}
	}

	void OnColorPicked( Color color )
	{		
		var deco = Building.GetTintComponent();
		if ( !deco.IsValid() ) return;

		if ( SelectedBuildingTintChannel.Value == TintChannel.Primary )
		{
			if ( deco.PrimaryColor != color )
				deco.SetPrimaryColor( color );
		}
		else if (SelectedBuildingTintChannel.Value == TintChannel.Secondary)
		{
			if ( deco.SecondaryColor != color )
				deco.SetSecondaryColor( color );
		}
		else if (SelectedBuildingTintChannel.Value == TintChannel.Accent)
		{
			if ( deco.AccentColor != color )
				deco.SetAccentColor( color );
		}
		else if (SelectedBuildingTintChannel.Value == TintChannel.Detail)
		{
			if ( deco.DetailColor != color )
				deco.SetDetailColor( color );
		}
	}

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