<?xml version="1.0" encoding="utf-8"?>
<Page
    x:Class="Sonex.Client.Pages.Laboratory.SceneCanvasLab"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:lab="using:Sonex.Client.Pages.Laboratory"
    xmlns:controls="using:Sonex.Client.Controls"
    xmlns:scene="using:Sonex.Client.Controls.SceneCanvas"
    xmlns:toolkit="using:CommunityToolkit.WinUI.Controls"
    mc:Ignorable="d">

    <Page.Resources>
        <lab:EmptyStringToVisibilityConverter x:Key="EmptyStringToVisibilityConverter" />
    </Page.Resources>

    <Grid Background="{StaticResource SolidBackgroundFillColorBaseBrush}" Padding="8" RowSpacing="8">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <scene:SceneCanvasToolBar x:Name="_ToolBar" Grid.Row="1" />

        <Grid Grid.Row="2" ColumnSpacing="8">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="280" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="280" />
            </Grid.ColumnDefinitions>

            <Grid x:Name="_LeftPanelsHost" Grid.Column="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <Border
                    x:Name="_LeftTopPanel"
                    Grid.Row="0"
                    MinHeight="80"
                    CornerRadius="10"
                    BorderThickness="1"
                    Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                    BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}">
                    <Grid RowSpacing="0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>

                        <TextBlock
                            Grid.Row="0"
                            Margin="8,8,8,4"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            TextAlignment="Center"
                            FontWeight="SemiBold"
                            Foreground="{ThemeResource TextFillColorSecondaryBrush}"
                            Text="Layers" />

                        <controls:ToolBar
                            Grid.Row="1"
                            HorizontalAlignment="Stretch"
                            ChromePadding="8,6,8,6"
                            ChromeCornerRadius="0"
                            ChromeBorderThickness="0,1,0,1">
                            <controls:ToolBarButton
                                x:Name="_AddLayerPanelButton"
                                Click="AddLayerPanelButton_Click"
                                ToolTipService.ToolTip="New layer">
                                <FontIcon Glyph="&#xE710;" />
                            </controls:ToolBarButton>
                            <controls:ToolBarButton
                                x:Name="_DeleteLayerPanelButton"
                                Click="DeleteLayerPanelButton_Click"
                                ToolTipService.ToolTip="Delete layer">
                                <FontIcon Glyph="&#xE74D;" />
                            </controls:ToolBarButton>
                            <controls:ToolBarButton
                                x:Name="_EditLayerPanelButton"
                                Click="EditLayerPanelButton_Click"
                                ToolTipService.ToolTip="Edit layer">
                                <FontIcon Glyph="&#xE70F;" />
                            </controls:ToolBarButton>
                            <controls:ToolBarButton
                                x:Name="_CloneLayerPanelButton"
                                Click="CloneLayerPanelButton_Click"
                                ToolTipService.ToolTip="Clone layer with objects">
                                <FontIcon Glyph="&#xE8C8;" />
                            </controls:ToolBarButton>
                            <controls:ToolBarButton
                                x:Name="_MoveLayerUpPanelButton"
                                Click="MoveLayerUpPanelButton_Click"
                                ToolTipService.ToolTip="Move layer up">
                                <FontIcon Glyph="&#xE74A;" />
                            </controls:ToolBarButton>
                            <controls:ToolBarButton
                                x:Name="_MoveLayerDownPanelButton"
                                Click="MoveLayerDownPanelButton_Click"
                                ToolTipService.ToolTip="Move layer down">
                                <FontIcon Glyph="&#xE74B;" />
                            </controls:ToolBarButton>
                        </controls:ToolBar>

                        <ListView
                            x:Name="_LayersListView"
                            Grid.Row="2"
                            Margin="8,8,8,8"
                            HorizontalContentAlignment="Stretch"
                            SelectionChanged="LayersListView_SelectionChanged">
                            <ListView.Transitions>
                                <TransitionCollection />
                            </ListView.Transitions>
                            <ListView.ItemContainerTransitions>
                                <TransitionCollection />
                            </ListView.ItemContainerTransitions>
                            <ListView.ItemContainerStyle>
                                <Style TargetType="ListViewItem">
                                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                                    <Setter Property="MinHeight" Value="36" />
                                    <Setter Property="Padding" Value="0" />
                                </Style>
                            </ListView.ItemContainerStyle>
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <Grid Padding="4,2" ColumnSpacing="4">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <CheckBox
                                            Grid.Column="0"
                                            Width="20"
                                            Height="20"
                                            HorizontalAlignment="Left"
                                            VerticalAlignment="Center"
                                            IsChecked="{Binding IsVisible, Mode=TwoWay}"
                                            Tag="{Binding Id}"
                                            Checked="LayerVisibilityCheckBox_Changed"
                                            Unchecked="LayerVisibilityCheckBox_Changed" />
                                        <StackPanel Grid.Column="1" Spacing="1">
                                            <TextBlock Text="{Binding Name}" TextTrimming="CharacterEllipsis" />
                                            <TextBlock
                                                Opacity="0.75"
                                                Text="{Binding Description}"
                                                Visibility="{Binding Description, Converter={StaticResource EmptyStringToVisibilityConverter}}"
                                                TextTrimming="CharacterEllipsis"
                                                MaxLines="1" />
                                        </StackPanel>
                                    </Grid>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </Grid>
                </Border>

                <toolkit:ContentSizer
                    x:Name="_LeftPanelContentSizer"
                    Grid.Row="1"
                    Height="8"
                    HorizontalAlignment="Stretch"
                    VerticalAlignment="Top"
                    Orientation="Horizontal"
                    TargetControl="{x:Bind _LeftTopPanel}" />

                <Border
                    x:Name="_LeftBottomPanel"
                    Grid.Row="2"
                    CornerRadius="10"
                    BorderThickness="1"
                    Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                    BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}">
                    <Grid RowSpacing="0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>

                        <TextBlock
                            Grid.Row="0"
                            Margin="8,8,8,4"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            TextAlignment="Center"
                            FontWeight="SemiBold"
                            Foreground="{ThemeResource TextFillColorSecondaryBrush}"
                            Text="Object Library" />

                        <controls:ToolBar
                            Grid.Row="1"
                            HorizontalAlignment="Stretch"
                            ChromePadding="8,6,8,6"
                            ChromeCornerRadius="0"
                            ChromeBorderThickness="0,1,0,1">
                            <ComboBox
                                x:Name="_LibraryCategoryComboBox"
                                MinWidth="140"
                                SelectionChanged="LibraryCategoryComboBox_SelectionChanged" />
                            <controls:ToolBarButton
                                x:Name="_AddLibraryObjectButton"
                                Click="AddLibraryObjectButton_Click"
                                ToolTipService.ToolTip="Add object definition">
                                <FontIcon Glyph="&#xE710;" />
                            </controls:ToolBarButton>
                            <controls:ToolBarButton
                                x:Name="_EditLibraryObjectButton"
                                Click="EditLibraryObjectButton_Click"
                                ToolTipService.ToolTip="Edit object definition">
                                <FontIcon Glyph="&#xE70F;" />
                            </controls:ToolBarButton>
                            <controls:ToolBarButton
                                x:Name="_DeleteLibraryObjectButton"
                                Click="DeleteLibraryObjectButton_Click"
                                ToolTipService.ToolTip="Delete object definition">
                                <FontIcon Glyph="&#xE74D;" />
                            </controls:ToolBarButton>
                        </controls:ToolBar>

                        <ListView
                            x:Name="_LibraryObjectsListView"
                            Grid.Row="2"
                            Margin="8,8,8,8"
                            CanDragItems="True"
                            HorizontalContentAlignment="Stretch"
                            SelectionChanged="LibraryObjectsListView_SelectionChanged"
                            DragItemsStarting="LibraryObjectsListView_DragItemsStarting"
                            DragItemsCompleted="LibraryObjectsListView_DragItemsCompleted"
                            DoubleTapped="LibraryObjectsListView_DoubleTapped">
                            <ListView.Transitions>
                                <TransitionCollection />
                            </ListView.Transitions>
                            <ListView.ItemContainerTransitions>
                                <TransitionCollection />
                            </ListView.ItemContainerTransitions>
                            <ListView.ItemContainerStyle>
                                <Style TargetType="ListViewItem">
                                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                                    <Setter Property="MinHeight" Value="36" />
                                    <Setter Property="Padding" Value="0" />
                                </Style>
                            </ListView.ItemContainerStyle>
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Padding="4,2" Spacing="2">
                                        <TextBlock Text="{Binding Name}" TextTrimming="CharacterEllipsis" />
                                        <TextBlock
                                            Opacity="0.75"
                                            Text="{Binding Description}"
                                            Visibility="{Binding Description, Converter={StaticResource EmptyStringToVisibilityConverter}}"
                                            TextTrimming="CharacterEllipsis"
                                            MaxLines="1" />
                                    </StackPanel>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </Grid>
                </Border>
            </Grid>

            <scene:SceneCanvas
                x:Name="_SceneCanvas"
                Grid.Column="1"
                AllowDrop="True"
                DragEnter="SceneCanvas_DragEnter"
                DragOver="SceneCanvas_DragOver"
                DragLeave="SceneCanvas_DragLeave"
                Drop="SceneCanvas_Drop" />

            <Border
                x:Name="_RightPanel"
                Grid.Column="2"
                CornerRadius="10"
                BorderThickness="1"
                Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}">
                <Grid Padding="8" RowSpacing="8">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <TextBlock
                        Grid.Row="0"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        TextAlignment="Center"
                        FontWeight="SemiBold"
                        Foreground="{ThemeResource TextFillColorSecondaryBrush}"
                        Text="Properties" />

                    <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto">
                        <StackPanel Spacing="8">
                            <TextBlock
                                x:Name="_PropertiesPlaceholderTextBlock"
                                Text="Select an object to edit its properties."
                                Opacity="0.75" />

                            <Border
                                x:Name="_ShapePropertiesPanel"
                                Padding="8"
                                CornerRadius="8"
                                Background="{ThemeResource LayerFillColorDefaultBrush}"
                                Visibility="Collapsed">
                                <StackPanel Spacing="6">
                                    <TextBlock FontWeight="SemiBold" Text="Shape" />
                                    <Grid ColumnSpacing="6" RowSpacing="6">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="Auto" />
                                        </Grid.RowDefinitions>

                                        <controls:ToolBarNumberField x:Name="_ShapePanelXNumberBox" Grid.Row="0" Grid.Column="0" Label="X:" SmallChange="0.01" ValueChanged="ShapePanelNumberBox_ValueChanged" />
                                        <controls:ToolBarNumberField x:Name="_ShapePanelYNumberBox" Grid.Row="0" Grid.Column="1" Label="Y:" SmallChange="0.01" ValueChanged="ShapePanelNumberBox_ValueChanged" />
                                        <controls:ToolBarNumberField x:Name="_ShapePanelZNumberBox" Grid.Row="1" Grid.Column="0" Label="Z:" SmallChange="0.01" ValueChanged="ShapePanelNumberBox_ValueChanged" />
                                        <controls:ToolBarNumberField x:Name="_ShapePanelRotationNumberBox" Grid.Row="1" Grid.Column="1" Label="R:" SmallChange="0.01" ValueChanged="ShapePanelNumberBox_ValueChanged" />
                                        <controls:ToolBarNumberField x:Name="_ShapePanelWidthNumberBox" Grid.Row="2" Grid.Column="0" Label="W:" SmallChange="0.01" ValueChanged="ShapePanelNumberBox_ValueChanged" />
                                        <controls:ToolBarNumberField x:Name="_ShapePanelHeightNumberBox" Grid.Row="2" Grid.Column="1" Label="H:" SmallChange="0.01" ValueChanged="ShapePanelNumberBox_ValueChanged" />
                                    </Grid>
                                </StackPanel>
                            </Border>

                            <Border
                                x:Name="_LinePropertiesPanel"
                                Padding="8"
                                CornerRadius="8"
                                Background="{ThemeResource LayerFillColorDefaultBrush}"
                                Visibility="Collapsed">
                                <StackPanel Spacing="6">
                                    <TextBlock FontWeight="SemiBold" Text="Line" />
                                    <Grid ColumnSpacing="6" RowSpacing="6">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="Auto" />
                                        </Grid.RowDefinitions>

                                        <controls:ToolBarNumberField x:Name="_LinePanelStartXNumberBox" Grid.Row="0" Grid.Column="0" Label="X1:" SmallChange="0.01" ValueChanged="LinePanelNumberBox_ValueChanged" />
                                        <controls:ToolBarNumberField x:Name="_LinePanelStartYNumberBox" Grid.Row="0" Grid.Column="1" Label="Y1:" SmallChange="0.01" ValueChanged="LinePanelNumberBox_ValueChanged" />
                                        <controls:ToolBarNumberField x:Name="_LinePanelStartZNumberBox" Grid.Row="1" Grid.Column="0" Label="Z1:" SmallChange="0.01" ValueChanged="LinePanelNumberBox_ValueChanged" />
                                        <controls:ToolBarNumberField x:Name="_LinePanelThicknessNumberBox" Grid.Row="1" Grid.Column="1" Label="T:" SmallChange="0.01" ValueChanged="LinePanelNumberBox_ValueChanged" />
                                        <controls:ToolBarNumberField x:Name="_LinePanelEndXNumberBox" Grid.Row="2" Grid.Column="0" Label="X2:" SmallChange="0.01" ValueChanged="LinePanelNumberBox_ValueChanged" />
                                        <controls:ToolBarNumberField x:Name="_LinePanelEndYNumberBox" Grid.Row="2" Grid.Column="1" Label="Y2:" SmallChange="0.01" ValueChanged="LinePanelNumberBox_ValueChanged" />
                                        <controls:ToolBarNumberField x:Name="_LinePanelEndZNumberBox" Grid.Row="3" Grid.Column="0" Label="Z2:" SmallChange="0.01" ValueChanged="LinePanelNumberBox_ValueChanged" />
                                    </Grid>
                                </StackPanel>
                            </Border>
                        </StackPanel>
                    </ScrollViewer>
                </Grid>
            </Border>
        </Grid>
    </Grid>
</Page>
