<UserControl
    x:Class="XplorePlane.Views.PipelineEditorView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:prism="http://prismlibrary.com/"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    prism:ViewModelLocator.AutoWireViewModel="True"
    d:DesignHeight="700"
    d:DesignWidth="350"
    mc:Ignorable="d">

    <UserControl.Resources>
        <SolidColorBrush x:Key="PanelBg" Color="White" />
        <SolidColorBrush x:Key="PanelBorder" Color="#cdcbcb" />
        <SolidColorBrush x:Key="SeparatorBrush" Color="#E0E0E0" />
        <SolidColorBrush x:Key="AccentBlue" Color="#E3F0FF" />
        <FontFamily x:Key="CsdFont">Microsoft YaHei UI</FontFamily>

        <!--  节点项样式  -->
        <Style x:Key="PipelineNodeItemStyle" TargetType="ListBoxItem">
            <Setter Property="Padding" Value="0" />
            <Setter Property="Margin" Value="0" />
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>

        <!--  工具栏按钮样式  -->
        <Style x:Key="ToolbarBtn" TargetType="Button">
            <Setter Property="Width" Value="28" />
            <Setter Property="Height" Value="28" />
            <Setter Property="Margin" Value="2,0" />
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderBrush" Value="#cdcbcb" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="FontFamily" Value="Microsoft YaHei UI" />
            <Setter Property="FontSize" Value="11" />
            <Setter Property="Cursor" Value="Hand" />
        </Style>
    </UserControl.Resources>

    <Border
        Background="{StaticResource PanelBg}"
        BorderBrush="{StaticResource PanelBorder}"
        BorderThickness="1"
        CornerRadius="4">
        <Grid>
            <Grid.RowDefinitions>
                <!--  Row 0: 工具栏  -->
                <RowDefinition Height="Auto" />
                <!--  Row 1: 流水线节点列表  -->
                <RowDefinition Height="3*" />
                <!--  Row 2: 分隔线  -->
                <RowDefinition Height="Auto" />
                <!--  Row 3: 参数面板  -->
                <RowDefinition Height="2*" MinHeight="80" />
                <!--  Row 4: 状态栏  -->
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <!--  标题栏：流水线名称 + 设备选择  -->
            <!--  工具栏  -->
            <Border
                Grid.Row="0"
                Padding="6,4"
                Background="#F5F5F5"
                BorderBrush="{StaticResource PanelBorder}"
                BorderThickness="0,1,0,1">
                <Grid>
                    <StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
                        <Button
                            Command="{Binding NewPipelineCommand}"
                            Content="新建"
                            Style="{StaticResource ToolbarBtn}"
                            ToolTip="新建流水线" />
                        <Button
                            Command="{Binding SavePipelineCommand}"
                            Content="保存"
                            Style="{StaticResource ToolbarBtn}"
                            ToolTip="保存流水线" />
                        <Button
                            Width="43"
                            Command="{Binding SaveAsPipelineCommand}"
                            Content="另存为"
                            Style="{StaticResource ToolbarBtn}"
                            ToolTip="另存为" />
                        <Button
                            Width="43"
                            Command="{Binding LoadPipelineCommand}"
                            Content="加载"
                            Style="{StaticResource ToolbarBtn}"
                            ToolTip="加载流水线" />
                        <Button
                            Command="{Binding ExecutePipelineCommand}"
                            Content="▶"
                            Style="{StaticResource ToolbarBtn}"
                            ToolTip="执行流水线" />
                        <Button
                            Command="{Binding CancelExecutionCommand}"
                            Content="■"
                            Style="{StaticResource ToolbarBtn}"
                            ToolTip="取消执行" />
                        <Button
                            Command="{Binding DeletePipelineCommand}"
                            Content="🗑"
                            Style="{StaticResource ToolbarBtn}"
                            ToolTip="工具箱" />
                    </StackPanel>
                    <Button
                        Width="50"
                        HorizontalAlignment="Right"
                        Command="{Binding OpenToolboxCommand}"
                        Content="工具箱"
                        Style="{StaticResource ToolbarBtn}"
                        ToolTip="打开算子工具箱" />
                </Grid>
            </Border>

            <!--  流水线节点列表（拖拽目标）  -->
            <ListBox
                x:Name="PipelineListBox"
                Grid.Row="1"
                Background="Transparent"
                BorderThickness="0"
                ItemContainerStyle="{StaticResource PipelineNodeItemStyle}"
                ItemsSource="{Binding PipelineNodes}"
                ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                SelectedItem="{Binding SelectedNode, Mode=TwoWay}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid x:Name="NodeRoot" MinHeight="48">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="44" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>

                            <!--  连接线列：上半段 + 下半段  -->
                            <Line
                                x:Name="TopLine"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Top"
                                Stroke="#5B9BD5"
                                StrokeThickness="2"
                                X1="0"
                                X2="0"
                                Y1="0"
                                Y2="10" />
                            <Line
                                HorizontalAlignment="Center"
                                VerticalAlignment="Bottom"
                                Stroke="#5B9BD5"
                                StrokeThickness="2"
                                X1="0"
                                X2="0"
                                Y1="0"
                                Y2="14" />

                            <!--  算子图标  -->
                            <Border
                                Grid.Column="0"
                                Width="28"
                                Height="28"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                Background="#E8F0FE"
                                BorderBrush="#5B9BD5"
                                BorderThickness="1.5"
                                CornerRadius="4">
                                <TextBlock
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    FontSize="13"
                                    Text="⚙" />
                            </Border>

                            <!--  算子名称  -->
                            <TextBlock
                                Grid.Column="1"
                                Margin="6,0,0,0"
                                VerticalAlignment="Center"
                                FontFamily="Microsoft YaHei UI"
                                FontSize="12"
                                Text="{Binding DisplayName}" />

                            <!--  操作按钮：上移 / 下移 / 删除（悬停显示）  -->
                            <StackPanel
                                x:Name="NodeActions"
                                Grid.Column="2"
                                Margin="0,0,4,0"
                                VerticalAlignment="Center"
                                Orientation="Horizontal"
                                Visibility="Collapsed">
                                <Button
                                    Width="22"
                                    Height="22"
                                    Margin="1,0"
                                    Background="Transparent"
                                    BorderBrush="#cdcbcb"
                                    BorderThickness="1"
                                    Command="{Binding DataContext.MoveNodeUpCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
                                    CommandParameter="{Binding}"
                                    Content="▲"
                                    Cursor="Hand"
                                    FontSize="10"
                                    ToolTip="上移" />
                                <Button
                                    Width="22"
                                    Height="22"
                                    Margin="1,0"
                                    Background="Transparent"
                                    BorderBrush="#cdcbcb"
                                    BorderThickness="1"
                                    Command="{Binding DataContext.MoveNodeDownCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
                                    CommandParameter="{Binding}"
                                    Content="▼"
                                    Cursor="Hand"
                                    FontSize="10"
                                    ToolTip="下移" />
                                <Button
                                    Width="22"
                                    Height="22"
                                    Margin="1,0"
                                    Background="Transparent"
                                    BorderBrush="#E05050"
                                    BorderThickness="1"
                                    Command="{Binding DataContext.RemoveOperatorCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
                                    CommandParameter="{Binding}"
                                    Content="✕"
                                    Cursor="Hand"
                                    FontSize="10"
                                    ToolTip="删除" />
                            </StackPanel>
                        </Grid>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Order}" Value="0">
                                <Setter TargetName="TopLine" Property="Visibility" Value="Collapsed" />
                            </DataTrigger>
                            <Trigger SourceName="NodeRoot" Property="IsMouseOver" Value="True">
                                <Setter TargetName="NodeActions" Property="Visibility" Value="Visible" />
                            </Trigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <!--  分隔线  -->
            <Rectangle
                Grid.Row="2"
                Height="1"
                Fill="{StaticResource SeparatorBrush}" />

            <!--  参数面板  -->
            <ScrollViewer
                Grid.Row="3"
                HorizontalScrollBarVisibility="Disabled"
                VerticalScrollBarVisibility="Auto">
                <StackPanel Margin="8,6">
                    <TextBlock
                        Margin="0,0,0,4"
                        FontFamily="{StaticResource CsdFont}"
                        FontSize="11"
                        FontWeight="Bold"
                        Foreground="#555"
                        Text="参数配置" />
                    <ItemsControl ItemsSource="{Binding SelectedNode.Parameters}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Grid Margin="0,3">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="100" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <TextBlock
                                        Grid.Column="0"
                                        VerticalAlignment="Center"
                                        FontFamily="Microsoft YaHei UI"
                                        FontSize="11"
                                        Text="{Binding DisplayName}"
                                        TextTrimming="CharacterEllipsis" />
                                    <TextBox
                                        Grid.Column="1"
                                        Padding="4,2"
                                        BorderBrush="#cdcbcb"
                                        BorderThickness="1"
                                        FontFamily="Microsoft YaHei UI"
                                        FontSize="11"
                                        Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}">
                                        <TextBox.Style>
                                            <Style TargetType="TextBox">
                                                <Setter Property="BorderBrush" Value="#cdcbcb" />
                                                <Setter Property="Background" Value="White" />
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding IsValueValid}" Value="False">
                                                        <Setter Property="BorderBrush" Value="Red" />
                                                        <Setter Property="Background" Value="#FFF0F0" />
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </TextBox.Style>
                                    </TextBox>
                                </Grid>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
            </ScrollViewer>

            <!--  状态栏  -->
            <Border
                Grid.Row="4"
                Padding="6,4"
                Background="#F5F5F5"
                BorderBrush="{StaticResource PanelBorder}"
                BorderThickness="0,1,0,0">
                <TextBlock
                    FontFamily="{StaticResource CsdFont}"
                    FontSize="11"
                    Foreground="#555"
                    Text="{Binding StatusMessage, StringFormat='Status: {0}'}"
                    TextTrimming="CharacterEllipsis" />
            </Border>
        </Grid>
    </Border>
</UserControl>