<Window xmlns:ctreditor="clr-namespace:CtrEditor" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Siemens="clr-namespace:LibS7Adv;assembly=LibS7Adv" xmlns:local="clr-namespace:CtrEditor"
        xmlns:uc="clr-namespace:CtrEditor.ObjetosSim.UserControls"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:ObjetosSim="clr-namespace:CtrEditor.ObjetosSim"
        xmlns:ObjetosExtraccion="clr-namespace:CtrEditor.ObjetosSim.Extraccion_Datos" x:Class="CtrEditor.MainWindow"
        Height="900" Width="1600" WindowState="Maximized" ResizeMode="CanResize" Title="{Binding directorioTrabajo, Converter={StaticResource UnsavedChangesConverter}}"
        Icon="/app2.png">

    <Window.DataContext>
        <ctreditor:MainViewModel />
    </Window.DataContext>

    <Window.Resources>

        <!-- Style for Start/Stop Button -->
        <Style x:Key="StartStopButtonStyle" TargetType="Button">
            <Setter Property="Background" Value="Transparent" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSimulationRunning}" Value="True">
                    <Setter Property="Background" Value="LightGreen" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <!-- Style for Connect/Disconnect Button -->
        <Style x:Key="ConnectDisconnectButtonStyle" TargetType="Button">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="Padding" Value="5" />
            <Setter Property="MinWidth" Value="80" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsConnected}" Value="True">
                    <Setter Property="Background" Value="#90EE90" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <!-- Converter for PLC Connect/Disconnect button text -->
        <local:ConnectStateToBtnTextConverter x:Key="ConnectStateToBtnTextConverter" />

        <!-- Converter for PLC Connect/Disconnect image -->
        <local:ConnectStateToImageConverter x:Key="ConnectStateToImageConverter" />

    </Window.Resources>

    <Grid>
        <!-- Menú Principal sobre toda la ventana -->
        <Menu VerticalAlignment="Top" HorizontalAlignment="Stretch">
            <MenuItem Header="Projecto">
                <MenuItem Header="Abrir Directorio de trabajo" Command="{Binding OpenWorkDirectoryCommand}" />
                <MenuItem Header="Ultimos Directorios Utilizados" ItemsSource="{Binding RecentDirectories}">
                    <MenuItem.ItemTemplate>
                        <DataTemplate>
                            <MenuItem Header="{Binding}" Command="{Binding DataContext.OpenRecentDirectoryCommand, RelativeSource={RelativeSource AncestorType=MenuItem, AncestorLevel=2}}" 
                                      CommandParameter="{Binding}"/>
                        </DataTemplate>
                    </MenuItem.ItemTemplate>
                </MenuItem>
                <MenuItem Header="Iniciar Simulacion" Command="{Binding StartSimulationCommand}" />
                <MenuItem Header="Detenet Simulacion" Command="{Binding StopSimulationCommand}" />
                <MenuItem Header="Debug Window" Command="{Binding DebugWindowCommand}" />
                <MenuItem Header="Guardar" Command="{Binding SaveCommand}" />
                <MenuItem Header="Salir" Command="{Binding ExitCommand}" />
            </MenuItem>
        </Menu>

        <Grid Margin="0,20,0,0">
            <!-- Margen superior para el menú -->
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*" MinWidth="200" />
                <ColumnDefinition Width="8*" MinWidth="200" />
                <ColumnDefinition Width="2*" MinWidth="100" />
            </Grid.ColumnDefinitions>

            <!-- Primera Columna -->
            <Grid Grid.Column="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="2*" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <ListBox x:Name="ListaImagenes" Grid.Row="0" Margin="5" ItemsSource="{Binding ListaImagenes}"
                        SelectedItem="{Binding SelectedImage}" />
                <ListBox x:Name="ListaFunciones" Grid.Row="1" Margin="5" ItemsSource="{Binding ListaOsBase}"
                        DisplayMemberPath="Nombre" SelectedItem="{Binding SelectedItem}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDoubleClick">
                            <i:InvokeCommandAction Command="{Binding ItemDoubleClickCommand}"
                                    CommandParameter="{Binding SelectedItem}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </ListBox>
                <Siemens:PLCControl x:Name="PLCSim" Grid.Row="2" Margin="5" DataContext="{Binding PLCViewModel}" />
            </Grid>
            <!-- GridSplitter -->
            <GridSplitter Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right" Width="5" Background="LightGray" />

            <!-- Segunda Columna -->
            <Grid Grid.Column="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <ToolBarTray Grid.Row="0">
                    <ToolBar>
                        <Button Command="{Binding TBStartSimulationCommand}" ToolTip="Iniciar Simulación"
                                Style="{StaticResource StartStopButtonStyle}">
                            <StackPanel>
                                <Image Source="Icons/start.png" Width="24" Height="24" />
                                <TextBlock Text="Iniciar" />
                            </StackPanel>
                        </Button>
                        <Button Command="{Binding TBStopSimulationCommand}" ToolTip="Detener Simulación">
                            <StackPanel>
                                <Image Source="Icons/stop.png" Width="24" Height="24" />
                                <TextBlock Text="Detener" />
                            </StackPanel>
                        </Button>
                        <Button Command="{Binding TBSaveCommand}" ToolTip="Guardar">
                            <StackPanel>
                                <Image Source="Icons/save.png" Width="24" Height="24" />
                                <TextBlock Text="Guardar" />
                            </StackPanel>
                        </Button>
                        <Button Command="{Binding TBTogglePLCConnectionCommand}"
                                ToolTip="{Binding IsConnected, Converter={StaticResource ConnectStateToBtnTextConverter}}"
                                Style="{StaticResource ConnectDisconnectButtonStyle}">
                            <StackPanel>
                                <Image
                                        Source="{Binding IsConnected, Converter={StaticResource ConnectStateToImageConverter}}"
                                        Width="24" Height="24" />
                                <TextBlock
                                        Text="{Binding IsConnected, Converter={StaticResource ConnectStateToBtnTextConverter}}" />
                            </StackPanel>
                        </Button>
                        <Button Command="{Binding TBMultiPageAnalizeCommand}"
                                ToolTip="Analyze Tags in multiple pages.">
                            <StackPanel>
                                <Image Source="Icons/match.png" Width="24" Height="24" />
                                <TextBlock Text="Multi Page Analyze" />
                            </StackPanel>
                        </Button>

                        <Button Command="{Binding TBMultiPageMatrixCommand}" ToolTip="Analyze Matrix (Multiple Pages)">
                            <StackPanel>
                                <Image Source="Icons/ocr.png" Width="24" Height="24" />
                                <TextBlock Text="Exportar Tags a Excel" />
                            </StackPanel>
                        </Button>

                    </ToolBar>
                </ToolBarTray>

                <ScrollViewer Grid.Row="1" x:Name="ImagenEnTrabajoScrollViewer" HorizontalScrollBarVisibility="Auto"
                        VerticalScrollBarVisibility="Auto" PanningMode="Both">
                    <Canvas x:Name="ImagenEnTrabajoCanvas" Margin="0" Background="Transparent">
                        <!-- Agregar Background="Transparent" para que capture los eventos del mouse y -->
                        <!-- asegurar que el Canvas reciba los eventos del botón derecho -->
                        <Canvas.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform x:Name="CanvasScaleTransform" ScaleX="1" ScaleY="1" />
                                <TranslateTransform x:Name="CanvasTranslateTransform" />
                            </TransformGroup>
                        </Canvas.RenderTransform>
                    </Canvas>
                </ScrollViewer>
            </Grid>

            <!-- GridSplitter -->
            <GridSplitter Grid.Column="1" Grid.Row="0" HorizontalAlignment="Right" Width="5" Background="LightGray" />

            <!-- Tercera Columna -->
            <Grid Grid.Column="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" MinHeight="50" />
                    <RowDefinition Height="*" MinHeight="100" />
                    <!-- ListBox1 -->
                    <RowDefinition Height="Auto" />
                    <!-- GridSplitter -->
                    <RowDefinition Height="*" MinHeight="200" />
                    <!-- StackPanel -->
                    <RowDefinition Height="Auto" />
                    <!-- ToolBarTray -->
                </Grid.RowDefinitions>

                <ToolBarTray Grid.Row="0">
                    <ToolBar>
                        <Button Command="{Binding TBEliminarTodosCommand}" ToolTip="Eliminar Todos">
                            <StackPanel>
                                <Image Source="Icons/borrar.png" Width="16" Height="16" />
                                <TextBlock Text="All Objects" />
                            </StackPanel>
                        </Button>
                        <Button Command="{Binding TBEliminarAutoCreatedCommand}" ToolTip="Eliminar Auto">
                            <StackPanel>
                                <Image Source="Icons/borrar.png" Width="16" Height="16" />
                                <TextBlock Text="Auto Created" />
                            </StackPanel>
                        </Button>
                        <Button Command="{Binding TBEliminarClonedCommand}" ToolTip="Eliminar Cloned for tag Extraction">
                            <StackPanel>
                                <Image Source="Icons/borrar.png" Width="16" Height="16" />
                                <TextBlock Text="Cloned" />
                            </StackPanel>
                        </Button>
                        <Button Command="{Binding TBAssingPagesCommand}" ToolTip="Assing Pages">
                            <StackPanel>
                                <Image Source="Icons/choose.png" Width="16" Height="16" />
                                <TextBlock Text="Assing Pages"  />
                            </StackPanel>
                        </Button>

                    </ToolBar>
                </ToolBarTray>
                <xctk:PropertyGrid Grid.Row="1" x:Name="PanelFilter" AutoGenerateProperties="True"
                        ShowDescriptionByTooltip="False" ShowSearchBox="False" ShowSortOptions="False"
                        ShowSummary="False" ShowTitle="False" SelectedObject="{Binding OsListFilter}">
                </xctk:PropertyGrid>
                <!-- ListBox -->
                <ListBox x:Name="ListaOs" Grid.Row="2" Margin="5" ItemsSource="{Binding VistaFiltrada}"
                        SelectedItem="{Binding SelectedItemOsList, Mode=TwoWay}"
                        SelectionChanged="ListaOs_SelectionChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Nombre}">
                                <TextBlock.Style>
                                    <Style TargetType="TextBlock">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Path=Enable_On_All_Pages}" Value="True">
                                                <Setter Property="Foreground" Value="Blue" />
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding Path=Enable_On_All_Pages}" Value="False">
                                                <Setter Property="Foreground" Value="Black" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

                <!-- GridSplitter -->
                <GridSplitter Grid.Row="3" Height="5" HorizontalAlignment="Stretch" Background="Gray"
                        ResizeDirection="Rows" VerticalAlignment="Center" />

                <!-- PanelEdicion -->
                <xctk:PropertyGrid Grid.Row="4" Margin="5" x:Name="PanelEdicion" AutoGenerateProperties="False"
                        ShowDescriptionByTooltip="True">
                    <xctk:PropertyGrid.EditorDefinitions>

                        <!-- String -->
                        <xctk:EditorTemplateDefinition>
                            <xctk:EditorTemplateDefinition.TargetProperties>
                                <xctk:TargetPropertyType Type="{x:Type sys:String}" />
                            </xctk:EditorTemplateDefinition.TargetProperties>
                            <xctk:EditorTemplateDefinition.EditingTemplate>
                                <DataTemplate>
                                    <TextBox Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
                                            Text="{Binding Value}" />
                                </DataTemplate>
                            </xctk:EditorTemplateDefinition.EditingTemplate>
                        </xctk:EditorTemplateDefinition>

                        <!-- Velocidad -->
                        <xctk:EditorTemplateDefinition TargetProperties="VelocidadActual">
                            <xctk:EditorTemplateDefinition.EditingTemplate>
                                <DataTemplate>
                                    <xctk:SingleUpDown Increment="0.01" Background="Beige"
                                            Text="{Binding Value, Converter={StaticResource floatFormatter}}"
                                            FontWeight="Bold" />
                                </DataTemplate>
                            </xctk:EditorTemplateDefinition.EditingTemplate>
                        </xctk:EditorTemplateDefinition>

                        <!-- Float -->
                        <xctk:EditorTemplateDefinition>
                            <xctk:EditorTemplateDefinition.TargetProperties>
                                <xctk:TargetPropertyType Type="{x:Type sys:Single}" />
                            </xctk:EditorTemplateDefinition.TargetProperties>
                            <xctk:EditorTemplateDefinition.EditingTemplate>
                                <DataTemplate>
                                    <xctk:SingleUpDown Increment="0.01"
                                            Text="{Binding Value, Converter={StaticResource floatFormatter}}" />
                                </DataTemplate>
                            </xctk:EditorTemplateDefinition.EditingTemplate>
                        </xctk:EditorTemplateDefinition>

                        <!-- Double -->
                        <xctk:EditorTemplateDefinition>
                            <xctk:EditorTemplateDefinition.TargetProperties>
                                <xctk:TargetPropertyType Type="{x:Type sys:Double}" />
                            </xctk:EditorTemplateDefinition.TargetProperties>
                            <xctk:EditorTemplateDefinition.EditingTemplate>
                                <DataTemplate>
                                    <TextBox Text="{Binding Value, Converter={StaticResource doubleFormatter}}" />
                                </DataTemplate>
                            </xctk:EditorTemplateDefinition.EditingTemplate>
                        </xctk:EditorTemplateDefinition>

                        <!-- osBuscarCoincidencias -->
                        <xctk:EditorTemplateDefinition>
                            <xctk:EditorTemplateDefinition.TargetProperties>
                                <xctk:TargetPropertyType Type="{x:Type ObjetosExtraccion:osBuscarCoincidencias}" />
                            </xctk:EditorTemplateDefinition.TargetProperties>
                            <xctk:EditorTemplateDefinition.EditingTemplate>
                                <DataTemplate>
                                    <ComboBox ItemsSource="{Binding DataContext.ObjetosSimulables, 
                                               RelativeSource={RelativeSource AncestorType=Window}, 
                                               Converter={StaticResource SubclassFilterConverterosBuscarCoincidencias}}"
                                            SelectedItem="{Binding Value}" DisplayMemberPath="Nombre" IsEditable="True" />
                                </DataTemplate>
                            </xctk:EditorTemplateDefinition.EditingTemplate>
                        </xctk:EditorTemplateDefinition>

                        <!-- osVMmotorSim -->
                        <xctk:EditorTemplateDefinition>
                            <xctk:EditorTemplateDefinition.TargetProperties>
                                <xctk:TargetPropertyType Type="{x:Type ObjetosSim:osVMmotorSim}" />
                            </xctk:EditorTemplateDefinition.TargetProperties>
                            <xctk:EditorTemplateDefinition.EditingTemplate>
                                <DataTemplate>
                                    <ComboBox ItemsSource="{Binding DataContext.ObjetosSimulables, 
                                               RelativeSource={RelativeSource AncestorType=Window}, 
                                               Converter={StaticResource SubclassFilterConverterosVMMotor}}"
                                            SelectedItem="{Binding Value}" DisplayMemberPath="Nombre" />
                                </DataTemplate>
                            </xctk:EditorTemplateDefinition.EditingTemplate>
                        </xctk:EditorTemplateDefinition>

                       <!-- osBase -->
                        <xctk:EditorTemplateDefinition>
                            <xctk:EditorTemplateDefinition.TargetProperties>
                                <xctk:TargetPropertyType Type="{x:Type ObjetosSim:osBase}" />
                            </xctk:EditorTemplateDefinition.TargetProperties>
                            <xctk:EditorTemplateDefinition.EditingTemplate>
                                <DataTemplate>
                                    <ComboBox
                                            ItemsSource="{Binding DataContext.ObjetosSimulables, RelativeSource={RelativeSource AncestorType=Window}}"
                                            SelectedItem="{Binding Value}" DisplayMemberPath="Nombre" />
                                </DataTemplate>
                            </xctk:EditorTemplateDefinition.EditingTemplate>
                        </xctk:EditorTemplateDefinition>

                    </xctk:PropertyGrid.EditorDefinitions>
                </xctk:PropertyGrid>

                <ToolBarTray Grid.Row="5">
                    <ToolBar>
                        <Button Command="{Binding TBEliminarUserControlCommand}" ToolTip="Eliminar Control">
                            <StackPanel>
                                <Image Source="Icons/borrar.png" Width="16" Height="16" />
                                <TextBlock Text="Eliminar" />
                            </StackPanel>
                        </Button>
                        <Button Command="{Binding TBDuplicarUserControlCommand}" ToolTip="Duplicar Control">
                            <StackPanel>
                                <Image Source="Icons/duplicate.png" Width="16" Height="16" />
                                <TextBlock Text="Duplicar" />
                            </StackPanel>
                        </Button>
                    </ToolBar>
                </ToolBarTray>

            </Grid>

        </Grid>
    </Grid>
</Window>
