<UserControl
    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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:s="https://github.com/canton7/Stylet"
    xmlns:Converters="clr-namespace:Artemis.UI.Converters"
    xmlns:utilities="clr-namespace:Artemis.UI.Utilities"
    xmlns:local="clr-namespace:Artemis.UI.Screens.ProfileEditor.Conditions"
    x:Class="Artemis.UI.Screens.ProfileEditor.Conditions.DataModelConditionGroupView"
    mc:Ignorable="d"
    d:DesignHeight="450" d:DesignWidth="800"
    d:DataContext="{d:DesignInstance IsDesignTimeCreatable=False, Type={x:Type local:DataModelConditionGroupViewModel}}">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Artemis.UI;component/ResourceDictionaries/DataModelConditions.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <Converters:InverseBooleanConverter x:Key="InverseBooleanConverter" />
            <utilities:BindingProxy x:Key="DataContextProxy" Data="{Binding}" />
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Button Grid.Row="0"
                Grid.Column="0"
                ToolTip="Delete the group and all its children"
                Style="{StaticResource MaterialDesignIconForegroundButton}"
                HorizontalAlignment="Left"
                Visibility="{Binding IsRootGroup, Converter={x:Static s:BoolToVisibilityConverter.InverseInstance}, Mode=OneWay}"
                Width="25"
                Height="25"
                Foreground="#E74C4C"
                Command="{s:Action Delete}">
            <materialDesign:PackIcon Kind="Close" Width="18" Height="18" />
        </Button>
        <Button Grid.Row="0"
                Grid.Column="1"
                Style="{StaticResource DataModelConditionButtonLeftClickMenu}"
                Background="#E74C4C"
                BorderBrush="#E74C4C"
                Margin="3 1"
                Content="{Binding SelectedBooleanOperator}"
                Visibility="{Binding DisplayBooleanOperator, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}"
                IsEnabled="{Binding IsEventGroup, Converter={StaticResource InverseBooleanConverter}}"
                ToolTipService.ShowOnDisabled="True">
            <Button.ToolTip>
                <StackPanel>
                    <TextBlock Visibility="{Binding IsEventGroup, Converter={x:Static s:BoolToVisibilityConverter.InverseInstance}, Mode=OneWay}">
                        Change the operator of the group, determining which conditions should match
                    </TextBlock>
                    <TextBlock Visibility="{Binding IsEventGroup, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}">
                        The operator of the root group cannot be changed for event-based conditions
                    </TextBlock>
                </StackPanel>
            </Button.ToolTip>
            <Button.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="And"
                              Command="{s:Action SelectBooleanOperator}"
                              CommandParameter="And"
                              ToolTip="All the conditions in the group should evaluate to true" />
                    <MenuItem Header="Or"
                              Command="{s:Action SelectBooleanOperator}"
                              CommandParameter="Or"
                              ToolTip="Any of the conditions in the group should evaluate to true" />
                    <MenuItem Header="And not"
                              Command="{s:Action SelectBooleanOperator}"
                              CommandParameter="AndNot"
                              ToolTip="All the conditions in the group should evaluate to false" />
                    <MenuItem Header="Or not"
                              Command="{s:Action SelectBooleanOperator}"
                              CommandParameter="OrNot"
                              ToolTip="Any of the conditions in the group should evaluate to false" />
                </ContextMenu>
            </Button.ContextMenu>
        </Button>
        <Button x:Name="AddChildButton"
                Grid.Row="0"
                Grid.Column="2"
                ToolTip="Add another condition to the current group"
                HorizontalAlignment="Left"
                Foreground="#4CE758"
                Width="25"
                Height="25">
            <Button.Style>
                <Style TargetType="{x:Type Button}" BasedOn="{StaticResource MaterialDesignIconForegroundButton}">
                    <Style.Triggers>
                        <EventTrigger RoutedEvent="ButtonBase.Click">
                            <BeginStoryboard>
                                <Storyboard>
                                    <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
                                        <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True" />
                                    </BooleanAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
            <Button.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Add condition" ToolTip="A condition that evaluates the state of a property in the data model" Command="{s:Action AddCondition}">
                        <MenuItem.Icon>
                            <materialDesign:PackIcon Kind="Equal" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="Add event condition"
                              ToolTip="An event condition that responds to data model events"
                              Command="{s:Action AddEventCondition}"
                              Visibility="{Binding Data.CanAddEventCondition, Source={StaticResource DataContextProxy}, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}">
                        <MenuItem.Icon>
                            <materialDesign:PackIcon Kind="LightningBolt" />
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="Add group" ToolTip="A group for conditions and other groups" Command="{s:Action AddGroup}">
                        <MenuItem.Icon>
                            <materialDesign:PackIcon Kind="CodeParentheses" />
                        </MenuItem.Icon>
                    </MenuItem>
                </ContextMenu>
            </Button.ContextMenu>
            <materialDesign:PackIcon Kind="Add" Width="18" Height="18" />
        </Button>

        <ToggleButton Grid.Row="0"
                      Grid.Column="3"
                      Style="{StaticResource MaterialDesignActionToggleButton}"
                      Focusable="False"
                      IsHitTestVisible="False"
                      IsChecked="{Binding IsConditionMet}"
                      Visibility="{Binding DisplayEvaluationResult, Converter={x:Static s:BoolToVisibilityConverter.Instance}, Mode=OneWay}"
                      Width="22"
                      Height="22"
                      HorizontalAlignment="Left">
            <ToggleButton.Content>
                <Border Background="#E74C4C" Width="22" Height="22">
                    <materialDesign:PackIcon Kind="Close" VerticalAlignment="Center" HorizontalAlignment="Center" Width="16" Height="16"/>
                </Border>
            </ToggleButton.Content>
            <materialDesign:ToggleButtonAssist.OnContent>
                <materialDesign:PackIcon Kind="Check" Width="16" Height="16"/>
            </materialDesign:ToggleButtonAssist.OnContent>
        </ToggleButton>

        <ItemsControl Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3" ItemsSource="{Binding Items}" Margin="0 3 0 0">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ContentControl s:View.Model="{Binding}" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" IsTabStop="False" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</UserControl>