<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:dd="https://gitee.com/akwkevin/aistudio.-wpf.-diagram"
                    xmlns:viewmodel="clr-namespace:AIStudio.Wpf.Mind.ViewModels"
                    xmlns:controls="clr-namespace:AIStudio.Wpf.Mind.Controls">

    <dd:ColorBrushConverter x:Key="ColorBrushConverter"/>
    <dd:DoubleToCornerRadius x:Key="DoubleToCornerRadius"/>
    <dd:IntToVisibilityConverter x:Key="IntToVisibilityConverter"/>
    <dd:DoubleToThickness x:Key="DoubleToThickness"/>
    <dd:NullableToVisibilityConverter x:Key="NullableToVisibilityConverter"/>
    <dd:NotNullOrEmptyToBoolConverter x:Key="NotNullOrEmptyToBoolConverter"/>
    <!-- Toggle Button -->
    <Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
        <Setter Property="Focusable" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToggleButton">
                    <Grid Width="17" Height="17" SnapsToDevicePixels="True">
                        <Rectangle Width="13" Height="13" Stroke="Gray" RadiusX="6" RadiusY="6" SnapsToDevicePixels="true">
                            <Rectangle.Fill>
                                <LinearGradientBrush EndPoint="0.5,2" StartPoint="0.5,0">
                                    <GradientStop Color="White" Offset="0"/>
                                    <GradientStop Color="Silver" Offset="0.5"/>
                                    <GradientStop Color="LightGray" Offset="1"/>
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>
                        <Rectangle x:Name="ExpandPath" Width="1" Height="9" Stroke="Gray" SnapsToDevicePixels="true" />
                        <Rectangle Width="9" Height="1" Stroke="Gray" SnapsToDevicePixels="true"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="Visibility"  TargetName="ExpandPath" Value="Collapsed"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <ControlTemplate x:Key="MindNodeLevelStyle" TargetType="{x:Type ContentControl}">
        <Grid>
            <Grid.ContextMenu>
                <ContextMenu ItemsSource="{Binding MenuOptions}">
                    <ContextMenu.ItemContainerStyle>
                        <Style TargetType="MenuItem">
                            <Setter Property="MenuItem.Header" Value="{Binding Text}" />
                            <Setter Property="MenuItem.ItemsSource" Value="{Binding Children}" />
                            <Setter Property="MenuItem.Command" Value="{Binding Command}" />
                            <Setter Property="MenuItem.Icon" Value="{Binding Icon}" />
                            <Setter Property="CommandParameter" Value="{Binding CommandParameter}" />
                            <Setter Property="MenuItem.IsCheckable" Value="{Binding IsCheckable}" />
                            <Setter Property="MenuItem.IsChecked" Value="{Binding IsChecked}" />
                        </Style>
                    </ContextMenu.ItemContainerStyle>
                </ContextMenu>
            </Grid.ContextMenu>

            <Grid>
                <Border BorderThickness="{Binding BorderThickness}" 
                        BorderBrush="{Binding ColorViewModel.LineColor,Converter={StaticResource ColorBrushConverter}}" 
                        Background="{Binding ColorViewModel.FillColor,Converter={StaticResource ColorBrushConverter}}"
                        CornerRadius="{Binding CornerRadius}"
                        IsHitTestVisible="False">
                </Border>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Image x:Name="image" Margin="5,10,5,5" Stretch="Fill" Width="128" Height="128" 
                           Visibility="{Binding ImageInfo,Converter={StaticResource NullableToVisibilityConverter}}"  
                           ToolTip="{Binding ImageInfo.Text}"
                           IsHitTestVisible="False"/>
                    <Grid Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <controls:RateControl Grid.Column="0" Rate="{Binding Rate}" 
                                                  Visibility="{Binding Rate,Converter={StaticResource NullableToVisibilityConverter}}"
                                                  IsHitTestVisible="False"/>
                        <controls:PriorityControl Grid.Column="1" Priority="{Binding Priority}" 
                                                  Visibility="{Binding Priority,Converter={StaticResource NullableToVisibilityConverter}}"
                                                  IsHitTestVisible="False"/>
                        <dd:TextControl Grid.Column="2" IsHitTestVisible="{Binding IsEditing}" AcceptsReturn="False">
                            <dd:TextControl.InputBindings>
                                <KeyBinding Key="Enter" Command="{Binding ExitEditCommand}" />                          
                            </dd:TextControl.InputBindings>
                        </dd:TextControl>
                        <controls:LinkControl Grid.Column="3" 
                                                  Visibility="{Binding LinkInfo,Converter={StaticResource NullableToVisibilityConverter}}" 
                                                  Url="{Binding LinkInfo.Link}"
                                                  ToolTip="{Binding LinkInfo.Text}" />
                        <controls:RemarkControl Grid.Column="4"
                                                  Visibility="{Binding Remark,Converter={StaticResource NullableToVisibilityConverter}}"                                                 
                                                  ToolTip="{Binding Remark}"/>
                        <controls:TagControl Grid.Column="5" IsHitTestVisible="False" ItemsSource="{Binding Tags}"/>
                    </Grid>
                </Grid>
            </Grid>
            <ToggleButton x:Name="toggle" IsChecked="{Binding IsExpanded}" Style="{StaticResource ExpandCollapseToggleStyle}" HorizontalAlignment="Left" Margin="-15,0,0,0" Visibility="{Binding Children.Count,Converter={StaticResource IntToVisibilityConverter}}"/>
        </Grid>
        <ControlTemplate.Triggers>
            <DataTrigger Binding="{Binding ImageInfo.Url,Converter={StaticResource NotNullOrEmptyToBoolConverter}}" Value="true">
                <Setter TargetName="image" Property="Source" Value="{Binding ImageInfo.Url}"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding ConnectorOrientation}" Value="None">
                <Setter TargetName="toggle" Property="Visibility" Value="Collapsed"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding ConnectorOrientation}" Value="Right">
                <Setter TargetName="toggle" Property="HorizontalAlignment" Value="Right"/>
                <Setter TargetName="toggle" Property="Margin" Value="0,0,-15,0"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding ConnectorOrientation}" Value="Top">
                <Setter TargetName="toggle" Property="HorizontalAlignment" Value="Center"/>
                <Setter TargetName="toggle" Property="VerticalAlignment" Value="Top"/>
                <Setter TargetName="toggle" Property="Margin" Value="0,-15,0,0"/>
            </DataTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style x:Key="MindNodeStyle" TargetType="{x:Type ContentControl}">
        <Setter Property="Template" Value="{StaticResource MindNodeLevelStyle}" />
    </Style>

    <DataTemplate DataType="{x:Type viewmodel:MindNode}">
        <Grid>
            <ContentControl Style="{StaticResource MindNodeStyle}"/>
        </Grid>
    </DataTemplate>

</ResourceDictionary>