<UserControl x:Class="Artemis.UI.Shared.Input.DataModelDynamicView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:shared="clr-namespace:Artemis.UI.Shared"
             xmlns:input="clr-namespace:Artemis.UI.Shared.Input"
             xmlns:s="https://github.com/canton7/Stylet"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800"
             d:DataContext="{d:DesignInstance input:DataModelDynamicViewModel}">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Artemis.UI.Shared;component/ResourceDictionaries/DataModelConditions.xaml" />
                <ResourceDictionary>
                    <shared:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
                    <shared:BindingProxy x:Key="DataContextProxy" Data="{Binding}" />
                    <DataTemplate x:Key="DataModelDataTemplate">
                        <Control x:Name="TemplateControl" Focusable="False" Template="{StaticResource DataModelSelectionTemplate}" />
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Data.ShowDataModelValues.Value, Source={StaticResource DataContextProxy}}" Value="True">
                                <Setter TargetName="TemplateControl" Property="Template" Value="{StaticResource DataModelSelectionTemplateWithValues}" />
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                    <Style x:Key="DataModelConditionButtonCornerToggle" BasedOn="{StaticResource DataModelConditionButton}" TargetType="Button">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding DisplaySwitchButton}" Value="True">
                                <Setter Property="materialDesign:ButtonAssist.CornerRadius" Value="0 2 2 0" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding DisplaySwitchButton}" Value="False">
                                <Setter Property="materialDesign:ButtonAssist.CornerRadius" Value="2" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <StackPanel Orientation="Horizontal">
        <Button Style="{StaticResource MaterialDesignFlatDarkBgButton}"
                Background="{Binding SwitchButtonBrush}"
                BorderBrush="{Binding SwitchButtonBrush}"
                Height="22"
                Padding="0"
                Width="22"
                FontSize="12"
                materialDesign:ButtonAssist.CornerRadius="2 0 0 2"
                Margin="3 0 -3 0"
                HorizontalAlignment="Left"
                ToolTip="Switch to manual input"
                Command="{s:Action SwitchToStatic}"
                Visibility="{Binding DisplaySwitchButton, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}">
            <materialDesign:PackIcon Kind="SwapHorizontal" />
        </Button>
        <Button Background="{Binding ButtonBrush}"
                BorderBrush="{Binding ButtonBrush}"
                Style="{StaticResource DataModelConditionButtonCornerToggle}"
                ToolTip="{Binding DisplayPath}"
                IsEnabled="{Binding IsEnabled}"
                HorizontalAlignment="Left"
                Click="PropertyButton_OnClick">
            <Button.ContextMenu>
                <ContextMenu IsOpen="{Binding IsDataModelViewModelOpen, Mode=OneWayToSource}">
                    <ContextMenu.Resources>
                        <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource MaterialDesignMenuItem}">
                            <Setter Property="ItemsSource" Value="{Binding Children}" />
                            <Setter Property="Command" Value="{Binding Data.SelectPropertyCommand, Source={StaticResource DataContextProxy}}" />
                            <Setter Property="CommandParameter" Value="{Binding}" />
                            <Setter Property="CommandTarget" Value="{Binding}" />
                            <Setter Property="IsEnabled" Value="{Binding IsMatchingFilteredTypes}" />
                            <Setter Property="IsSubmenuOpen" Value="{Binding IsVisualizationExpanded, Mode=TwoWay}" />
                            <Setter Property="HeaderTemplate" Value="{StaticResource DataModelDataTemplate}" />
                        </Style>
                    </ContextMenu.Resources>
                    <ContextMenu.ItemsSource>
                        <CompositeCollection>
                            <MenuItem Header="No modules available" IsEnabled="False" Visibility="{Binding Data.HasNoModules, Source={StaticResource DataContextProxy}, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}">
                                <MenuItem.Icon>
                                    <materialDesign:PackIcon Kind="EmojiSad" />
                                </MenuItem.Icon>
                            </MenuItem>
                            <CollectionContainer Collection="{Binding Data.ExtraDataModelViewModels, Source={StaticResource DataContextProxy}}" />
                            <Separator Visibility="{Binding Data.HasExtraDataModels, Source={StaticResource DataContextProxy}, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}"/>
                            <CollectionContainer Collection="{Binding Data.DataModelViewModel.Children, Source={StaticResource DataContextProxy}}" />
                        </CompositeCollection>
                    </ContextMenu.ItemsSource>
                </ContextMenu>
            </Button.ContextMenu>
            <Grid>
                <Grid Visibility="{Binding IsValid,Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}">
                    <TextBlock Text="{Binding DisplayValue}"
                               Visibility="{Binding DataModelPath, Converter={StaticResource NullToVisibilityConverter}}" />
                    <TextBlock FontStyle="Italic"
                               Visibility="{Binding DataModelPath, Converter={StaticResource NullToVisibilityConverter}, ConverterParameter=Inverted}">
                        <Run Text="« " /><Run Text="{Binding Placeholder}" /><Run Text=" »" />
                    </TextBlock>
                </Grid>
                <TextBlock FontStyle="Italic"
                           Visibility="{Binding IsValid, Converter={x:Static s:BoolToVisibilityConverter.InverseInstance}, Mode=OneWay}">
                    « Invalid »
                </TextBlock>
            </Grid>
        </Button>
    </StackPanel>

</UserControl>