<Window x:Class="TodoList.Views.MainWindow"
        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:local="clr-namespace:TodoList.Views"
        xmlns:models="clr-namespace:TodoList.Models"
        xmlns:converters="clr-namespace:TodoList.Converters"
        mc:Ignorable="d"
        Title="{Binding AppVersion, StringFormat='待办事项 v{0}'}" Height="450" Width="350"
        Background="#F5F5F7"
        Icon="/icon.ico"
        WindowStartupLocation="CenterScreen"
        PreviewKeyDown="Window_PreviewKeyDown">
    
    <Window.Resources>
        <ObjectDataProvider x:Key="PriorityEnum" MethodName="GetValues"
                            ObjectType="{x:Type sys:Enum}"
                            xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="models:TodoPriority"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        
        <ObjectDataProvider x:Key="SortByEnum" MethodName="GetValues"
                            ObjectType="{x:Type sys:Enum}"
                            xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="models:SortBy"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        
        <ObjectDataProvider x:Key="SortOrderEnum" MethodName="GetValues"
                            ObjectType="{x:Type sys:Enum}"
                            xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="models:SortOrder"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        
        <Style x:Key="ModernButton" TargetType="Button">
            <Setter Property="Background" Value="#007AFF"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Padding" Value="10,5"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Background="{TemplateBinding Background}" CornerRadius="5">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#0062CC"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <BooleanToVisibilityConverter x:Key="BoolToVis"/>
        <converters:EnumDescriptionConverter x:Key="EnumDescConverter"/>
    </Window.Resources>

    <!-- Force Rebuild Trigger -->
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/> <!-- Toolbar -->
            <RowDefinition Height="Auto"/> <!-- Input -->
            <RowDefinition Height="*"/>    <!-- List -->
            <RowDefinition Height="Auto"/> <!-- Footer -->
        </Grid.RowDefinitions>

        <!-- Header / Toolbar -->
        <Border Grid.Row="0" Background="White" Padding="10" Effect="{DynamicResource {x:Static DropShadowEffect.ShadowDepthProperty}}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                
                <StackPanel Grid.Column="1" Orientation="Horizontal" Margin="5,0,10,0" VerticalAlignment="Center">
                    <ComboBox ItemsSource="{Binding Source={StaticResource SortByEnum}}"
                              SelectedItem="{Binding SortBy}"
                              Width="70" VerticalContentAlignment="Center" Margin="0,0,3,0">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Converter={StaticResource EnumDescConverter}}"/>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>
                    <ComboBox ItemsSource="{Binding Source={StaticResource SortOrderEnum}}"
                              SelectedItem="{Binding SortOrder}"
                              Width="50" VerticalContentAlignment="Center">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Converter={StaticResource EnumDescConverter}}"/>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>
                </StackPanel>
                
                <Button Grid.Column="2" Content="设置快捷键" 
                        Command="{Binding OpenSettingsCommand}"
                        Background="Transparent" Foreground="#007AFF" Margin="0,0,10,0" FontSize="11">
                    <Button.Template>
                        <ControlTemplate TargetType="Button">
                             <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

                <CheckBox Grid.Column="3" Content="已完成" 
                          IsChecked="{Binding ShowCompleted}"
                          VerticalAlignment="Center" Foreground="#555" FontSize="11"/>
            </Grid>
        </Border>

        <!-- Input Area -->
        <Border Grid.Row="1" Margin="10" Background="White" CornerRadius="6" Padding="8">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                
                <TextBox Text="{Binding NewContent, UpdateSourceTrigger=PropertyChanged}" 
                         FontSize="12" Padding="4" Margin="0,0,8,0" BorderThickness="0,0,0,1"
                         VerticalContentAlignment="Center"
                         Tag="添加新任务...">
                     <TextBox.Style>
                        <Style TargetType="TextBox">
                            <Style.Triggers>
                                <Trigger Property="Text" Value="">
                                    <!-- Placeholder could be done with a visual brush or adornment, keeping it simple for now -->
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                     </TextBox.Style>
                     <TextBox.InputBindings>
                        <KeyBinding Key="Enter" Command="{Binding AddTaskCommand}"/>
                    </TextBox.InputBindings>
                </TextBox>
                
                <ComboBox Grid.Column="1" ItemsSource="{Binding Source={StaticResource PriorityEnum}}"
                          SelectedItem="{Binding NewPriority}"
                          Width="65" Margin="0,0,8,0" VerticalContentAlignment="Center">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Converter={StaticResource EnumDescConverter}}"/>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
                
                <Button Grid.Column="2" Content="添加" Command="{Binding AddTaskCommand}" Style="{StaticResource ModernButton}"
                        VerticalAlignment="Center" Height="28" Width="65" FontSize="12"/>
            </Grid>
        </Border>

        <!-- Task List -->
        <ListBox Grid.Row="2" ItemsSource="{Binding Tasks}" 
                 HorizontalContentAlignment="Stretch" 
                 Background="Transparent" BorderThickness="0"
                 Margin="10,0,10,10"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="Background" Value="White"/>
                    <Setter Property="Margin" Value="0,0,0,6"/>
                    <Setter Property="Padding" Value="8"/>
                    <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_MouseDoubleClick"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <Border Background="{TemplateBinding Background}" CornerRadius="8" Padding="{TemplateBinding Padding}">
                                    <ContentPresenter/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Priority}" Value="High">
                            <Setter Property="Background" Value="#FFCDD2"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Priority}" Value="Medium">
                            <Setter Property="Background" Value="#FFE0B2"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Priority}" Value="Low">
                            <Setter Property="Background" Value="#C8E6C9"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>

                        <CheckBox IsChecked="{Binding IsCompleted}" 
                                  Command="{Binding DataContext.ToggleCompleteCommand, RelativeSource={RelativeSource AncestorType=Window}}"
                                  CommandParameter="{Binding}"
                                  VerticalAlignment="Center">
                            <CheckBox.LayoutTransform>
                                <ScaleTransform ScaleX="1.0" ScaleY="1.0"/>
                            </CheckBox.LayoutTransform>
                        </CheckBox>

                        <StackPanel Grid.Column="1" Margin="10,0">
                            <TextBlock Text="{Binding Content}" FontSize="13" VerticalAlignment="Center">
                                <TextBlock.Style>
                                    <Style TargetType="TextBlock">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsCompleted}" Value="True">
                                                <Setter Property="TextDecorations" Value="Strikethrough"/>
                                                <Setter Property="Foreground" Value="#999"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding IsCompleted}" Value="False">
                                                <Setter Property="Foreground" Value="#333"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                            <TextBlock Text="{Binding Priority, Converter={StaticResource EnumDescConverter}}" FontSize="10" Foreground="#888" Margin="0,1,0,0"/>
                        </StackPanel>

                        <Button Grid.Column="2" Content="✎" 
                                Command="{Binding DataContext.OpenEditDialogCommand, RelativeSource={RelativeSource AncestorType=Window}}"
                                CommandParameter="{Binding}"
                                Width="20" Height="20" 
                                Background="Transparent" Foreground="#007AFF" 
                                BorderThickness="0" FontSize="10" FontWeight="Bold"
                                Cursor="Hand" Margin="0,0,4,0">
                            <Button.Template>
                                <ControlTemplate TargetType="Button">
                                    <Border x:Name="border" Background="{TemplateBinding Background}" CornerRadius="12">
                                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                    </Border>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter TargetName="border" Property="Background" Value="#1A007AFF"/>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Button.Template>
                        </Button>

                        <Button Grid.Column="3" Content="✕" 
                                Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource AncestorType=Window}}"
                                CommandParameter="{Binding}"
                                Width="20" Height="20" 
                                Background="Transparent" Foreground="#FF3B30" 
                                BorderThickness="0" FontSize="10" FontWeight="Bold"
                                Cursor="Hand">
                            <Button.Template>
                                <ControlTemplate TargetType="Button">
                                    <Border x:Name="border" Background="{TemplateBinding Background}" CornerRadius="12">
                                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                    </Border>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter TargetName="border" Property="Background" Value="#1AFF3B30"/>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Button.Template>
                        </Button>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>


        <!-- Edit Dialog Overlay -->
        <Grid Grid.RowSpan="3" Background="#80000000" Visibility="{Binding IsEditDialogOpen, Converter={StaticResource BoolToVis}}">
            <Border Background="White" Width="300" Height="220" CornerRadius="8" VerticalAlignment="Center" HorizontalAlignment="Center" Padding="15">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    
                    <TextBlock Text="编辑任务" FontSize="15" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,0,0,12"/>
                    
                    <StackPanel Grid.Row="1" VerticalAlignment="Center">
                        <TextBlock Text="任务内容" Foreground="#666" Margin="0,0,0,4" FontSize="10"/>
                        <TextBox Text="{Binding EditContent, UpdateSourceTrigger=PropertyChanged}" 
                                 FontSize="12" Padding="6" Margin="0,0,0,12" BorderThickness="1" BorderBrush="#DDD"
                                 VerticalContentAlignment="Center"/>
                        
                        <TextBlock Text="优先级" Foreground="#666" Margin="0,0,0,4" FontSize="10"/>
                        <ComboBox ItemsSource="{Binding Source={StaticResource PriorityEnum}}"
                                  SelectedItem="{Binding EditPriority}"
                                  Width="260" VerticalContentAlignment="Center" BorderThickness="1" BorderBrush="#DDD" FontSize="11">
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Converter={StaticResource EnumDescConverter}}"/>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                    </StackPanel>
                    
                    <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,12,0,0">
                        <Button Content="取消" Command="{Binding CloseEditDialogCommand}" Width="70" Margin="0,0,8,0"
                                Background="#EEE" Foreground="#333" Height="26" BorderThickness="0" FontSize="11">
                             <Button.Template>
                                <ControlTemplate TargetType="Button">
                                    <Border Background="{TemplateBinding Background}" CornerRadius="4">
                                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                    </Border>
                                </ControlTemplate>
                            </Button.Template>
                        </Button>
                        <Button Content="保存" Command="{Binding SaveEditCommand}" Width="70" Height="26" Style="{StaticResource ModernButton}" FontSize="11"/>
                    </StackPanel>
                </Grid>
            </Border>
        </Grid>

        <!-- Footer -->
        <Border Grid.Row="3" Background="#F5F5F7" Padding="10,8" HorizontalAlignment="Center">
            <TextBlock FontSize="10">
                <Hyperlink NavigateUri="https://github.com/xinshoushangdao/TodoList" RequestNavigate="Hyperlink_RequestNavigate">
                    <Run Text="GitHub Repository"/>
                </Hyperlink>
            </TextBlock>
        </Border>
    </Grid>
</Window>
