<UserControl x:Class="VIZ.Package.Module.PageGroupView"
             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: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: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:PageGroupViewModel}" 
             x:Name="uc"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/VIZ.Package.Module.Resource;component/Style/IconButton/IconButton_Default.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>

            <resource:RowHandleConverter x:Key="RowHandleConverter"></resource:RowHandleConverter>
            <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>

            <!-- 列模板选择器 -->
            <common:ColumnDefintionGeneratorTemplateSelector x:Key="ColumnDefintionGeneratorTemplateSelector">
                <common:ColumnDefintionGeneratorTemplateSelector.Templates>
                    <common:ColumnDefintionGeneratorTemplateCollection>
                        <!-- 序号 -->
                        <common:ColumnDefintionGeneratorTemplate ColumnKey="Index">
                            <common:ColumnDefintionGeneratorTemplate.DataTemplate>
                                <DataTemplate>
                                    <ContentControl>
                                        <dxg:GridColumn Header="序号" ReadOnly="True" AllowSorting="False" AllowColumnFiltering="False" Width="40" AllowResizing="False">
                                            <dxg:GridColumn.CellTemplate>
                                                <DataTemplate>
                                                    <Grid Background="{Binding Path=Row.IsOpen,Converter={StaticResource RowHandleBgConverter}}">
                                                        <TextBlock Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding RowData.RowHandle,Converter={StaticResource RowHandleConverter}}"></TextBlock>
                                                    </Grid>
                                                </DataTemplate>
                                            </dxg:GridColumn.CellTemplate>
                                        </dxg:GridColumn>
                                    </ContentControl>
                                </DataTemplate>
                            </common:ColumnDefintionGeneratorTemplate.DataTemplate>
                        </common:ColumnDefintionGeneratorTemplate>
                        <!-- 编号 -->
                        <common:ColumnDefintionGeneratorTemplate ColumnKey="PageNum">
                            <common:ColumnDefintionGeneratorTemplate.DataTemplate>
                                <DataTemplate>
                                    <ContentControl>
                                        <dxg:GridColumn Header="编号" AllowSorting="False" AllowColumnFiltering="False" AllowResizing="True"
                                                        FieldName="PageNum"
                                                        Visible="{Binding Path=Visible,Mode=TwoWay}"
                                                        VisibleIndex="{Binding Path=VisibleIndex,Mode=TwoWay}">
                                            <dxg:GridColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4,0,4,0"
                                                               Text="{Binding Row.PageNum,StringFormat={}{0:D4}}"></TextBlock>
                                                </DataTemplate>
                                            </dxg:GridColumn.CellTemplate>
                                            <dxg:GridColumn.CellEditTemplate>
                                                <DataTemplate>
                                                    <local:PageNumberEdit></local:PageNumberEdit>
                                                </DataTemplate>
                                            </dxg:GridColumn.CellEditTemplate>
                                        </dxg:GridColumn>
                                    </ContentControl>
                                </DataTemplate>
                            </common:ColumnDefintionGeneratorTemplate.DataTemplate>
                        </common:ColumnDefintionGeneratorTemplate>
                        <!-- 缩略图 -->
                        <common:ColumnDefintionGeneratorTemplate ColumnKey="ThumbnailBitmap">
                            <common:ColumnDefintionGeneratorTemplate.DataTemplate>
                                <DataTemplate>
                                    <ContentControl>
                                        <common:GridColumnResizeMinHeight Header="缩略图" ReadOnly="True" AllowSorting="False" AllowColumnFiltering="False"
                                                                          AllowEditing="False" Width="120" MinWidth="40" MaxWidth="300" AllowResizing="True"
                                                                          Visible="{Binding Path=Visible,Mode=TwoWay}"
                                                                          VisibleIndex="{Binding Path=VisibleIndex,Mode=TwoWay}">
                                            <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>
                                    </ContentControl>
                                </DataTemplate>
                            </common:ColumnDefintionGeneratorTemplate.DataTemplate>
                        </common:ColumnDefintionGeneratorTemplate>
                        <!-- 场景名 -->
                        <common:ColumnDefintionGeneratorTemplate ColumnKey="Scene">
                            <common:ColumnDefintionGeneratorTemplate.DataTemplate>
                                <DataTemplate>
                                    <ContentControl>
                                        <dxg:GridColumn Header="场景名" FieldName="Scene" ReadOnly="True" AllowSorting="False" AllowColumnFiltering="False"
                                                        Visible="{Binding Path=Visible,Mode=TwoWay}"
                                                        VisibleIndex="{Binding Path=VisibleIndex,Mode=TwoWay}"></dxg:GridColumn>
                                    </ContentControl>
                                </DataTemplate>
                            </common:ColumnDefintionGeneratorTemplate.DataTemplate>
                        </common:ColumnDefintionGeneratorTemplate>
                        <!-- 备注 -->
                        <common:ColumnDefintionGeneratorTemplate ColumnKey="Remark">
                            <common:ColumnDefintionGeneratorTemplate.DataTemplate>
                                <DataTemplate>
                                    <ContentControl>
                                        <dxg:GridColumn Header="备注"  FieldName="Remark" AllowSorting="False" AllowColumnFiltering="False"
                                                        Visible="{Binding Path=Visible,Mode=TwoWay}"
                                                        VisibleIndex="{Binding Path=VisibleIndex,Mode=TwoWay}">
                                            <dxg:GridColumn.EditSettings>
                                                <dxe:TextEditSettings TextWrapping="Wrap" TextTrimming="CharacterEllipsis" MaxLength="50"></dxe:TextEditSettings>
                                            </dxg:GridColumn.EditSettings>
                                        </dxg:GridColumn>
                                    </ContentControl>
                                </DataTemplate>
                            </common:ColumnDefintionGeneratorTemplate.DataTemplate>
                        </common:ColumnDefintionGeneratorTemplate>
                        <!-- 层 -->
                        <common:ColumnDefintionGeneratorTemplate ColumnKey="Layer">
                            <common:ColumnDefintionGeneratorTemplate.DataTemplate>
                                <DataTemplate>
                                    <ContentControl>
                                        <dxg:GridColumn Header="层" FieldName="Layer" AllowSorting="False" AllowColumnFiltering="False"
                                                        Visible="{Binding Path=Visible,Mode=TwoWay}"
                                                        VisibleIndex="{Binding Path=VisibleIndex,Mode=TwoWay}">
                                            <dxg:GridColumn.EditSettings>
                                                <dxe:ComboBoxEditSettings ItemsSource="{Binding Source={x:Static domain:ApplicationConstants.VIZ_LAYERS}}"
                                                                          IsTextEditable="False"></dxe:ComboBoxEditSettings>
                                            </dxg:GridColumn.EditSettings>
                                        </dxg:GridColumn>
                                    </ContentControl>
                                </DataTemplate>
                            </common:ColumnDefintionGeneratorTemplate.DataTemplate>
                        </common:ColumnDefintionGeneratorTemplate>
                        <!-- 连接 -->
                        <common:ColumnDefintionGeneratorTemplate ColumnKey="ConnGroupID">
                            <common:ColumnDefintionGeneratorTemplate.DataTemplate>
                                <DataTemplate>
                                    <ContentControl>
                                        <dxg:GridColumn Header="连接" FieldName="ConnGroupID" AllowSorting="False" AllowColumnFiltering="False"
                                                        Visible="{Binding Path=Visible,Mode=TwoWay}"
                                                        VisibleIndex="{Binding Path=VisibleIndex,Mode=TwoWay}">
                                            <dxg:GridColumn.EditSettings>
                                                <dxe:ComboBoxEditSettings ItemsSource="{Binding ElementName=uc, Path=DataContext.ConnGroups,Mode=TwoWay}"
                                                                          DisplayMember="Name" ValueMember="GroupID" IsTextEditable="False"></dxe:ComboBoxEditSettings>
                                            </dxg:GridColumn.EditSettings>
                                        </dxg:GridColumn>
                                    </ContentControl>
                                </DataTemplate>
                            </common:ColumnDefintionGeneratorTemplate.DataTemplate>
                        </common:ColumnDefintionGeneratorTemplate>
                        <!-- 初始化加载 -->
                        <common:ColumnDefintionGeneratorTemplate ColumnKey="TakeInitedProgress">
                            <common:ColumnDefintionGeneratorTemplate.DataTemplate>
                                <DataTemplate>
                                    <ContentControl>
                                        <dxg:GridColumn Header="初始化加载" ReadOnly="True" AllowSorting="False" AllowColumnFiltering="False" AllowEditing="False"
                                                        Visible="{Binding Path=Visible,Mode=TwoWay}"
                                                        VisibleIndex="{Binding Path=VisibleIndex,Mode=TwoWay}">
                                            <dxg:GridColumn.CellTemplate>
                                                <DataTemplate>
                                                    <Grid Background="Transparent" ToolTip="{Binding Path=Row.SceneInfo.TakeInitedMessage}">
                                                        <dxe:ProgressBarEdit Minimum="0" Maximum="1" ContentDisplayMode="Value" 
                                                                             DisplayFormatString="{}{0:P}" Height="20" ShowBorder="False" Background="Transparent"
                                                                             EditValue="{Binding Path=Row.SceneInfo.TakeInitedProgress}"></dxe:ProgressBarEdit>
                                                    </Grid>
                                                </DataTemplate>
                                            </dxg:GridColumn.CellTemplate>
                                        </dxg:GridColumn>
                                    </ContentControl>
                                </DataTemplate>
                            </common:ColumnDefintionGeneratorTemplate.DataTemplate>
                        </common:ColumnDefintionGeneratorTemplate>
                        <!-- 引擎类型 -->
                        <common:ColumnDefintionGeneratorTemplate ColumnKey="EngineType">
                            <common:ColumnDefintionGeneratorTemplate.DataTemplate>
                                <DataTemplate>
                                    <ContentControl>
                                        <dxg:GridColumn Header="引擎类型" FieldName="EngineType" ReadOnly="True" AllowSorting="False" AllowColumnFiltering="False"
                                                        Visible="{Binding Path=Visible,Mode=TwoWay}"
                                                        VisibleIndex="{Binding Path=VisibleIndex,Mode=TwoWay}"></dxg:GridColumn>
                                    </ContentControl>
                                </DataTemplate>
                            </common:ColumnDefintionGeneratorTemplate.DataTemplate>
                        </common:ColumnDefintionGeneratorTemplate>
                        <!-- 模板类型 -->
                        <common:ColumnDefintionGeneratorTemplate ColumnKey="PageType">
                            <common:ColumnDefintionGeneratorTemplate.DataTemplate>
                                <DataTemplate>
                                    <ContentControl>
                                        <dxg:GridColumn Header="模板类型" ReadOnly="True" AllowSorting="False" AllowColumnFiltering="False"
                                                        Binding="{Binding Path=PageType,Converter={StaticResource Enum2EnumDescriptionConverter}}"
                                                        Visible="{Binding Path=Visible,Mode=TwoWay}"
                                                        VisibleIndex="{Binding Path=VisibleIndex,Mode=TwoWay}">
                                        </dxg:GridColumn>
                                    </ContentControl>
                                </DataTemplate>
                            </common:ColumnDefintionGeneratorTemplate.DataTemplate>
                        </common:ColumnDefintionGeneratorTemplate>
                        <!-- 插件 -->
                        <common:ColumnDefintionGeneratorTemplate ColumnKey="PluginName">
                            <common:ColumnDefintionGeneratorTemplate.DataTemplate>
                                <DataTemplate>
                                    <ContentControl>
                                        <dxg:GridColumn Header="插件" FieldName="PluginName" ReadOnly="True" AllowSorting="False" AllowColumnFiltering="False"
                                                        Visible="{Binding Path=Visible,Mode=TwoWay}"
                                                        VisibleIndex="{Binding Path=VisibleIndex,Mode=TwoWay}"></dxg:GridColumn>
                                    </ContentControl>
                                </DataTemplate>
                            </common:ColumnDefintionGeneratorTemplate.DataTemplate>
                        </common:ColumnDefintionGeneratorTemplate>
                    </common:ColumnDefintionGeneratorTemplateCollection>
                </common:ColumnDefintionGeneratorTemplateSelector.Templates>
            </common:ColumnDefintionGeneratorTemplateSelector>

        </ResourceDictionary>
    </UserControl.Resources>

    <Grid IsEnabled="{Binding IsEnabled}">
        <Grid.RowDefinitions>
            <RowDefinition Height="35"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <!-- 按钮组 -->
        <Border Background="#11FFFFFF">
            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                <!-- 清理 -->
                <fcommon:IconButton Icon="/VIZ.Package.Module.Resource;component/Icons/icon_clear_32x32.png"
                                    Style="{StaticResource IconButton_Menu_Mask}"
                                    ToolTip="清理内存" Margin="10,0,0,0" 
                                    IconWidth="24" IconHeight="24" Width="24" Height="24"
                                    Command="{Binding TakeClearCommand}"></fcommon:IconButton>
                <!-- 初始化 -->
                <fcommon:IconButton Icon="/VIZ.Package.Module.Resource;component/Icons/icon_init_32x32.png"
                                    Style="{StaticResource IconButton_Menu_Mask}"
                                    ToolTip="初始化" Margin="10,0,0,0" 
                                    IconWidth="24" IconHeight="24" Width="24" Height="24"
                                    Command="{Binding TakeInitAllCommand}"></fcommon:IconButton>
            </StackPanel>
        </Border>

        <dx:DXTabControl Grid.Row="1" Margin="0,10,0,0" Padding="0,5,0,0" 
                         ItemsSource="{Binding Path=PageGroupModels}" Background="Transparent"
                         SelectedItem="{Binding Path=SelectedPageGroupModel,Mode=TwoWay}">
            <dx:DXTabControl.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="新建分组" Command="{Binding Path=PlacementTarget.DataContext.AddGroupCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
                    <MenuItem Header="重命名分组" Command="{Binding Path=PlacementTarget.DataContext.RenameGroupCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
                    <Separator></Separator>
                    <MenuItem Header="复制分组" Command="{Binding Path=PlacementTarget.DataContext.CopyPageGroupCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
                    <Separator></Separator>
                    <MenuItem Header="删除分组" Command="{Binding Path=PlacementTarget.DataContext.DeleteGroupCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
                </ContextMenu>
            </dx:DXTabControl.ContextMenu>
            <dx:DXTabControl.View>
                <dx:TabControlStretchView Tag="Stretch"
                                          HeaderLocation="Top" ShowHeaderMenu="False"
                                          NewButtonShowMode="InTabPanel"
                                          HideButtonShowMode="InAllTabs"
                                          PinnedTabSize="100">
                </dx:TabControlStretchView>
            </dx:DXTabControl.View>

            <dxmvvm:Interaction.Behaviors>
                <dxmvvm:EventToCommand EventName="TabAdding" Command="{Binding TabAddGroupCommand}" PassEventArgsToCommand="True" />
            </dxmvvm:Interaction.Behaviors>
            <dx:DXTabControl.ItemContainerStyle>
                <Style TargetType="dx:DXTabItem">
                    <Setter Property="Content" Value="{Binding .}" />
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <dxg:GridControl ItemsSource="{Binding Path=Pages}" ShowBorder="False" IsFilterEnabled="False" SelectionMode="Row" Margin="0,5,0,0"
                                                 SelectedItem="{Binding Path=SelectedPage,Mode=TwoWay}"
                                                 SelectedItems="{Binding Path=SelectedPages}"
                                                 ColumnsSource="{Binding ElementName=uc, Path=DataContext.Columns}"
                                                 ColumnGeneratorTemplateSelector="{StaticResource ResourceKey=ColumnDefintionGeneratorTemplateSelector}">
                                    <dxg:GridControl.ContextMenu>
                                        <common:ContextMenuEx BindingTargetType="{x:Type dx:DXTabControl}">
                                            <MenuItem Header="粘贴页" Command="{Binding Path=DataContext.PastePageCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type common:ContextMenuEx}}}"/>
                                        </common:ContextMenuEx>
                                    </dxg:GridControl.ContextMenu>

                                    <dxg:GridControl.View>
                                        <dxg:TableView IsColumnMenuEnabled="True" ScrollAnimationMode="EaseInOut" AllowScrollAnimation="True"
                                                       IsColumnChooserVisible="{Binding ElementName=uc, Path=DataContext.IsColumnChooserVisible,Mode=TwoWay}"
                                                       AllowEditing="True" ShowIndicator="False" RowMinHeight="69.5"
                                                       NavigationStyle="Cell" ShowVerticalLines="False"
                                                       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 ElementName=uc, Path=DataContext.OpenPageCommand}"></dxmvvm:EventToCommand>
                                                <dxmvvm:EventToCommand EventName="ShowGridMenu" Command="{Binding ElementName=uc, Path=DataContext.ShowGridMenuCommand}" PassEventArgsToCommand="True"></dxmvvm:EventToCommand>
                                            </dxmvvm:Interaction.Behaviors>
                                            <dxg:TableView.ColumnMenuCustomizations>
                                                <dxb:BarButtonItem Name="ShowColumnChooser" Content="显示列选择" 
                                                                   Command="{Binding ElementName=uc,Path=DataContext.ColumnChoiceCommand}"
                                                                   Tag="{x:Static Member=common:GridControlHelper.KEEP_MENU_TAG}"/>
                                            </dxg:TableView.ColumnMenuCustomizations>
                                            <dxg:TableView.RowCellMenuCustomizations>
                                                <dxb:BarButtonItem Name="OpenPage" Content="打开" 
                                                                   Command="{Binding ElementName=uc,Path=DataContext.OpenPageCommand}"
                                                                   Tag="{x:Static Member=common:GridControlHelper.KEEP_MENU_TAG}"/>
                                                <dxb:BarItemLinkSeparator />
                                                <dxb:BarButtonItem Name="Update" Content="更新选中页" 
                                                                   Command="{Binding ElementName=uc,Path=DataContext.UpdateCommand}"
                                                                   Tag="{x:Static Member=common:GridControlHelper.KEEP_MENU_TAG}"/>
                                                <dxb:BarButtonItem Name="TakeInit" Content="初始化选中页" 
                                                                   Command="{Binding ElementName=uc,Path=DataContext.TakeInitCommand}"
                                                                   Tag="{x:Static Member=common:GridControlHelper.KEEP_MENU_TAG}"/>
                                                <dxb:BarItemLinkSeparator />
                                                <dxb:BarButtonItem Name="CopyPage" Content="复制选中页" 
                                                                   Command="{Binding ElementName=uc,Path=DataContext.BeginCopyPageCommand}"
                                                                   Tag="{x:Static Member=common:GridControlHelper.KEEP_MENU_TAG}"/>
                                                <dxb:BarButtonItem Name="PastePage" Content="粘贴页" 
                                                                   Command="{Binding ElementName=uc,Path=DataContext.PastePageCommand}"
                                                                   Tag="{x:Static Member=common:GridControlHelper.KEEP_MENU_TAG}"/>
                                                <dxb:BarItemLinkSeparator />
                                                <dxb:BarButtonItem Name="DeleteItem" Content="删除选中页" 
                                                                   Command="{Binding ElementName=uc,Path=DataContext.DeleteItemCommand}"
                                                                   Tag="{x:Static Member=common:GridControlHelper.KEEP_MENU_TAG}"/>

                                            </dxg:TableView.RowCellMenuCustomizations>
                                        </dxg:TableView>
                                    </dxg:GridControl.View>
                                </dxg:GridControl>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Header" Value="{Binding GroupName}" />
                    <Setter Property="dx:TabControlStretchView.PinMode" Value="Left" />
                </Style>
            </dx:DXTabControl.ItemContainerStyle>
        </dx:DXTabControl>

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