<UserControl x:Class="AppTunnel.Views.AppsTabView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid Margin="0,8,0,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="12"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <!-- LEFT: Selected Apps (Tunnel List) -->
        <Border Grid.Column="0" Style="{StaticResource CardPanel}" Margin="0">
            <DockPanel>
                <Grid DockPanel.Dock="Top" Margin="0,0,0,8">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <StackPanel Grid.Column="0">
                        <TextBlock Text="✅ برنامه‌های تونل" FontSize="14" FontWeight="SemiBold"
                                   Foreground="{StaticResource TextPrimaryBrush}"/>
                        <TextBlock Text="ترافیک این برنامه‌ها از VPN عبور می‌کند"
                                   Foreground="{StaticResource TextSecondaryBrush}"
                                   FontSize="10" Margin="0,2,0,0"/>
                    </StackPanel>
                    <Button Grid.Column="1" Style="{StaticResource SecondaryButton}"
                            Content="📁 افزودن"
                            ToolTip="افزودن دستی فایل exe"
                            Command="{Binding AddAppCommand}"
                            FontSize="11" Padding="10,6"/>
                </Grid>

                <!-- Tunnel Search -->
                <TextBox x:Name="TunnelSearchBox"
                         Style="{StaticResource ModernTextBox}"
                         Text="{Binding TunnelSearchText, UpdateSourceTrigger=PropertyChanged}"
                         Tag="فیلتر برنامه‌های تونل..."
                         DockPanel.Dock="Top"
                         Margin="0,0,0,8"
                         FontSize="12" Padding="10,7"
                         PreviewMouseDoubleClick="OnSearchBoxPreviewMouseDoubleClick"/>

                <!-- Tunnel Apps List -->
                <ListView ItemsSource="{Binding FilteredTunnelApps}"
                          SelectedItem="{Binding SelectedApp}"
                          Background="Transparent"
                          BorderThickness="0"
                          FlowDirection="LeftToRight"
                          Cursor="Arrow"
                          ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Border Background="#11FFFFFF" CornerRadius="8"
                                    Padding="10,8" Margin="0,2" Cursor="Arrow">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>

                                    <!-- App Icon -->
                                    <Image Grid.Column="0" Source="{Binding Icon}"
                                           Width="28" Height="28" Margin="0,0,10,0"/>

                                    <!-- App Name & Exe -->
                                    <StackPanel Grid.Column="1" VerticalAlignment="Center">
                                        <TextBlock Text="{Binding DisplayName}"
                                                   Foreground="{StaticResource TextPrimaryBrush}"
                                                   FontSize="13" TextTrimming="CharacterEllipsis"/>
                                        <TextBlock Text="{Binding ExecutableName}"
                                                   Foreground="{StaticResource TextSecondaryBrush}"
                                                   FontSize="10" FlowDirection="LeftToRight"/>
                                    </StackPanel>

                                    <!-- Traffic -->
                                    <TextBlock Grid.Column="2" Text="{Binding TrafficDisplay}"
                                               Foreground="{StaticResource AccentBrush}"
                                               FontSize="11" VerticalAlignment="Center"
                                               Margin="8,0" FlowDirection="LeftToRight"/>

                                    <!-- Toggle -->
                                    <CheckBox Grid.Column="3" Style="{StaticResource ToggleSwitch}"
                                              IsChecked="{Binding IsEnabled, Mode=OneWay}"
                                              Command="{Binding DataContext.ToggleAppCommand,
                                                  RelativeSource={RelativeSource AncestorType=UserControl}}"
                                              CommandParameter="{Binding}"
                                              VerticalAlignment="Center" Margin="4,0"/>

                                    <!-- Remove -->
                                    <Button Grid.Column="4" Style="{StaticResource DangerButton}"
                                            Content="✕" FontSize="14"
                                            Command="{Binding DataContext.RemoveAppCommand,
                                                RelativeSource={RelativeSource AncestorType=UserControl}}"
                                            CommandParameter="{Binding}"/>
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                            <Setter Property="Padding" Value="0"/>
                            <Setter Property="Margin" Value="0"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ListViewItem">
                                        <ContentPresenter/>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ListView.ItemContainerStyle>
                </ListView>
            </DockPanel>
        </Border>

        <!-- RIGHT: Available Apps (Discovery) -->
        <Border Grid.Column="2" Style="{StaticResource CardPanel}" Margin="0">
            <DockPanel>
                <Grid DockPanel.Dock="Top" Margin="0,0,0,8">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <StackPanel Grid.Column="0">
                        <TextBlock Text="🔍 برنامه‌های نصب شده" FontSize="14" FontWeight="SemiBold"
                                   Foreground="{StaticResource TextPrimaryBrush}"/>
                        <TextBlock Text="کلیک کنید تا به تونل اضافه شود"
                                   Foreground="{StaticResource TextSecondaryBrush}"
                                   FontSize="10" Margin="0,2,0,0"/>
                    </StackPanel>
                    <Button Grid.Column="1" Style="{StaticResource SecondaryButton}"
                            Content="🔄 بارگذاری"
                            ToolTip="بارگذاری مجدد لیست برنامه‌ها"
                            Command="{Binding RefreshAppsCommand}"
                            FontSize="11" Padding="10,6"/>
                </Grid>
                <TextBox x:Name="AvailableSearchBox"
                         Style="{StaticResource ModernTextBox}"
                         Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}"
                         Tag="جستجوی برنامه..."
                         DockPanel.Dock="Top"
                         Margin="0,0,0,8"
                         PreviewMouseDoubleClick="OnSearchBoxPreviewMouseDoubleClick"/>

                <!-- Available Apps List -->
                <ListView ItemsSource="{Binding FilteredAvailableApps}"
                          Background="Transparent"
                          BorderThickness="0"
                          FlowDirection="LeftToRight"
                          ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Border x:Name="itemBorder"
                                    Background="#08FFFFFF" CornerRadius="6"
                                    Padding="8,6" Margin="0,1" Cursor="Hand"
                                    MouseLeftButtonUp="OnAvailableAppClick">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>

                                    <Image Grid.Column="0" Source="{Binding Icon}"
                                           Width="24" Height="24" Margin="0,0,8,0"/>

                                    <StackPanel Grid.Column="1" VerticalAlignment="Center">
                                        <TextBlock Text="{Binding DisplayName}"
                                                   Foreground="{StaticResource TextPrimaryBrush}"
                                                   FontSize="12" TextTrimming="CharacterEllipsis"/>
                                        <TextBlock Text="{Binding ExecutableName}"
                                                   Foreground="{StaticResource TextSecondaryBrush}"
                                                   FontSize="9" FlowDirection="LeftToRight"/>
                                    </StackPanel>

                                    <Border Grid.Column="2" x:Name="addBadge"
                                            Background="#25E07820" CornerRadius="4"
                                            Padding="6,2" Margin="4,0,0,0">
                                        <TextBlock Text="＋" FontSize="11"
                                                   Foreground="{StaticResource PrimaryBrush}"
                                                   VerticalAlignment="Center"/>
                                    </Border>
                                </Grid>
                            </Border>
                            <DataTemplate.Triggers>
                                <Trigger SourceName="itemBorder" Property="IsMouseOver" Value="True">
                                    <Setter TargetName="itemBorder" Property="Background" Value="#18FFFFFF"/>
                                    <Setter TargetName="addBadge" Property="Background" Value="#40E07820"/>
                                </Trigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                            <Setter Property="Padding" Value="0"/>
                            <Setter Property="Margin" Value="0"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ListViewItem">
                                        <ContentPresenter/>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ListView.ItemContainerStyle>
                </ListView>
            </DockPanel>
        </Border>
    </Grid>
</UserControl>
