<UserControl x:Class="NavisworksTransport.UI.WPF.Views.AliasTreeControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:NavisworksTransport.UI.WPF.ViewModels"
             xmlns:converters="clr-namespace:NavisworksTransport.UI.WPF.Converters"
             Background="White"
             PreviewKeyDown="UserControl_PreviewKeyDown">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../Resources/NavisworksStyles.xaml"/>
                <ResourceDictionary Source="../Resources/AliasTreeIcons.xaml"/>
            </ResourceDictionary.MergedDictionaries>

            <!-- Converters -->
            <converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>

            <!-- 别名树 TreeView 的模板 -->
            <HierarchicalDataTemplate DataType="{x:Type vm:AliasNodeViewModel}" 
                                       ItemsSource="{Binding Children}">
                <Grid VerticalAlignment="Center">
                    <!-- 显示模式：StackPanel 包含别名（红色）+ [原文]（黑色） -->
                    <StackPanel Orientation="Horizontal">
                        <StackPanel.Style>
                            <Style TargetType="StackPanel">
                                <Setter Property="Visibility" Value="Visible"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsEditing}" Value="True">
                                        <Setter Property="Visibility" Value="Collapsed"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </StackPanel.Style>
                        <!-- 别名/原名 -->
                        <TextBlock Text="{Binding DisplayName}"
                                   FontSize="12" FontWeight="Bold"
                                   TextTrimming="CharacterEllipsis" MaxWidth="140">
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Setter Property="Foreground" Value="#FF1A1A1A"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding HasAlias}" Value="True">
                                            <Setter Property="Foreground" Value="#FFC62828"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=TreeViewItem}}" Value="True">
                                            <Setter Property="Foreground" Value="White"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding IsOrphan}" Value="True">
                                            <Setter Property="TextDecorations" Value="Strikethrough"/>
                                            <Setter Property="Foreground" Value="#FFAAAAAA"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                        <!-- [原文] 后缀，黑色加粗 -->
                        <TextBlock FontSize="12" FontWeight="Bold"
                                   VerticalAlignment="Center">
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Setter Property="Foreground" Value="#FF1A1A1A"/>
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding HasAlias}" Value="True">
                                            <Setter Property="Visibility" Value="Visible"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=TreeViewItem}}" Value="True">
                                            <Setter Property="Foreground" Value="White"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                            <Run Text=" ["/>
                            <Run Text="{Binding OriginalName}"/>
                            <Run Text="]"/>
                        </TextBlock>
                    </StackPanel>

                    <!-- 编辑模式 -->
                    <TextBox Text="{Binding EditValue, UpdateSourceTrigger=PropertyChanged}"
                             FontSize="12"
                             KeyDown="OnAliasTextBoxKeyDown"
                             LostFocus="OnAliasTextBoxLostFocus"
                             IsVisibleChanged="OnEditTextBoxVisibleChanged"
                             Background="#FFFFFDE6"
                             BorderBrush="#FF1565C0" BorderThickness="1">
                        <TextBox.Style>
                            <Style TargetType="TextBox">
                                <Setter Property="Visibility" Value="Collapsed"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsEditing}" Value="True">
                                        <Setter Property="Visibility" Value="Visible"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBox.Style>
                    </TextBox>
                </Grid>
            </HierarchicalDataTemplate>
        </ResourceDictionary>
    </UserControl.Resources>

    <DockPanel>
        <!-- ── 顶部工具栏 ── -->
        <Border DockPanel.Dock="Top" 
                Background="#FFF5F5F5" 
                BorderBrush="#FFDDDDDD" 
                BorderThickness="0,0,0,1"
                Padding="4,2">
            <DockPanel>
                <!-- 当前选中节点名 -->
                <TextBlock x:Name="TbCurrentNode"
                           DockPanel.Dock="Left"
                           Text="当前: 无"
                           VerticalAlignment="Center"
                           Margin="2,0,6,0"
                           FontSize="11"
                           Foreground="#FF666666"
                           MaxWidth="120"
                           TextTrimming="CharacterEllipsis"/>

                <!-- 图标工具栏 -->
                <StackPanel Orientation="Horizontal" DockPanel.Dock="Right">
                    <Separator Style="{StaticResource AliasToolbarSeparatorStyle}"/>
                    <Button x:Name="BtnAutoGen" Style="{StaticResource AliasToolbarButtonStyle}"
                            ToolTip="自动生成别名"
                            Click="OnAutoGenClick">
                        <Path Data="{StaticResource AutoGenIconGeometry}"
                              Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}"
                              Width="16" Height="16" Stretch="Uniform"/>
                    </Button>
                    <Separator Style="{StaticResource AliasToolbarSeparatorStyle}"/>
                    <Button x:Name="BtnExport" Style="{StaticResource AliasToolbarButtonStyle}"
                            ToolTip="导出别名 (JSON)"
                            Click="OnExportClick">
                        <Path Data="{StaticResource ExportIconGeometry}"
                              Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}"
                              Width="16" Height="16" Stretch="Uniform"/>
                    </Button>
                    <Button x:Name="BtnImport" Style="{StaticResource AliasToolbarButtonStyle}"
                            ToolTip="导入别名 (JSON)"
                            Click="OnImportClick">
                        <Path Data="{StaticResource ImportIconGeometry}"
                              Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}"
                              Width="16" Height="16" Stretch="Uniform"/>
                    </Button>
                    <Separator Style="{StaticResource AliasToolbarSeparatorStyle}"/>
                    <Button x:Name="BtnClearAll" Style="{StaticResource AliasToolbarButtonStyle}"
                            ToolTip="清除全部别名"
                            Click="OnClearAllClick">
                        <Path Data="{StaticResource ClearIconGeometry}"
                              Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}"
                              Width="16" Height="16" Stretch="Uniform"/>
                    </Button>
                    <Button x:Name="BtnRefresh" Style="{StaticResource AliasToolbarButtonStyle}"
                            ToolTip="刷新树结构"
                            Click="OnRefreshClick">
                        <Path Data="{StaticResource RefreshIconGeometry}"
                              Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}"
                              Width="16" Height="16" Stretch="Uniform"/>
                    </Button>
                </StackPanel>

                <!-- 别名输入框 -->
                <TextBox x:Name="TxtAliasInput"
                         DockPanel.Dock="Right"
                         Width="100"
                         Height="20"
                         FontSize="11"
                         Margin="0,0,4,0"
                         ToolTip="输入别名后回车保存"
                         KeyDown="OnAliasInputKeyDown"/>
            </DockPanel>
        </Border>

        <!-- ── 别名树 ── -->
        <TreeView x:Name="AliasTreeView"
                  MinWidth="300"
                  VirtualizingPanel.IsVirtualizing="False"
                  Background="White"
                  BorderThickness="0"
                  ItemContainerStyle="{StaticResource AliasTreeViewItemStyle}"
                  TreeViewItem.Expanded="OnTreeViewItemExpanded"
                  TreeViewItem.Selected="OnTreeViewItemSelected"
                  TreeViewItem.PreviewMouseRightButtonDown="OnTreeViewItemRightClick"
                  TreeViewItem.PreviewMouseLeftButtonDown="OnTreeViewItemLeftClick">
            <TreeView.Resources>
                <!-- 无焦点时也显示蓝色选中态（浅灰蓝底色 + 白字，匹配选择树） -->
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" 
                                 Color="#3A6EA5"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" 
                                 Color="White"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
                                 Color="#3A6EA5"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" 
                                 Color="White"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" 
                                 Color="#1A1A1A"/>
            </TreeView.Resources>
            <TreeView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="编辑别名" Click="OnCtxEditClick">
                        <MenuItem.Icon>
                            <Path Data="{StaticResource EditIconGeometry}" 
                                  Fill="#FF666666" Width="14" Height="14" Stretch="Uniform"
                                  Margin="0,0,4,0"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="批量编辑子节点..." Click="OnCtxBatchEditClick">
                        <MenuItem.Icon>
                            <Path Data="{StaticResource BatchEditIconGeometry}" 
                                  Fill="#FF666666" Width="14" Height="14" Stretch="Uniform"
                                  Margin="0,0,4,0"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="清除别名" Click="OnCtxClearClick"/>
                    <MenuItem Header="复制原名" Click="OnCtxCopyNameClick"/>
                    <Separator/>
                    <MenuItem Header="导出子树别名..." Click="OnCtxExportSubtreeClick">
                        <MenuItem.Icon>
                            <Path Data="{StaticResource ExportIconGeometry}" 
                                  Fill="#FF666666" Width="14" Height="14" Stretch="Uniform"
                                  Margin="0,0,4,0"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="导入子树别名..." Click="OnCtxImportSubtreeClick">
                        <MenuItem.Icon>
                            <Path Data="{StaticResource ImportIconGeometry}" 
                                  Fill="#FF666666" Width="14" Height="14" Stretch="Uniform"
                                  Margin="0,0,4,0"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <Separator/>
                    <MenuItem Header="复制子树别名" Click="OnCtxCopyAliasTreeClick"/>
                    <MenuItem Header="粘贴子树别名" Click="OnCtxPasteAliasTreeClick"/>
                </ContextMenu>
            </TreeView.ContextMenu>
        </TreeView>

        <!-- ── 底部状态栏 ── -->
        <StatusBar DockPanel.Dock="Bottom"
                   Background="#FFF8F8F8"
                   BorderBrush="#FFDDDDDD"
                   BorderThickness="0,1,0,0"
                   Height="20"
                   Visibility="Collapsed">
            <StatusBarItem x:Name="StatusBarItem">
                <TextBlock x:Name="TbStatus" FontSize="10" Foreground="#FF666666"/>
            </StatusBarItem>
        </StatusBar>
    </DockPanel>
</UserControl>
