<UserControl x:Class="VIZ.Package.Module.PageTemplateView"
             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:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
             xmlns:dxcn="http://schemas.devexpress.com/winfx/2008/xaml/core/internal"
             xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
             xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
             xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
             xmlns:dxdo="http://schemas.devexpress.com/winfx/2008/xaml/docking"
             xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
             xmlns:fcore="clr-namespace:VIZ.Framework.Core;assembly=VIZ.Framework.Core"
             xmlns:fcommon="clr-namespace:VIZ.Framework.Common;assembly=VIZ.Framework.Common"
             xmlns:common="clr-namespace:VIZ.Package.Common;assembly=VIZ.Package.Common"
             xmlns:storage="clr-namespace:VIZ.Package.Storage;assembly=VIZ.Package.Storage"
             xmlns:resource="clr-namespace:VIZ.Package.Module.Resource;assembly=VIZ.Package.Module.Resource"
             xmlns:local="clr-namespace:VIZ.Package.Module"
             xmlns:domain="clr-namespace:VIZ.Package.Domain;assembly=VIZ.Package.Domain"
             d:DataContext="{d:DesignInstance Type=local:PageTemplateViewModel}" 
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">

    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:EventToCommand EventName="PreviewKeyDown" Command="{Binding Path=PreviewKeyDownCommand}" PassEventArgsToCommand="True"></dxmvvm:EventToCommand>
    </dxmvvm:Interaction.Behaviors>

    <UserControl.Resources>
        <ResourceDictionary>
            <resource:RowHandleConverter x:Key="RowHandleConverter"></resource:RowHandleConverter>
            <fcore:Bool2BoolConverter x:Key="Bool2BoolConverter"></fcore:Bool2BoolConverter>
            <fcore:MultiBool2BoolConverter x:Key="MultiBool2BoolConverter" Mask="1 0" MatchResult="True" NotMatchResult="False"></fcore:MultiBool2BoolConverter>
            <fcore:Bitmap2ImageSourceConverter x:Key="Bitmap2ImageSourceConverter"></fcore:Bitmap2ImageSourceConverter>
            <fcore:Enum2EnumDescriptionConverter x:Key="Enum2EnumDescriptionConverter" EnumType="{x:Type storage:PageType}"></fcore:Enum2EnumDescriptionConverter>
            <fcore:Bool2SolidColorBrushConverter x:Key="RowHandleBgConverter"
                                                 TrueBrush="#FF0078D7"
                                                 FalseBrush="Transparent"></fcore:Bool2SolidColorBrushConverter>
            <fcore:Bool2SolidColorBrushConverter x:Key="RowHandleTakeConverter"
                                                 TrueBrush="#FF67ad5b"
                                                 FalseBrush="Transparent"></fcore:Bool2SolidColorBrushConverter>

            <!-- 层模板 -->
            <fcore:Bool2ControlTemplateConverter x:Key="Bool2ControlTemplateConverter_LayerColumnCellTemplate">
                <fcore:Bool2ControlTemplateConverter.TrueResult>
                    <ControlTemplate>
                        <Border Background="Transparent">
                            <TextBlock Text="{Binding Path=Row.LayerIdentifier}" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4,0,4,0"></TextBlock>
                            <Border.ToolTip>
                                <Grid Height="75" MinWidth="220">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*"></RowDefinition>
                                        <RowDefinition Height="*"></RowDefinition>
                                        <RowDefinition Height="*"></RowDefinition>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="110"></ColumnDefinition>
                                        <ColumnDefinition Width="*"></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Layer identifier:" Foreground="#88ffffff" HorizontalAlignment="Right"></TextBlock>
                                    <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Row.LayerIdentifier,Mode=OneWay}" Margin="10,0,10,0"></TextBlock>
                                    <TextBlock Grid.Row="1" Grid.Column="0" Text="State identifier:" Foreground="#88ffffff" HorizontalAlignment="Right"></TextBlock>
                                    <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Row.StateIdentifier,Mode=OneWay}" Margin="10,0,10,0"></TextBlock>
                                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Background scene:" Foreground="#88ffffff" HorizontalAlignment="Right"></TextBlock>
                                    <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=Row.BackgroundScene,Mode=OneWay}" Margin="10,0,10,0"></TextBlock>
                                </Grid>
                            </Border.ToolTip>
                        </Border>
                    </ControlTemplate>
                </fcore:Bool2ControlTemplateConverter.TrueResult>
                <fcore:Bool2ControlTemplateConverter.FalseResult>
                    <ControlTemplate>
                        <TextBlock Text="{Binding Path=Row.Layer}" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4,0,4,0"></TextBlock>
                    </ControlTemplate>
                </fcore:Bool2ControlTemplateConverter.FalseResult>
            </fcore:Bool2ControlTemplateConverter>

            <fcore:Bool2ControlTemplateConverter x:Key="Bool2ControlTemplateConverter_LayerColumnCellEditTemplate">
                <fcore:Bool2ControlTemplateConverter.TrueResult>
                    <ControlTemplate>
                        <Border Background="Transparent">
                            <TextBlock Text="{Binding Path=Row.LayerIdentifier}" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4,0,4,0"></TextBlock>
                            <Border.ToolTip>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*"></RowDefinition>
                                        <RowDefinition Height="*"></RowDefinition>
                                        <RowDefinition Height="*"></RowDefinition>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="80"></ColumnDefinition>
                                        <ColumnDefinition Width="*"></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Layer identifier:" Foreground="#aaffffff"></TextBlock>
                                    <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Row.LayerIdentifier,Mode=OneWay}"></TextBlock>
                                    <TextBlock Grid.Row="1" Grid.Column="0" Text="State identifier:" Foreground="#aaffffff"></TextBlock>
                                    <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Row.StateIdentifier,Mode=OneWay}"></TextBlock>
                                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Background scene:" Foreground="#aaffffff"></TextBlock>
                                    <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=Row.BackgroundScene,Mode=OneWay}"></TextBlock>
                                </Grid>
                            </Border.ToolTip>
                        </Border>
                    </ControlTemplate>
                </fcore:Bool2ControlTemplateConverter.TrueResult>
                <fcore:Bool2ControlTemplateConverter.FalseResult>
                    <ControlTemplate>
                        <dxe:ComboBoxEdit ItemsSource="{Binding Source={x:Static domain:ApplicationConstants.VIZ_LAYERS}}"
                                          EditValue="{Binding Path=Row.Layer,Mode=TwoWay}" ShowBorder="False"
                                          IsTextEditable="False"></dxe:ComboBoxEdit>
                    </ControlTemplate>
                </fcore:Bool2ControlTemplateConverter.FalseResult>
            </fcore:Bool2ControlTemplateConverter>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid Margin="0,10,0,10">
        <Grid.IsHitTestVisible>
            <!-- Mask: 1 0 -->
            <MultiBinding Converter="{StaticResource MultiBool2BoolConverter}">
                <Binding Path="IsEnabled"/>
                <Binding Path="IsWaitingPageOpen"/>
            </MultiBinding>
        </Grid.IsHitTestVisible>

        <dxg:GridControl x:Name="grid" ItemsSource="{Binding Path=SceneTemplateModels}" ShowBorder="False" SelectionMode="Row"
                         SelectedItem="{Binding Path=SelectedSceneTemplateModel}"
                         SelectedItems="{Binding Path=SelectedSceneTemplateModels}">
            <dxg:GridControl.Resources>
                <Style TargetType="{x:Type dxg:RowControl}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Row.IsOpen}" Value="True">
                            <Setter Property="Background" Value="#AA0078D7"></Setter>
                        </DataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding Row.IsOpen}" Value="True"></Condition>
                                <Condition Binding="{Binding IsActive,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type dxdo:LayoutPanel}}}" Value="True"></Condition>
                            </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.Setters>
                                <Setter Property="Background" Value="#FF0078D7"></Setter>
                            </MultiDataTrigger.Setters>
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </dxg:GridControl.Resources>

            <dxg:GridControl.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="打开" Command="{Binding Path=PlacementTarget.DataContext.OpenScenePageCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
                    <Separator></Separator>
                    <MenuItem Header="添加至播出单" Command="{Binding Path=PlacementTarget.DataContext.AddToPageGroupCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
                    <MenuItem Header="更新模板" Command="{Binding Path=PlacementTarget.DataContext.UpdateCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
                    <Separator></Separator>
                    <MenuItem Header="删除模板  ( Delete )" Command="{Binding Path=PlacementTarget.DataContext.DeleteCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
                </ContextMenu>
            </dxg:GridControl.ContextMenu>
            <dxg:GridControl.Columns>
                <dxg:GridColumn Header="序号" ReadOnly="True" AllowSorting="False" AllowColumnFiltering="False" Width="40" AllowResizing="False">
                    <dxg:GridColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <Rectangle Fill="{Binding Path=Row.IsTake,Converter={StaticResource RowHandleTakeConverter}}"></Rectangle>
                                <TextBlock Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding RowData.RowHandle,Converter={StaticResource RowHandleConverter}}"></TextBlock>
                            </Grid>
                        </DataTemplate>
                    </dxg:GridColumn.CellTemplate>
                </dxg:GridColumn>
                <common:GridColumnResizeMinHeight Header="缩略图" ReadOnly="True" AllowSorting="False" AllowColumnFiltering="False" 
                                                  AllowEditing="False" Width="120" MinWidth="40" MaxWidth="300" AllowResizing="True">
                    <common:GridColumnResizeMinHeight.CellTemplate>
                        <DataTemplate>
                            <fcommon:ResizeImageControl ImageSource="{Binding Path=Row.ThumbnailBitmap,Converter={StaticResource Bitmap2ImageSourceConverter}}"
                                                                IconSource="/VIZ.Package.Module.Resource;component/Icons/image_20x20.png"
                                                                ShowImageMinWidth="50" ShowImageMinHeight="50"></fcommon:ResizeImageControl>
                        </DataTemplate>
                    </common:GridColumnResizeMinHeight.CellTemplate>
                </common:GridColumnResizeMinHeight>
                <dxg:GridColumn Header="场景名" FieldName="Scene" ReadOnly="True" AllowSorting="False" AllowColumnFiltering="False"></dxg:GridColumn>
                <dxg:GridColumn Header="备注" FieldName="Remark" AllowSorting="False" AllowColumnFiltering="False">
                    <dxg:GridColumn.EditSettings>
                        <dxe:TextEditSettings TextWrapping="Wrap" TextTrimming="CharacterEllipsis" MaxLength="50"></dxe:TextEditSettings>
                    </dxg:GridColumn.EditSettings>
                </dxg:GridColumn>
                <dxg:GridColumn Header="层" FieldName="Layer" AllowSorting="False" AllowColumnFiltering="False" AllowEditing="True">
                    <dxg:GridColumn.CellTemplate>
                        <DataTemplate>
                            <ContentControl Template="{Binding Path=Row.TransitionLogic,Converter={StaticResource Bool2ControlTemplateConverter_LayerColumnCellTemplate}}"></ContentControl>
                        </DataTemplate>
                    </dxg:GridColumn.CellTemplate>
                    <dxg:GridColumn.CellEditTemplate>
                        <DataTemplate>
                            <ContentControl Template="{Binding Path=Row.TransitionLogic,Converter={StaticResource Bool2ControlTemplateConverter_LayerColumnCellEditTemplate}}"></ContentControl>
                        </DataTemplate>
                    </dxg:GridColumn.CellEditTemplate>
                </dxg:GridColumn>
                <dxg:GridColumn Header="引擎类型" FieldName="EngineType" ReadOnly="True" AllowSorting="False" AllowColumnFiltering="False"></dxg:GridColumn>
            </dxg:GridControl.Columns>
            <dxg:GridControl.View>
                <dxg:TableView IsColumnMenuEnabled="False" RowMinHeight="69.5"
                               AllowEditing="True" ShowIndicator="False"
                               NavigationStyle="Cell" ShowVerticalLines="False" ShowHorizontalLines="False"
                               AlternateRowBackground="#05ffffff"
                               ShowGroupPanel="False" EditorShowMode="MouseDown"
                               AllowDragDrop="True"
                               ShowBandsPanel="False"
                               ShowTotalSummary="False"
                               ShowFixedTotalSummary="False"
                               ShowDragDropHint="False"
                               ShowTargetInfoInDragDropHint="false">
                    <dxmvvm:Interaction.Behaviors>
                        <dxmvvm:EventToCommand EventName="RowDoubleClick" Command="{Binding Path=OpenScenePageCommand}"></dxmvvm:EventToCommand>
                    </dxmvvm:Interaction.Behaviors>
                </dxg:TableView>
            </dxg:GridControl.View>
        </dxg:GridControl>

        <dx:WaitIndicator DeferedVisibility="{Binding IsLoading}" Content="Loading..." />
    </Grid>
</UserControl>
