<adonisControls:AdonisWindow x:Class="FModel.Views.ModelViewer"
                             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                             xmlns:converters="clr-namespace:FModel.Views.Resources.Converters"
                             xmlns:adonisUi="clr-namespace:AdonisUI;assembly=AdonisUI"
                             xmlns:adonisControls="clr-namespace:AdonisUI.Controls;assembly=AdonisUI"
                             xmlns:helix="http://helix-toolkit.org/wpf/SharpDX"
                             WindowStartupLocation="CenterScreen" ResizeMode="CanResize" IconVisibility="Collapsed"
                             PreviewKeyDown="OnWindowKeyDown" Closing="OnClosing"
                             Height="{Binding Source={x:Static SystemParameters.MaximizedPrimaryScreenHeight}, Converter={converters:RatioConverter}, ConverterParameter='0.60'}"
                             Width="{Binding Source={x:Static SystemParameters.MaximizedPrimaryScreenWidth}, Converter={converters:RatioConverter}, ConverterParameter='0.60'}">
    <adonisControls:AdonisWindow.Style>
        <Style TargetType="adonisControls:AdonisWindow" BasedOn="{StaticResource {x:Type adonisControls:AdonisWindow}}" >
            <Setter Property="Title" Value="Model Viewer" />
        </Style>
    </adonisControls:AdonisWindow.Style>
    <adonisControls:AdonisWindow.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Resources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </adonisControls:AdonisWindow.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="350" MinWidth="250" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="4*" />
        </Grid.ColumnDefinitions>

        <GroupBox Grid.Column="0" Padding="{adonisUi:Space 0}" Background="Transparent">
            <DockPanel Margin="10">
                <Grid DockPanel.Dock="Top">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="10" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Models" VerticalAlignment="Center" Margin="0 0 0 10" />
                    <ComboBox Grid.Row="0" Grid.Column="2" Style="{StaticResource ModelsComboBox}" IsEnabled="{Binding IsReady}" />

                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Triangles" VerticalAlignment="Center" Margin="0 0 0 10" />
                    <TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding ModelViewer.SelectedModel.TriangleCount, FallbackValue=0, StringFormat={}{0:### ### ### ###}}" VerticalAlignment="Center" HorizontalAlignment="Right" />

                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Materials" VerticalAlignment="Center" Margin="0 0 0 10" />
                    <TextBlock Grid.Row="2" Grid.Column="2" Text="{Binding ModelViewer.SelectedModel.Group3d.Count, FallbackValue=0, StringFormat={}{0:### ###}}" VerticalAlignment="Center" HorizontalAlignment="Right" />

                    <Grid Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="10" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="10" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

                        <Button Grid.Row="0" Grid.Column="0" Content="Focus" Click="OnFocusClick" IsEnabled="{Binding IsReady}" />
                        <ToggleButton Grid.Row="0" Grid.Column="2" IsChecked="{Binding ModelViewer.AppendMode}" IsEnabled="{Binding IsReady}"
                                      Style="{DynamicResource {x:Static adonisUi:Styles.ToolbarToggleButton}}">
                            <TextBlock Text="{Binding IsChecked, Converter={x:Static converters:BoolToToggleConverter.Instance},
                                    StringFormat={}Append {0}, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ToggleButton}}}" />
                        </ToggleButton>

                        <Button Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Click="Save" IsEnabled="{Binding IsReady}"
                                Style="{DynamicResource {x:Static adonisUi:Styles.AccentButton}}">
                            <TextBlock Text="{Binding ModelViewer.LoadedModelsView.Count, StringFormat={}Save All Loaded Models ({0})}" />
                        </Button>
                    </Grid>
                </Grid>
                <Separator DockPanel.Dock="Top" Tag="MATERIALS" Style="{StaticResource CustomSeparator}" />
                <ListBox x:Name="MaterialsListName" DockPanel.Dock="Top" Style="{StaticResource MaterialsListBox}" IsEnabled="{Binding IsReady}">
                    <ListBox.ContextMenu>
                        <ContextMenu DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">
                            <MenuItem Header="Copy Name" Click="OnCopyClick">
                                <MenuItem.Icon>
                                    <Viewbox Width="16" Height="16">
                                        <Canvas Width="24" Height="24">
                                            <Path Fill="{DynamicResource {x:Static adonisUi:Brushes.ForegroundBrush}}" Data="{StaticResource CopyIcon}" />
                                        </Canvas>
                                    </Viewbox>
                                </MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Header="Overwrite Material" Click="OnOverwriteMaterialClick">
                                <MenuItem.Icon>
                                    <Viewbox Width="16" Height="16">
                                        <Canvas Width="24" Height="24">
                                            <Path Fill="{DynamicResource {x:Static adonisUi:Brushes.ForegroundBrush}}" Data="{StaticResource OverwriteIcon}" />
                                        </Canvas>
                                    </Viewbox>
                                </MenuItem.Icon>
                            </MenuItem>
                        </ContextMenu>
                    </ListBox.ContextMenu>
                </ListBox>
            </DockPanel>
        </GroupBox>

        <GridSplitter Grid.Column="1" ResizeDirection="Columns" Width="4" VerticalAlignment="Stretch" ResizeBehavior="PreviousAndNext"
                      Background="{DynamicResource {x:Static adonisUi:Brushes.Layer3BackgroundBrush}}" />

        <TextBlock Grid.Column="2" Text="Model is loading, please wait..." HorizontalAlignment="Center" VerticalAlignment="Center"/>
        <helix:Viewport3DX Grid.Column="2" EffectsManager="{Binding ModelViewer.EffectManager}" Camera="{Binding ModelViewer.Cam}"
                           ShowViewCube="False" IsChangeFieldOfViewEnabled="False" IsMoveEnabled="False" UseDefaultGestures="False"
                           ShowCameraTarget="False" FXAALevel="Ultra" MSAA="Maximum" BackgroundColor="#2A2B34"
                           EnableSSAO="True" SSAOIntensity="1" EnableSwapChainRendering="True">
            <helix:Viewport3DX.InputBindings>
                <MouseBinding Command="helix:ViewportCommands.Rotate" Gesture="LeftClick" />
                <MouseBinding Command="helix:ViewportCommands.Zoom" Gesture="RightClick" />
                <MouseBinding Command="helix:ViewportCommands.Pan" Gesture="MiddleClick" />
                <KeyBinding Command="helix:ViewportCommands.ZoomExtents" Modifiers="Shift" Key="C"/>
            </helix:Viewport3DX.InputBindings>

            <helix:EnvironmentMap3D Texture="{Binding ModelViewer.HDRi}" />
            <helix:DirectionalLight3D Color="White" Direction="{Binding Camera.LookDirection,
                RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type helix:Viewport3DX}}}" />

            <helix:LineGeometryModel3D Geometry="{Binding ModelViewer.SelectedModel.XAxis}" Color="#FC3854" />
            <helix:LineGeometryModel3D Geometry="{Binding ModelViewer.SelectedModel.YAxis}" Color="#85CB22" />
            <helix:LineGeometryModel3D Geometry="{Binding ModelViewer.SelectedModel.ZAxis}" Color="#388EED" />

            <helix:GroupModel3D x:Name="MyAntiCrashGroup" Mouse3DDown="OnMouse3DDown"
                                ItemsSource="{Binding ModelViewer.SelectedModel.Group3d, IsAsync=True}"/>
        </helix:Viewport3DX>
    </Grid>
</adonisControls:AdonisWindow>
