<UserControl xmlns="https://github.com/avaloniaui"
             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"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="ReceiptPDFBuilder.Views.MainView"
             xmlns:helpers="clr-namespace:ReceiptPDFBuilder.Helpers"
             xmlns:models="clr-namespace:ReceiptPDFBuilder.Models"
             xmlns:vm="clr-namespace:ReceiptPDFBuilder.ViewModels"
             xmlns:progRing="clr-namespace:AvaloniaProgressRing;assembly=AvaloniaProgressRing"
             x:DataType="vm:MainViewModel">
  <Grid ColumnDefinitions="*"
        RowDefinitions="Auto, 2*, Auto, Auto, *">
    <StackPanel Orientation="Vertical"
                Spacing="2">
        <Label Content="Easy Receipt Folder -> PDF Builder"
               FontSize="20"
               FontWeight="Bold"
               HorizontalAlignment="Center"/>
        <Button Content="Choose Receipt Folder"
                Command="{Binding ChooseFolder}"
                IsEnabled="{Binding !IsCreatingPDF}" />
        <Label Content="Report Title"
               IsVisible="{Binding IsTitleBoxVisible}" />
        <TextBox Text="{Binding ReportTitle}"
                 IsVisible="{Binding IsTitleBoxVisible}"
                 Watermark="Receipts December 2024"
                 Margin="2,0,2,4"/>
    </StackPanel>
    <DataGrid x:Name="FilesGrid"
              Classes="DragAndDrop ItemsDragAndDrop"
              Grid.Row="1"
              Grid.Column="0"
              Grid.ColumnSpan="2"
              Margin="2"
              ItemsSource="{Binding ReportFiles}"
              AutoGenerateColumns="False"
              IsReadOnly="False"
              GridLinesVisibility="All"
              CanUserReorderColumns="False"
              CanUserResizeColumns="True"
              CanUserSortColumns="False"
              BorderThickness="1"
              VerticalScrollBarVisibility="Visible"
              ScrollViewer.AllowAutoHide="False"
              HorizontalScrollBarVisibility="Disabled"
              HeadersVisibility="All"
              BorderBrush="Gray">
        <DataGrid.Styles>
            <Style Selector="TextBlock">
                <Setter Property="TextWrapping" Value="NoWrap" />
                <Setter Property="TextTrimming" Value="CharacterEllipsis" />
            </Style>
            <Style Selector="TextBox">
                <Setter Property="TextWrapping" Value="NoWrap" />
            </Style>
        </DataGrid.Styles>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Title"  
                                Binding="{Binding Title}"
                                IsReadOnly="False"
                                Width="*"/>
            <DataGridTemplateColumn Header="Receipt Date"
                                IsReadOnly="False"
                                Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Label Content="{Binding ReceiptDate}" 
                               VerticalAlignment="Center"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate DataType="models:ReportFile">
                        <CalendarDatePicker SelectedDate="{Binding ReceiptDateTime}"
                                            DisplayDate="{Binding ReceiptDateTime}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Header="File Name" 
                                Binding="{Binding FileName}"
                                IsReadOnly="True"
                                Width="*" />
            <DataGridTemplateColumn Header=""
                                    IsReadOnly="True"
                                    Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal"
                                    Spacing="4">
                            <Button Command="{Binding $parent[DataGrid].((vm:MainViewModel)DataContext).EditFileProperties}"
                                    CommandParameter="{Binding}"
                                    Classes="accent"
                                    Margin="2"
                                    IsEnabled="True">
                                <Button.Content>
                                    <TextBlock><Run Text="&#xf1f8;" FontFamily="{StaticResource FontAwesomeSolid}"/> Edit</TextBlock>
                                </Button.Content>
                            </Button>
                            <Button Command="{Binding $parent[DataGrid].((vm:MainViewModel)DataContext).RemoveFile}"
                                    CommandParameter="{Binding}"
                                    Classes="Danger"
                                    Margin="2"
                                    IsEnabled="True">
                                <Button.Content>
                                    <TextBlock><Run Text="&#xf1f8;" FontFamily="{StaticResource FontAwesomeSolid}"/> Remove</TextBlock>
                                </Button.Content>
                            </Button>
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate x:DataType="models:ReportFile">
                <TextBlock TextWrapping="Wrap" Margin="4,4,4,0">
                    <Run FontWeight="Bold" Text="File Name"/>: <Run Text="{Binding FileName}"/>
                    <LineBreak/>
                    <Run FontWeight="Bold" Text="Notes"/>: <Run Text="{Binding Notes}"/>
                </TextBlock>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>
    <StackPanel Orientation="Vertical"
                HorizontalAlignment="Center"
                Spacing="4"
                Grid.Row="2"
                Margin="4">
      <Button Content="Create Report PDF"
              Command="{Binding BuildPDF}"
              Classes="accent"
              IsEnabled="{Binding IsCreatePDFButtonEnabled}" />
      <StackPanel Orientation="Horizontal"
                  IsVisible="{Binding IsCreatingPDF}"
                  Spacing="6">
        <Label Content="Creating PDF..."
               IsVisible="{Binding IsCreatingPDF}"
               VerticalAlignment="Center"/>
        <progRing:ProgressRing Width="30" 
                        Height="30"
                        IsActive="{Binding IsCreatingPDF}"
                        Foreground="{DynamicResource SystemAccentColor}"/>
      </StackPanel>
    </StackPanel>
    <StackPanel Orientation="Vertical"
                HorizontalAlignment="Stretch"
                Spacing="2"
                Grid.Row="3">
        <Rectangle Fill="Gray" Height="3" HorizontalAlignment="Stretch"/>
        <Label Content="Program Log" FontSize="14" FontWeight="Bold"/>
    </StackPanel>
    <ScrollViewer Margin="2"
                  Grid.Row="4"
                  x:Name="LogScrollView"
                  VerticalScrollBarVisibility="Visible"
                  AllowAutoHide="False">
        <SelectableTextBlock Text="{Binding CreatePDFLog}"
                            Margin="2"
                            TextWrapping="Wrap"
                            x:Name="LogBlock"/>
    </ScrollViewer>
  </Grid>
</UserControl>
