<UserControl x:Class="ImageResizer.Views.InputPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:ImageResizer.Views"
             xmlns:m="clr-namespace:ImageResizer.Models"
             xmlns:p="clr-namespace:ImageResizer.Properties"
             xmlns:ui="http://schemas.modernwpf.com/2019"
             MinWidth="350">
    <UserControl.Resources>
        <Style x:Key="DisabledWhenUnselectedComboBoxStyle" TargetType="ComboBox" BasedOn="{StaticResource DefaultComboBoxStyle}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Value="True">
                    <Setter Property="IsEnabled" Value="True"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Value="False">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <Style x:Key="DisabledWhenUnselectedTextBoxStyle" TargetType="TextBox" BasedOn="{StaticResource DefaultTextBoxStyle}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Value="True">
                    <Setter Property="IsEnabled" Value="True"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Value="False">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>
    
    
    
    <StackPanel Background="{DynamicResource PrimaryBackgroundBrush}">
        <Label Margin="12,12,12,0"
               FontSize="16"
               Content="{x:Static p:Resources.Input_Content}"
               Target="{Binding ElementName=_selectedSizeListBox}"/>
        
        <ListBox Margin="12,24,12,0"
                 BorderThickness="0"
                 Background="Transparent"
                 AutomationProperties.Name="Sizes listbox"
                 ItemsSource="{Binding Settings.AllSizes}"
                 Name="_selectedSizeListBox"
                 SelectedIndex="{Binding Settings.SelectedSizeIndex}">
            
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <RadioButton Margin="0,4,0,0"
                                             VerticalAlignment="Center"
                                             VerticalContentAlignment="Top"
                                             AutomationProperties.Name="{Binding Name}"
                                             Focusable="False"
                                             IsChecked="{Binding IsSelected,RelativeSource={RelativeSource TemplatedParent}}">
                                    <ContentPresenter/>
                                </RadioButton>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="AutomationProperties.Name" Value="{Binding Name}"/>
                </Style>
            </ListBox.ItemContainerStyle>
            
            <ListBox.Resources>
                <DataTemplate DataType="{x:Type m:ResizeSize}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}" FontWeight="SemiBold" Foreground="{DynamicResource PrimaryForegroundBrush}"/>
                        <TextBlock Text="(" Margin="4,0,0,0" Foreground="{DynamicResource SecondaryForegroundBrush}"/>
                        <TextBlock Text="{Binding Fit,Converter={StaticResource EnumValueConverter},ConverterParameter=ThirdPersonSingular}" Foreground="{DynamicResource SecondaryForegroundBrush}"/>
                        <TextBlock Text="{Binding Width,Converter={StaticResource AutoDoubleConverter},ConverterParameter=Auto}" Margin="4,0,0,0" Foreground="{DynamicResource SecondaryForegroundBrush}"/>
                        <TextBlock Text="&#xE711;" FontSize="11" FontFamily="Segoe MDL2 Assets" Visibility="{Binding ShowHeight,Converter={StaticResource BoolValueConverter}}" Margin="4,5,0,0" Foreground="{DynamicResource SecondaryForegroundBrush}"/>
                        <TextBlock Text="{Binding Height,Converter={StaticResource AutoDoubleConverter},ConverterParameter=Auto}" Visibility="{Binding ShowHeight,Converter={StaticResource BoolValueConverter}}" Margin="4,0,0,0" Foreground="{DynamicResource SecondaryForegroundBrush}"/>
                        <TextBlock Text="{Binding Unit,Converter={StaticResource EnumValueConverter},ConverterParameter=ToLower}" Margin="4,0,0,0" Foreground="{DynamicResource SecondaryForegroundBrush}"/>
                        <TextBlock Text=")" Foreground="{DynamicResource SecondaryForegroundBrush}"/>
                    </StackPanel>
                </DataTemplate>
                
                <DataTemplate DataType="{x:Type m:CustomSize}">
                    <StackPanel Orientation="Horizontal" Margin="0,-8,0,0">
                        <TextBlock VerticalAlignment="Center"
                                   Text="{Binding Name}"
                                   Foreground="{DynamicResource PrimaryForegroundBrush}"
                                   FontWeight="SemiBold"/>
                        <ComboBox Margin="8,0,0,0"
                                  ItemsSource="{Binding Source={StaticResource ResizeFitValues}}"
                                  Style="{StaticResource DisabledWhenUnselectedComboBoxStyle}"
                                  AutomationProperties.Name="{x:Static p:Resources.Resize_Type}"
                                  SelectedItem="{Binding Fit}">
                            <ComboBox.ItemTemplate>
                                <DataTemplate DataType="{x:Type m:ResizeFit}">
                                    <ContentPresenter Content="{Binding Converter={StaticResource EnumValueConverter}}"/>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                        <TextBox Width="56"
                                 TextWrapping="Wrap"
                                 Style="{StaticResource DisabledWhenUnselectedTextBoxStyle}"
                                 AutomationProperties.Name="{x:Static p:Resources.Width}"
                                 Margin="8,0,0,0">
                            <TextBox.Text>
                                <Binding Converter="{StaticResource AutoDoubleConverter}"
                                         Path="Width"
                                         UpdateSourceTrigger="PropertyChanged">
                                    <Binding.ValidationRules>
                                        <local:AutoDoubleValidationRule/>
                                    </Binding.ValidationRules>
                                </Binding>
                            </TextBox.Text>
                        </TextBox>
                        <TextBlock VerticalAlignment="Center"
                                   Text="&#xE711;"
                                   FontFamily="Segoe MDL2 Assets"
                                   Width="25"
                                   Foreground="{DynamicResource SecondaryForegroundBrush}"
                                   Name="Times_Symbol"
                                   AutomationProperties.Name="{x:Static p:Resources.Times_Symbol}"
                                   TextAlignment="Center"
                                   Visibility="{Binding ShowHeight,Converter={StaticResource BoolValueConverter}}"/>

                        <TextBox Width="56"
                                 Style="{StaticResource DisabledWhenUnselectedTextBoxStyle}"
                                 TextWrapping="Wrap"
                                 AutomationProperties.Name="{x:Static p:Resources.Height}"
                                 Visibility="{Binding ShowHeight,Converter={StaticResource BoolValueConverter}}">
                            <TextBox.Text>
                                <Binding Converter="{StaticResource AutoDoubleConverter}"
                                         Path="Height"
                                         UpdateSourceTrigger="PropertyChanged">
                                    <Binding.ValidationRules>
                                        <local:AutoDoubleValidationRule/>
                                    </Binding.ValidationRules>
                                </Binding>
                            </TextBox.Text>
                        </TextBox>
                        <ComboBox Margin="8,0,0,0"
                                  ItemsSource="{Binding Source={StaticResource ResizeUnitValues}}"
                                  Style="{StaticResource DisabledWhenUnselectedComboBoxStyle}"
                                  AutomationProperties.Name="{x:Static p:Resources.Unit}"
                                  SelectedItem="{Binding Unit}">
                            <ComboBox.ItemTemplate>
                                <DataTemplate DataType="{x:Type m:ResizeUnit}">
                                    <ContentPresenter Content="{Binding Converter={StaticResource EnumValueConverter}}"/>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                    </StackPanel>
                </DataTemplate>
            </ListBox.Resources>
        </ListBox>
        
        <CheckBox Margin="12,36,12,0"
                  Content="{x:Static p:Resources.Input_ShrinkOnly}"
                  IsChecked="{Binding Settings.ShrinkOnly}"/>
        <!-- TODO: This option doesn't make much sense when resizing into a directory. We should swap it for an option
                   to overwrite any files in the directory instead (issue #88) -->
        <CheckBox Margin="12,4,12,0"
                  Content="{x:Static p:Resources.Input_Replace}"
                  IsChecked="{Binding Settings.Replace}"/>
        
        <CheckBox Margin="12,4,12,0"
                  Content="{x:Static p:Resources.Input_IgnoreOrientation}"
                  IsChecked="{Binding Settings.IgnoreOrientation}"/>
        
        <Border Margin="0,24,0,0"
                Background="{DynamicResource SecondaryBackgroundBrush}"
                BorderBrush="{DynamicResource PrimaryBorderBrush}"
                BorderThickness="0,1,0,0"
                Padding="12">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <Button Content="&#xE115;"
                        FontFamily="Segoe MDL2 Assets"
                        Style="{StaticResource DefaultButtonStyle}"
                        FontSize="16"
                        Margin="-6,0,0,0"
                        AutomationProperties.Name="{x:Static p:Resources.Input_ShowAdvanced}"
                        ToolTip="{x:Static p:Resources.Input_ShowAdvanced}"
                        Background="Transparent"
                        Command="{Binding ShowAdvancedCommand}"
                        Visibility="{Binding ShowAdvancedSettings, Converter={StaticResource BoolValueConverter}}"
                        />

                <Button Grid.Column="1"
                        Style="{StaticResource AccentButtonStyle}"
                        MinWidth="76"
                        Command="{Binding ResizeCommand}"
                        AutomationProperties.Name="{x:Static p:Resources.Resize_Tooltip}"
                        Content="{x:Static p:Resources.Input_Resize}"
                        IsDefault="True"/>

                <Button Grid.Column="2"
                        Style="{StaticResource DefaultButtonStyle}"
                        MinWidth="76"
                        Margin="12,0,0,0"
                        Command="{Binding CancelCommand}"
                        Content="{x:Static p:Resources.Cancel}"
                        IsCancel="True"/>
            </Grid>
        </Border>
    </StackPanel>
</UserControl>
