<UserControl x:Class="NebulaAuth.View.ProxyManagerView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:md="http://materialdesigninxaml.net/winfx/xaml/themes"
             xmlns:other="clr-namespace:NebulaAuth.ViewModel.Other"
             xmlns:nebulaAuth="clr-namespace:NebulaAuth"
             mc:Ignorable="d"
             d:DesignHeight="500" d:DesignWidth="400"
             d:DataContext="{d:DesignInstance other:ProxyManagerVM}"
             d:Background="#141119"
             d:Foreground="White"
             FontFamily="{md:MaterialDesignFont}"
             MinHeight="700"
             MinWidth="600"
             MaxHeight="700"
             MaxWidth="600"
             Background="Transparent"
             FontSize="16">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <nebulaAuth:DialogHeader IconKind="Proxy" Title="{Tr ProxyManagerDialog.Title}" />

        <!-- Tabs -->
        <TabControl Grid.Row="1"
                    md:ColorZoneAssist.Background="{DynamicResource Base100Brush}"
                    Style="{StaticResource MaterialDesignUniformTabControl}">

            <!-- Tab 1: Proxy Manager -->
            <TabItem Header="{Tr ProxyManagerDialog.TabManager}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <!-- Default proxy + display options -->
                    <Grid Grid.Row="0">
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <Grid Margin="15">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <TextBlock HorizontalAlignment="Stretch">
                                <Run Text="{Tr ProxyManagerDialog.DefaultProxy, IsDynamic=False}" />
                                <Run
                                    Text="{Binding DefaultProxy.Key, StringFormat='&#x0a;0:', FallbackValue='-', Mode=OneWay}" />
                                <Run>
                                    <Run.Text>
                                        <MultiBinding Mode="OneWay"
                                                      Converter="{StaticResource ProxyDataTextMultiConverter}">
                                            <Binding Path="DefaultProxy.Value" />
                                            <Binding Path="DataContext.DisplayProtocol"
                                                     RelativeSource="{RelativeSource AncestorType=UserControl}" />
                                            <Binding Path="DataContext.DisplayCredentials"
                                                     RelativeSource="{RelativeSource AncestorType=UserControl}" />
                                        </MultiBinding>
                                    </Run.Text>
                                </Run>
                            </TextBlock>
                            <Button Grid.Column="1" Command="{Binding RemoveDefaultCommand}">
                                <md:PackIcon Kind="ClearBox" Width="20" Height="20" />
                            </Button>
                        </Grid>
                        <StackPanel Grid.Row="1" Margin="15,0,15,15">
                            <CheckBox Content="{Tr ProxyManagerDialog.DisplayProxyProtocol}"
                                      IsChecked="{Binding DisplayProtocol}" />
                            <CheckBox Content="{Tr ProxyManagerDialog.DisplayProxyCredentials}"
                                      IsChecked="{Binding DisplayCredentials}" />
                        </StackPanel>
                    </Grid>

                    <!-- Proxy list -->
                    <md:Card Grid.Row="1" Margin="10">
                        <ListBox VirtualizingStackPanel.VirtualizationMode="Recycling" FontSize="14"
                                 SelectedValue="{Binding SelectedProxy}" ItemsSource="{Binding Proxies}">
                            <ListBox.InputBindings>
                                <KeyBinding Key="Delete"
                                            Command="{Binding DataContext.RemoveProxyCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" />
                            </ListBox.InputBindings>
                            <ListBox.ItemContainerStyle>
                                <Style TargetType="ListBoxItem" BasedOn="{StaticResource MaterialDesignListBoxItem}">
                                    <Setter Property="Tag"
                                            Value="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext}" />
                                    <Setter Property="ContextMenu">
                                        <Setter.Value>
                                            <ContextMenu>
                                                <MenuItem Header="{Tr MainWindow.ContextMenus.Proxy.Copy}"
                                                          Command="{Binding PlacementTarget.Tag.CopyProxyCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                                                          CommandParameter="{Binding Value}" />
                                                <MenuItem Header="{Tr MainWindow.ContextMenus.Proxy.CopyAddress}"
                                                          Command="{Binding PlacementTarget.Tag.CopyProxyAddressCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                                                          CommandParameter="{Binding Value}" />
                                                <MenuItem Header="{Tr Common.Delete}"
                                                          Command="{Binding PlacementTarget.Tag.RemoveProxyCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                                                          CommandParameter="{Binding Value}" />
                                            </ContextMenu>
                                        </Setter.Value>
                                    </Setter>
                                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                                </Style>
                            </ListBox.ItemContainerStyle>
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="Auto" />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock VerticalAlignment="Center">
                                            <Run Text="{Binding Key, Mode=OneWay}" /><Run Text=": " />
                                            <Run>
                                                <Run.Text>
                                                    <MultiBinding Mode="OneWay"
                                                                  Converter="{StaticResource ProxyDataTextMultiConverter}">
                                                        <Binding Path="Value" />
                                                        <Binding Path="DataContext.DisplayProtocol"
                                                                 RelativeSource="{RelativeSource AncestorType=UserControl}" />
                                                        <Binding Path="DataContext.DisplayCredentials"
                                                                 RelativeSource="{RelativeSource AncestorType=UserControl}" />
                                                    </MultiBinding>
                                                </Run.Text>
                                            </Run>
                                        </TextBlock>

                                        <TextBlock Grid.Column="1" VerticalAlignment="Center" FontSize="10"
                                                   Margin="0,0,2,0"
                                                   Foreground="{DynamicResource MaterialDesign.Brush.ForegroundLight}"
                                                   Text="{Binding Key, Converter={StaticResource ProxyAccountCountConverter}}" />
                                        <Button Style="{StaticResource MaterialDesignIconButton}" Padding="0"
                                                Width="24"
                                                Height="24" md:RippleAssist.IsDisabled="True" Grid.Column="2"
                                                HorizontalAlignment="Right"
                                                VerticalAlignment="Center"
                                                Command="{Binding DataContext.SetDefaultCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
                                                CommandParameter="{Binding}">
                                            <md:PackIcon Height="16" Width="16" Kind="Heart" />
                                        </Button>
                                    </Grid>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </md:Card>

                    <!-- Add / Remove area -->
                    <Grid Grid.Row="2" Margin="5,0,15,0">
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <nebulaAuth:HintBox
                            Visibility="{Binding ErrorText, Converter={StaticResource NullableToVisibilityConverter}}"
                            Margin="15,0,15,0"
                            Grid.Row="0"
                            Severity="Error"
                            FontSize="14"
                            Text="{Binding ErrorText}"
                            CloseCommand="{Binding ClearErrorCommand}"
                            ShowCloseButton="True" />
                        <Grid Grid.Row="1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <TextBox Text="{Binding AddProxyField}"
                                     KeyDown="ProxyInput_KeyDown"
                                     FontSize="12"
                                     md:TextFieldAssist.HasClearButton="True"
                                     AcceptsReturn="True"
                                     MaxHeight="100" Style="{StaticResource MaterialDesignOutlinedTextBox}"
                                     Margin="15"
                                     Height="90"
                                     md:HintAssist.IsFloating="False"
                                     md:HintAssist.Hint="IP:PORT&#x0a;IP:PORT:USER:PASS&#x0a;PROTOCOL://IP:PORT:USER:PASS&#x0a;IP:PORT:USER:PASS{ID}
                     " />
                            <Button x:Name="AddProxyBtn" ToolTip="CTRL+ENTER" IsDefault="True" Grid.Column="1"
                                    Height="90" Command="{Binding AddProxyCommand}">
                                <md:PackIcon Kind="Add" />
                            </Button>
                            <Button Grid.Column="2" Height="90" Command="{Binding RemoveProxyCommand}"
                                    CommandParameter="{x:Null}">
                                <md:PackIcon Kind="Trash" />
                            </Button>
                        </Grid>
                    </Grid>
                </Grid>
            </TabItem>

            <!-- Tab 2: Bulk Assignment -->
            <TabItem Header="{Tr ProxyManagerDialog.TabAssignment}">
                <Grid Margin="12">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <!-- Stats counters -->
                    <UniformGrid Grid.Row="0" Columns="3" Margin="0,8,0,12">
                        <Border Margin="0,0,4,0" Padding="10,8" CornerRadius="8"
                                Background="{DynamicResource MaterialDesign.Brush.Card.Background}">
                            <StackPanel HorizontalAlignment="Center">
                                <TextBlock FontSize="20" FontWeight="Bold" HorizontalAlignment="Center"
                                           Text="{Binding TotalAccounts, Mode=OneWay}" />
                                <TextBlock FontSize="11" HorizontalAlignment="Center"
                                           Text="{Tr ProxyManagerDialog.TotalAccounts}"
                                           Foreground="{DynamicResource MaterialDesign.Brush.ForegroundLight}" />
                            </StackPanel>
                        </Border>
                        <Border Margin="4,0,4,0" Padding="10,8" CornerRadius="8"
                                Background="{DynamicResource MaterialDesign.Brush.Card.Background}">
                            <StackPanel HorizontalAlignment="Center">
                                <TextBlock FontSize="20" FontWeight="Bold" HorizontalAlignment="Center"
                                           Text="{Binding TotalProxies, Mode=OneWay}" />
                                <TextBlock FontSize="11" HorizontalAlignment="Center"
                                           Text="{Tr ProxyManagerDialog.TotalProxies}"
                                           Foreground="{DynamicResource MaterialDesign.Brush.ForegroundLight}" />
                            </StackPanel>
                        </Border>
                        <Border Margin="4,0,0,0" Padding="10,8" CornerRadius="8"
                                Background="{DynamicResource MaterialDesign.Brush.Card.Background}">
                            <StackPanel HorizontalAlignment="Center">
                                <TextBlock FontSize="20" FontWeight="Bold" HorizontalAlignment="Center"
                                           Text="{Binding AssignedProxies, Mode=OneWay}" />
                                <TextBlock FontSize="11" HorizontalAlignment="Center"
                                           Text="{Tr ProxyManagerDialog.AssignedProxies}"
                                           Foreground="{DynamicResource MaterialDesign.Brush.ForegroundLight}" />
                            </StackPanel>
                        </Border>
                    </UniformGrid>

                    <!-- Toggle: behaviour for unspecified proxy -->
                    <StackPanel Grid.Row="1" Margin="0,0,0,10">
                        <TextBlock Text="{Tr ProxyManagerDialog.UnspecifiedBehavior}" FontSize="13"
                                   Margin="0,0,0,4"
                                   Foreground="{DynamicResource MaterialDesign.Brush.ForegroundLight}" />
                        <StackPanel Orientation="Horizontal">
                            <RadioButton Content="{Tr ProxyManagerDialog.UnspecifiedRandom}"
                                         IsChecked="{Binding UnspecifiedAssignRandom}"
                                         Margin="0,0,24,0" />
                            <RadioButton Content="{Tr ProxyManagerDialog.UnspecifiedRemove}"
                                         IsChecked="{Binding UnspecifiedAssignRandom, Converter={StaticResource ReverseBooleanConverter}}" />
                        </StackPanel>
                    </StackPanel>

                    <!-- Input -->
                    <TextBox Grid.Row="2"
                             Text="{Binding AssignmentInput, UpdateSourceTrigger=PropertyChanged}"
                             FontSize="12"
                             md:TextFieldAssist.HasClearButton="True"
                             AcceptsReturn="True"
                             Style="{StaticResource MaterialDesignOutlinedTextBox}"
                             VerticalScrollBarVisibility="Auto"
                             md:HintAssist.IsFloating="False"
                             VerticalContentAlignment="Top"
                             Cursor="IBeam"

                             md:HintAssist.Hint="login:IP:PORT&#x0a;login:IP:PORT:USER:PASS&#x0a;login:socks5://USER:PASS@IP:PORT&#x0a;login:ID&#x0a;login" />

                    <!-- Result HintBox -->
                    <nebulaAuth:HintBox Grid.Row="3"
                                        Visibility="{Binding AssignmentTip, Converter={StaticResource NullableToVisibilityConverter}}"
                                        Margin="0,8,0,0"
                                        Severity="Info"
                                        FontSize="14"
                                        Text="{Binding AssignmentTip}"
                                        CloseCommand="{Binding ClearAssignmentTipCommand}"
                                        ShowCloseButton="True" />

                    <!-- Apply button -->
                    <Button Grid.Row="4" Margin="0,10,0,0" HorizontalAlignment="Right"
                            Command="{Binding ApplyAssignmentCommand}"
                            Content="{Tr ProxyManagerDialog.ApplyAssignment}" />


                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
</UserControl>