<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="using:ClaudeDo.Ui.ViewModels.Islands"
             xmlns:islands="using:ClaudeDo.Ui.Views.Islands"
             xmlns:converters="using:Avalonia.Data.Converters"
             xmlns:loc="using:ClaudeDo.Ui.Localization"
             x:Class="ClaudeDo.Ui.Views.Islands.TasksIslandView"
             x:DataType="vm:TasksIslandViewModel">
  <DockPanel LastChildFill="True">

    <!-- Header -->
    <Border DockPanel.Dock="Top" Classes="island-header">
      <Grid ColumnDefinitions="*,Auto">
        <StackPanel Grid.Column="0" Spacing="4">
          <TextBlock Classes="eyebrow" Text="{Binding HeaderEyebrow}"/>
          <TextBlock Classes="display"
                     Text="{Binding HeaderTitle}"
                     TextTrimming="CharacterEllipsis"/>
          <TextBlock Classes="meta"
                     Foreground="{DynamicResource TextMuteBrush}"
                     Text="{Binding Subtitle}"
                     TextTrimming="CharacterEllipsis"/>
        </StackPanel>

        <StackPanel Grid.Column="1" Orientation="Horizontal" Spacing="4"
                    VerticalAlignment="Top">
          <Border Classes="kbd" VerticalAlignment="Center" Margin="0,0,6,0"
                  IsVisible="{Binding HasStatusPill}">
            <TextBlock Text="{Binding StatusPill}"/>
          </Border>
          <Button Classes="icon-btn" Classes.active="{Binding IsShowingCompleted}"
                  Command="{Binding ToggleShowCompletedCommand}"
                  ToolTip.Tip="{loc:Tr tasks.showCompletedTip}">
            <PathIcon Width="15" Height="15" Data="{StaticResource Icon.Eye}"/>
          </Button>
          <Button Classes="icon-btn" IsVisible="{Binding IsMyDayList}"
                  Command="{Binding ClearDayCommand}" ToolTip.Tip="{loc:Tr tasks.clearDayTip}">
            <PathIcon Width="15" Height="15" Data="{StaticResource Icon.Broom}"/>
          </Button>
          <Button Classes="icon-btn" IsVisible="{Binding IsMyDayList}"
                  Command="{Binding ShowPrepLogCommand}" ToolTip.Tip="{loc:Tr tasks.planMyDayTip}">
            <Viewbox Width="18" Height="18">
              <Path Classes="plan-icon" Data="{StaticResource Icon.PlanDay}"/>
            </Viewbox>
          </Button>
          <Button Classes="icon-btn" Command="{Binding OpenListSettingsCommand}" ToolTip.Tip="{loc:Tr tasks.listSettingsTip}">
            <PathIcon Width="15" Height="15" Data="{StaticResource Icon.Settings}"/>
          </Button>
        </StackPanel>
      </Grid>
    </Border>

    <!-- Add-task row -->
    <Border DockPanel.Dock="Top" Classes="add-task" Margin="16,14,16,8">
      <Grid ColumnDefinitions="Auto,*,Auto">
        <Border Grid.Column="0" Classes="add-task-plus" VerticalAlignment="Center">
          <PathIcon Width="12" Height="12" Data="{StaticResource Icon.Plus}"
                    Foreground="{DynamicResource TextFaintBrush}"/>
        </Border>
        <TextBox Grid.Column="1" x:Name="AddTaskBox" Classes="add-task-input"
                 PlaceholderText="{loc:Tr tasks.addPlaceholder}"
                 Text="{Binding NewTaskTitle, Mode=TwoWay}"
                 VerticalAlignment="Center"
                 Margin="12,0,0,0">
          <TextBox.KeyBindings>
            <KeyBinding Gesture="Enter" Command="{Binding AddCommand}"/>
          </TextBox.KeyBindings>
        </TextBox>
        <Border Grid.Column="2" Classes="kbd kbd-enter" VerticalAlignment="Center"
                IsVisible="{Binding #AddTaskBox.IsFocused}">
          <TextBlock Text="{loc:Tr tasks.enterKey}"/>
        </Border>
      </Grid>
    </Border>

    <!-- Notes pinned row (My Day only) -->
    <Button DockPanel.Dock="Top"
            Classes="btn" HorizontalAlignment="Stretch" HorizontalContentAlignment="Left"
            Margin="16,0,16,8"
            IsVisible="{Binding ShowNotesRow}"
            Command="{Binding OpenNotesCommand}"
            Content="{loc:Tr tasks.notesPinnedRow}"/>

    <!-- Task list -->
    <ScrollViewer>
      <StackPanel Margin="10,4">

        <!-- OVERDUE -->
        <StackPanel IsVisible="{Binding HasOverdue}">
          <TextBlock Classes="eyebrow section-label overdue"
                     Text="{loc:Tr tasks.overdue}" Margin="14,14,14,6"/>
          <ItemsControl ItemsSource="{Binding OverdueItems}">
            <ItemsControl.ItemTemplate>
              <DataTemplate DataType="vm:TaskRowViewModel">
                <Button Classes="flat" HorizontalAlignment="Stretch"
                        HorizontalContentAlignment="Stretch"
                        Command="{Binding $parent[ItemsControl].((vm:TasksIslandViewModel)DataContext).SelectCommand}"
                        CommandParameter="{Binding}"
                        PointerPressed="OnRowPointerPressed"
                        PointerMoved="OnRowPointerMoved"
                        PointerReleased="OnRowPointerReleased"
                        DragDrop.AllowDrop="True"
                        DragDrop.DragOver="OnRowDragOver"
                        DragDrop.Drop="OnRowDrop">
                  <islands:TaskRowView/>
                </Button>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>
        </StackPanel>

        <!-- TASKS -->
        <StackPanel IsVisible="{Binding HasOpen}">
          <TextBlock Classes="eyebrow section-label"
                     Text="{loc:Tr tasks.tasks}" Margin="14,14,14,6"
                     IsVisible="{Binding ShowOpenLabel}"/>
          <ItemsControl ItemsSource="{Binding OpenItems}">
            <ItemsControl.ItemTemplate>
              <DataTemplate DataType="vm:TaskRowViewModel">
                <Button Classes="flat" HorizontalAlignment="Stretch"
                        HorizontalContentAlignment="Stretch"
                        Command="{Binding $parent[ItemsControl].((vm:TasksIslandViewModel)DataContext).SelectCommand}"
                        CommandParameter="{Binding}"
                        PointerPressed="OnRowPointerPressed"
                        PointerMoved="OnRowPointerMoved"
                        PointerReleased="OnRowPointerReleased"
                        DragDrop.AllowDrop="True"
                        DragDrop.DragOver="OnRowDragOver"
                        DragDrop.Drop="OnRowDrop">
                  <islands:TaskRowView/>
                </Button>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>
        </StackPanel>

        <!-- COMPLETED -->
        <StackPanel>
          <StackPanel.IsVisible>
            <MultiBinding Converter="{x:Static converters:BoolConverters.And}">
              <Binding Path="HasCompleted"/>
              <Binding Path="IsShowingCompleted"/>
            </MultiBinding>
          </StackPanel.IsVisible>
          <Grid ColumnDefinitions="*,Auto" Margin="14,14,14,6">
            <TextBlock Grid.Column="0" Classes="eyebrow section-label"
                       Text="{Binding CompletedHeader}" VerticalAlignment="Center"/>
            <Button Grid.Column="1" Classes="icon-btn"
                    Command="{Binding ClearCompletedCommand}"
                    ToolTip.Tip="{loc:Tr tasks.clearCompletedTip}"
                    VerticalAlignment="Center">
              <PathIcon Data="{StaticResource Icon.Trash}" Width="13" Height="13"
                        Foreground="{DynamicResource BloodBrush}"/>
            </Button>
          </Grid>
          <ItemsControl ItemsSource="{Binding CompletedItems}">
            <ItemsControl.ItemTemplate>
              <DataTemplate DataType="vm:TaskRowViewModel">
                <Button Classes="flat" HorizontalAlignment="Stretch"
                        HorizontalContentAlignment="Stretch"
                        Command="{Binding $parent[ItemsControl].((vm:TasksIslandViewModel)DataContext).SelectCommand}"
                        CommandParameter="{Binding}"
                        PointerPressed="OnRowPointerPressed"
                        PointerMoved="OnRowPointerMoved"
                        PointerReleased="OnRowPointerReleased"
                        DragDrop.AllowDrop="True"
                        DragDrop.DragOver="OnRowDragOver"
                        DragDrop.Drop="OnRowDrop">
                  <islands:TaskRowView/>
                </Button>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>
        </StackPanel>

      </StackPanel>
    </ScrollViewer>

  </DockPanel>
</UserControl>
