<Window x:Class="NavisworksTransport.UI.WPF.Views.TimeTagDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:NavisworksTransport.UI.WPF.Views"
        xmlns:controls="clr-namespace:NavisworksTransport.UI.WPF.Controls"
        xmlns:converters="clr-namespace:NavisworksTransport.UI.WPF.Converters"
        Title="时标设置" 
        Height="750" Width="1100"
        WindowStartupLocation="CenterOwner"
        ResizeMode="CanResize"
        Topmost="True"
        x:Name="root">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/TransportPlugin;component/src/UI/WPF/Resources/NavisworksStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <!-- 转换器 -->
            <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
            <local:InverseBooleanToVisibilityConverter x:Key="InverseBoolToVisibilityConverter"/>
            <converters:EditableNumberConverter x:Key="EditableNumberConverter"/>
        </ResourceDictionary>
    </Window.Resources>
    
    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- 标题和路径信息 -->
        <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="0,0,0,10">
            <TextBlock Text="路径: " FontWeight="Bold" FontSize="14"/>
            <TextBlock Text="{Binding RouteName}" FontWeight="Bold" FontSize="14"/>
        </StackPanel>

        <!-- 事件时间线可视化 -->
        <Border Grid.Row="1" BorderBrush="#FFDDDDDD" BorderThickness="1" Margin="0,0,0,10" 
                Height="140" IsEnabled="{Binding CanEdit}">
            <controls:EventTimelineControl 
                Events="{Binding Events}"
                TotalDistance="{Binding TotalDistanceValue}"
                SelectedEvent="{Binding SelectedEvent, Mode=TwoWay}"/>
        </Border>

        <!-- 配置选择 -->
        <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="0,0,0,10">
            <TextBlock Text="时标配置: " VerticalAlignment="Center" Margin="0,0,5,0" FontWeight="SemiBold"/>
            <TextBlock Text="时标配置: " VerticalAlignment="Center" Margin="0,0,5,0" FontWeight="SemiBold"/>
            <ComboBox Width="200" 
                      ItemsSource="{Binding Profiles}" 
                      SelectedValue="{Binding SelectedProfileId}"
                      SelectedValuePath="Id"
                      DisplayMemberPath="Name"/>
            <Button Content="新建配置" Margin="10,0,5,0" Padding="15,3" Command="{Binding CreateProfileCommand}"/>
            <Button Content="删除配置" Margin="5,0,0,0" Padding="15,3" Command="{Binding DeleteProfileCommand}"/>
            <Separator Margin="20,0" Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}"/>
            <Button Content="导出" Padding="15,3" Command="{Binding ExportCommand}"/>
            <Button Content="导入到TimeLiner" Padding="15,3" Margin="10,0,0,0" Command="{Binding ImportToTimeLinerCommand}"/>
        </StackPanel>

        <!-- 主内容区 -->
        <Grid Grid.Row="3" IsEnabled="{Binding CanEdit}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="190"/>
                <ColumnDefinition Width="2.5*"/>
                <ColumnDefinition Width="260"/>
            </Grid.ColumnDefinitions>

            <!-- 左侧：参数设置 -->
            <StackPanel Grid.Column="0" Margin="0,0,10,0">
                <GroupBox Header="全局参数" Padding="10">
                    <StackPanel>
                        <TextBlock Text="默认速度 (m/s):" Margin="0,0,0,5" FontWeight="SemiBold"/>
                        <Slider Minimum="0.1" Maximum="5" 
                                Value="{Binding DefaultSpeed, Mode=TwoWay}" 
                                TickFrequency="0.1" IsSnapToTickEnabled="True"/>
                        <TextBlock Text="{Binding DefaultSpeed, StringFormat={}{0:F1}}" 
                                   HorizontalAlignment="Center" Margin="0,2,0,15"/>

                        <TextBlock Text="转弯速度比例:" Margin="0,0,0,5" FontWeight="SemiBold"/>
                        <Slider Minimum="0.1" Maximum="1" 
                                Value="{Binding TurnSpeedRatio, Mode=TwoWay}" 
                                TickFrequency="0.1" IsSnapToTickEnabled="True"/>
                        <TextBlock Text="{Binding TurnSpeedRatio, StringFormat=P0}" 
                                   HorizontalAlignment="Center" Margin="0,2,0,15"/>

                        <Separator Margin="0,10"/>

                        <TextBlock Text="总距离:" FontWeight="Bold"/>
                        <TextBlock Text="{Binding TotalDistanceText}" Margin="0,0,0,10" FontSize="14"/>
                        
                        <TextBlock Text="总时间:" FontWeight="Bold"/>
                        <TextBlock Text="{Binding TotalTimeText}" Foreground="Blue" FontSize="18" FontWeight="Bold"/>
                    </StackPanel>
                </GroupBox>
            </StackPanel>

            <!-- 中间：事件列表 -->
            <GroupBox Grid.Column="1" Header="事件节点列表">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <!-- 列表工具栏 -->
                    <ToolBar Grid.Row="0" Margin="0,0,0,5">
                        <Button Content="插入事件" Command="{Binding InsertEventCommand}" Padding="8,2"
                                ToolTip="在选中事件后插入新事件，未选中则在起点后插入"/>
                        <Button Content="删除事件" Command="{Binding DeleteEventCommand}" Padding="8,2" Margin="5,0"
                                Background="#FFFFE6E6" ToolTip="删除选中的事件节点"/>
                    </ToolBar>

                    <!-- 事件表格 -->
                    <DataGrid Grid.Row="1" 
                              ItemsSource="{Binding Events}" 
                              SelectedItem="{Binding SelectedEvent}"
                              AutoGenerateColumns="False" 
                              CanUserAddRows="False"
                              CanUserDeleteRows="False"
                              SelectionMode="Single"
                              GridLinesVisibility="Horizontal"
                              AlternatingRowBackground="#FFF0F8FF"
                              HorizontalScrollBarVisibility="Auto"
                              VerticalScrollBarVisibility="Auto"
                              HeadersVisibility="Column"
                              RowHeaderWidth="0">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="序号" Binding="{Binding SortOrder}" Width="50" IsReadOnly="True"/>
                            <DataGridTextColumn Header="事件名称" Binding="{Binding Name}" Width="*" MinWidth="120"/>
                            <DataGridTextColumn Header="类型" Binding="{Binding Type}" Width="80" IsReadOnly="True"/>
                            <DataGridTextColumn Header="距离(m)" Binding="{Binding Distance, StringFormat=F2}" Width="85" IsReadOnly="True"/>
                            <DataGridTextColumn Header="等待(s)" Binding="{Binding WaitTime, StringFormat=F1}" Width="75" IsReadOnly="True"/>
                            <DataGridTextColumn Header="动作(s)" Binding="{Binding ActionTime, StringFormat=F1}" Width="75" IsReadOnly="True"/>
                            <DataGridTextColumn Header="累计时间(s)" Binding="{Binding CumulativeTime, StringFormat=F2}" Width="95" IsReadOnly="True"/>
                        </DataGrid.Columns>
                    </DataGrid>

                    <!-- 提示 -->
                    <TextBlock Grid.Row="2" Text="提示：点击事件选中，在右侧编辑详情" 
                               Foreground="Gray" FontStyle="Italic" Margin="0,5,0,0"/>
                </Grid>
            </GroupBox>

            <!-- 右侧：事件详情编辑 -->
            <GroupBox Grid.Column="2" Header="事件详情" IsEnabled="{Binding CanEditEvent}">
                <StackPanel Margin="10">
                    <TextBlock Text="选中事件:" FontWeight="Bold" Margin="0,0,0,5"/>
                    <TextBlock Text="{Binding SelectedEvent.Name}" FontSize="16" Margin="0,0,0,15" 
                               Foreground="#FF2B579A"/>

                    <TextBlock Text="事件类型:" FontWeight="SemiBold" Margin="0,0,0,3"/>
                    <!-- 事件类型可编辑时显示ComboBox -->
                    <ComboBox ItemsSource="{Binding DataContext.EventTypes, ElementName=root}"
                              SelectedItem="{Binding SelectedEventType, Mode=TwoWay}"
                              Margin="0,0,0,15"
                              Visibility="{Binding IsSelectedEventTypeEditable, Converter={StaticResource BoolToVisibilityConverter}}"/>
                    <!-- 事件类型不可编辑时显示只读文本 -->
                    <TextBlock Text="{Binding SelectedEventType}" 
                               FontSize="14" Foreground="Gray" Margin="0,0,0,15"
                               Visibility="{Binding IsSelectedEventTypeEditable, Converter={StaticResource InverseBoolToVisibilityConverter}}"/>

                    <TextBlock Text="距离 (米):" FontWeight="SemiBold" Margin="0,0,0,3"/>
                    <!-- 距离可编辑时显示Slider和输入框 -->
                    <Slider x:Name="DistanceSlider"
                            Minimum="0" 
                            Maximum="{Binding TotalDistanceValue, FallbackValue=100}" 
                            Value="{Binding SelectedDistance, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
                            TickFrequency="1" IsSnapToTickEnabled="True"
                            Visibility="{Binding IsSelectedDistanceEditable, Converter={StaticResource BoolToVisibilityConverter}}"
                            PreviewMouseLeftButtonDown="DistanceSlider_PreviewMouseLeftButtonDown"
                            PreviewMouseLeftButtonUp="DistanceSlider_PreviewMouseLeftButtonUp"/>
                    <TextBox Text="{Binding SelectedDistance, Mode=TwoWay, UpdateSourceTrigger=LostFocus, Converter={StaticResource EditableNumberConverter}, ConverterParameter=F2, ValidatesOnExceptions=True, NotifyOnValidationError=True}" 
                             Margin="0,0,0,15" HorizontalAlignment="Right" Width="80"
                             Visibility="{Binding IsSelectedDistanceEditable, Converter={StaticResource BoolToVisibilityConverter}}"/>
                    <!-- 距离不可编辑时显示只读文本 -->
                    <TextBlock Text="{Binding SelectedDistanceDisplay}" 
                               FontSize="14" Foreground="Gray" Margin="0,0,0,15"
                               Visibility="{Binding IsSelectedDistanceEditable, Converter={StaticResource InverseBoolToVisibilityConverter}}"/>

                    <TextBlock Text="等待时间 (秒):" FontWeight="SemiBold" Margin="0,0,0,3"/>
                    <Slider Minimum="0" Maximum="60" 
                            Value="{Binding SelectedWaitTime, Mode=TwoWay}"
                            TickFrequency="1" IsSnapToTickEnabled="True"/>
                    <TextBox Text="{Binding SelectedWaitTime, Mode=TwoWay, UpdateSourceTrigger=LostFocus, Converter={StaticResource EditableNumberConverter}, ConverterParameter=F1, ValidatesOnExceptions=True, NotifyOnValidationError=True}" 
                             Margin="0,0,0,15" HorizontalAlignment="Right" Width="80"/>

                    <TextBlock Text="动作时间 (秒):" FontWeight="SemiBold" Margin="0,0,0,3"/>
                    <Slider Minimum="0" Maximum="60" 
                            Value="{Binding SelectedActionTime, Mode=TwoWay}"
                            TickFrequency="1" IsSnapToTickEnabled="True"/>
                    <TextBox Text="{Binding SelectedActionTime, Mode=TwoWay, UpdateSourceTrigger=LostFocus, Converter={StaticResource EditableNumberConverter}, ConverterParameter=F1, ValidatesOnExceptions=True, NotifyOnValidationError=True}" 
                             Margin="0,0,0,15" HorizontalAlignment="Right" Width="80"/>

                    <Separator Margin="0,10"/>

                    <TextBlock Text="当前累计时间:" FontWeight="SemiBold"/>
                    <TextBlock Text="{Binding SelectedEvent.CumulativeTime, StringFormat={}{0:F2} 秒}" 
                               FontSize="14" Foreground="Green"/>
                </StackPanel>
            </GroupBox>
        </Grid>

        <!-- 底部按钮 -->
        <StackPanel Grid.Row="4" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,0,0">
            <Button Content="刷新" Padding="20,5" Margin="0,0,10,0" Command="{Binding RefreshCommand}"/>
            <Button Content="关闭" Padding="20,5" Click="CloseButton_Click"/>
        </StackPanel>
    </Grid>
</Window>
