<Window x:Class="CtrEditor.PopUps.LibraryWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:CtrEditor.Controls"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        xmlns:local="clr-namespace:CtrEditor.PopUps"
        xmlns:ObjetosSim="clr-namespace:CtrEditor.ObjetosSim"
        xmlns:ctr="clr-namespace:CtrEditor"
        Title="Biblioteca de Objetos Simulables" 
        Height="{Binding Source={x:Static ctr:EstadoPersistente.Instance}, Path=LibraryWindow.Height, Mode=TwoWay}"
        Width="{Binding Source={x:Static ctr:EstadoPersistente.Instance}, Path=LibraryWindow.Width, Mode=TwoWay}"
        Left="{Binding Source={x:Static ctr:EstadoPersistente.Instance}, Path=LibraryWindow.Left, Mode=TwoWay}"
        Top="{Binding Source={x:Static ctr:EstadoPersistente.Instance}, Path=LibraryWindow.Top, Mode=TwoWay}"
        WindowStartupLocation="Manual" ResizeMode="CanResize"
        Loaded="Window_Loaded" Closing="Window_Closing">
    
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisConverter"/>
        
        <!-- Estilo para nombres largos con wrapping -->
        <Style x:Key="WrappingTextBlockStyle" TargetType="TextBlock">
            <Setter Property="TextWrapping" Value="Wrap"/>
            <Setter Property="MaxWidth" Value="200"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>

        <!-- Estilo para nombres largos con formato especial -->
        <Style x:Key="WrappingProjectTextBlockStyle" TargetType="TextBlock" BasedOn="{StaticResource WrappingTextBlockStyle}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsCurrentProject}" Value="True">
                    <Setter Property="FontWeight" Value="Bold" />
                    <Setter Property="Foreground" Value="Blue" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
        
        <!-- Menú contextual para directorios -->
        <ContextMenu x:Key="DirectoryContextMenu">
            <MenuItem Header="Nueva Biblioteca" Command="{Binding CreateLibraryInDirectoryCommand}"/>
            <Separator/>
            <MenuItem Header="Eliminar Directorio" Command="{Binding RemoveDirectoryCommand}"/>
        </ContextMenu>
        
        <!-- Estilo para TreeViewItem de directorios -->
        <Style x:Key="DirectoryTreeViewItemStyle" TargetType="TreeViewItem">
            <Setter Property="ContextMenu" Value="{StaticResource DirectoryContextMenu}"/>
        </Style>
    </Window.Resources>
    
    <Window.InputBindings>
        <KeyBinding Key="C" Modifiers="Ctrl" Command="{Binding CopyObjectCommand}" />
        <KeyBinding Key="V" Modifiers="Ctrl" Command="{Binding PasteObjectCommand}" />
        <KeyBinding Key="Delete" Command="{Binding DeleteObjectCommand}" />
    </Window.InputBindings>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="{Binding Source={x:Static ctr:EstadoPersistente.Instance}, Path=LibraryWindow.Column0Width, Mode=TwoWay}" MinWidth="200" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="{Binding Source={x:Static ctr:EstadoPersistente.Instance}, Path=LibraryWindow.Column2Width, Mode=TwoWay}" MinWidth="250" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="{Binding Source={x:Static ctr:EstadoPersistente.Instance}, Path=LibraryWindow.Column4Width, Mode=TwoWay}" MinWidth="250" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="*" MinWidth="300" />
        </Grid.ColumnDefinitions>

        <!-- Primera columna: TreeView jerárquico de directorios y bibliotecas -->
        <Grid Grid.Column="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <Label Grid.Row="0" Content="Bibliotecas" FontWeight="Bold" />
            
            <TreeView Grid.Row="1" Name="LibraryTreeView" 
                      ItemsSource="{Binding LibraryTreeNodes}"
                      SelectedItemChanged="LibraryTreeView_SelectedItemChanged">
                <TreeView.ItemContainerStyleSelector>
                    <local:LibraryTreeItemStyleSelector DirectoryStyle="{StaticResource DirectoryTreeViewItemStyle}"/>
                </TreeView.ItemContainerStyleSelector>
                
                <TreeView.Resources>
                    <!-- Template for directory and library nodes -->
                    <HierarchicalDataTemplate DataType="{x:Type local:LibraryTreeNode}">
                        <HierarchicalDataTemplate.ItemsSource>
                            <Binding Path="Children"/>
                        </HierarchicalDataTemplate.ItemsSource>
                        
                        <StackPanel Orientation="Horizontal" Margin="2">
                            <TextBlock Text="📁" Margin="0,0,5,0" VerticalAlignment="Center" 
                                       Visibility="{Binding IsDirectory, Converter={StaticResource BoolToVisConverter}}"/>
                            <TextBlock Text="📄" Margin="0,0,5,0" VerticalAlignment="Center">
                                <TextBlock.Style>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="Visibility" Value="Collapsed"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsDirectory}" Value="False">
                                                <Setter Property="Visibility" Value="Visible"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                            <TextBlock Text="{Binding DisplayName}" Style="{StaticResource WrappingProjectTextBlockStyle}"/>
                        </StackPanel>
                    </HierarchicalDataTemplate>
                </TreeView.Resources>
            </TreeView>

            <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                <Button Content="Agregar Directorio" Command="{Binding AddLibraryDirectoryCommand}" Margin="2" Padding="5,2" />
            </StackPanel>
        </Grid>

        <!-- GridSplitter 1 -->
        <GridSplitter Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Stretch" 
                      Background="LightGray" Width="5" />

        <!-- Segunda columna: Filtros -->
        <Grid Grid.Column="2">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <Label Grid.Row="0" Content="Filtros" FontWeight="Bold" />
            
            <Expander Grid.Row="1" Header="Configuración de Filtros" IsExpanded="True">
                <controls:osVisFilter x:Name="ObjectFilter" Margin="5"/>
            </Expander>
        </Grid>

        <!-- GridSplitter 2 -->
        <GridSplitter Grid.Column="3" HorizontalAlignment="Center" VerticalAlignment="Stretch" 
                      Background="LightGray" Width="5" />

        <!-- Tercera columna: Objetos con selección múltiple -->
        <Grid Grid.Column="4">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <Label Grid.Row="0" Content="Objetos" FontWeight="Bold" />

            <!-- Controles de selección múltiple -->
            <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="5">
                <Button Content="Seleccionar Todo" Click="SelectAllObjects_Click" Margin="2" Padding="5,2"/>
                <Button Content="Deseleccionar Todo" Click="UnselectAllObjects_Click" Margin="2" Padding="5,2"/>
            </StackPanel>

            <!-- Listbox para objetos con checkboxes -->
            <ListBox Grid.Row="2" Name="SelectableObjectsList" 
                     ItemsSource="{Binding SelectableObjects}"
                     SelectionMode="Extended">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" 
                                      Margin="0,0,5,0" VerticalAlignment="Center"/>
                            <TextBlock Text="{Binding Object.Nombre}" VerticalAlignment="Center">
                                <TextBlock.Style>
                                    <Style TargetType="TextBlock">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Object.Enable_On_All_Pages}" Value="True">
                                                <Setter Property="Foreground" Value="Blue" />
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding Object.Cloned}" Value="True">
                                                <Setter Property="Foreground" Value="Orange" />
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding Object.AutoCreated}" Value="True">
                                                <Setter Property="Foreground" Value="Green" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

            <!-- Botones de acción -->
            <StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                <Button Content="Copiar (Ctrl+C)" Command="{Binding CopyObjectCommand}" Margin="2" Padding="5,2" />
                <Button Content="Pegar (Ctrl+V)" Command="{Binding PasteObjectCommand}" Margin="2" Padding="5,2" />
                <Button Content="Eliminar" Command="{Binding DeleteObjectCommand}" Margin="2" Padding="5,2" />
            </StackPanel>
        </Grid>

        <!-- GridSplitter 3 -->
        <GridSplitter Grid.Column="5" HorizontalAlignment="Center" VerticalAlignment="Stretch" 
                      Background="LightGray" Width="5" />

        <!-- Cuarta columna: Propiedades del objeto seleccionado -->
        <Grid Grid.Column="6">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <Label Grid.Row="0" Content="Propiedades del Objeto" FontWeight="Bold" />
            
            <xctk:PropertyGrid Grid.Row="1" 
                               Name="ObjectPropertyGrid"
                               SelectedObject="{Binding SelectedObject}"
                               IsReadOnly="True"
                               ShowSearchBox="True"
                               ShowSortOptions="True"
                               ShowTitle="False"
                               Margin="5" />

            <!-- Botones de control -->
            <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right" Margin="5">
                <Button Content="Cerrar" IsCancel="True" Click="CloseButton_Click" 
                        Margin="2" Padding="15,5" MinWidth="75" />
            </StackPanel>
        </Grid>
    </Grid>
</Window> 