<Page x:Class="ForzaUI.Scenes.Anthem.A_LanguageSelect"
      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"
      mc:Ignorable="d"
      xmlns:local="clr-namespace:ForzaUI;assembly="
      Style="{StaticResource A_Style_Page_Generic}"
      DataContext="{Binding RelativeSource={RelativeSource Self}}"
      xmlns:System="clr-namespace:System;assembly=mscorlib">

    <Page.Resources>

        <local:MultiConverter x:Key="A_Converter_ScreenReaderSelection">
            <local:MultiConverterEntry Converter="{StaticResource A_Converter_StringFormatConverter}" Parameter="{local:Loc InGame.IDS_LanguageSelect_ScreenReader_CurrentlyApplied}" TargetType="{x:Type String}" />
        </local:MultiConverter>

        <DataTemplate x:Key="A_DataTemplate_ListBoxItem_LanguageSelectItem">
            <Grid IsHitTestVisible="False"
                  Height="{StaticResource A_Double_TextMenu_Height_Default}">
                <local:FZTextBlock x:Name="Text"
                                   Text="{Binding Name, Converter={StaticResource A_StringToUpper}}"
                                   Style="{StaticResource A_Style_FZTextBlock_OptionText}"
                                   local:AccumulateSelfOnParent.ParentName="OptionsList"
                                   local:AccumulateSelfOnParent.SortOrder="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"
                                   local:FindSelfIndexInCollection.Collection="{Binding (local:AccumulateSelfOnParent.Output), ElementName=OptionsList}"
                                   local:ScreenReader.SelectedNumber="{Binding (local:FindSelfIndexInCollection.Index), RelativeSource={RelativeSource Self}, Converter={StaticResource A_IndexToNumberConverter}, FallbackValue=0}"
                                   local:ScreenReader.SelectedCount="{Binding (local:AccumulateSelfOnParent.Output).Count, ElementName=OptionsList, FallbackValue=0}"
                                   local:ScreenReader.Event="OnChange"
                                   local:ScreenReader.ControlType="Button"
                                   local:ScreenReader.LineType="SelectedItem" />
                <StackPanel HorizontalAlignment="Right" Visibility="{Binding IsToggle, Converter={StaticResource A_Converter_BoolToVis}}" Orientation="Horizontal">
                    <Control x:Name="CheckBox"
                             VerticalAlignment="Center"
                             Style="{StaticResource A_Style_Control_Checkbox}"
                             Tag="{Binding IsSelected}" />
                </StackPanel>
            </Grid>
            <DataTemplate.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource TemplatedParent}}" Value="True" />
                        <Condition Binding="{Binding IsSelected}" Value="True" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="local:ScreenReader.Line"
                            TargetName="Text"
                            Value="{Binding ScreenReaderName, Converter={StaticResource A_Converter_ScreenReaderSelection}}" />
                </MultiDataTrigger>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource TemplatedParent}}" Value="True" />
                        <Condition Binding="{Binding IsSelected}" Value="False" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="local:ScreenReader.Line"
                            TargetName="Text"
                            Value="{Binding ScreenReaderName}" />
                </MultiDataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <ControlTemplate x:Key="A_ControlTemplate_ScrollViewer_LanguageSelect"
                     TargetType="ScrollViewer">
            <Grid>
                <Canvas HorizontalAlignment="Right">
                    <ScrollBar x:Name="PART_VerticalScrollBar"
                               HorizontalAlignment="Left"
                               Minimum="0"
                               MinHeight="{Binding ActualHeight, ElementName=PART_ScrollContentPresenter}"
                               Maximum="{TemplateBinding ScrollViewer.ScrollableHeight}"
                               ViewportSize="{TemplateBinding ScrollViewer.ViewportHeight}"
                               Value="{Binding Path=VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource Mode=TemplatedParent}}"
                               Visibility="{TemplateBinding ScrollViewer.ComputedVerticalScrollBarVisibility}"
                               Style="{StaticResource A_Style_ScrollBar_TextMenu}" />
                </Canvas>

                <ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
                                        Content="{TemplateBinding Content}"
                                        CanContentScroll="{TemplateBinding ScrollViewer.CanContentScroll}"
                                        ClipToBounds="True" />
            </Grid>
        </ControlTemplate>

        <ControlTemplate TargetType="{x:Type ListBox}"
              x:Key="A_Template_ListBox_LanguageSelectPopup">
            <Border Background="{x:Null}">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="ValidationStates">
                        <VisualState x:Name="Valid" />
                        <VisualState x:Name="InvalidFocused" />
                        <VisualState x:Name="InvalidUnfocused" />
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                <local:FZScrollViewer Template="{StaticResource A_ControlTemplate_ScrollViewer_LanguageSelect}"
                                      CanContentScroll="True"
                                      HorizontalScrollBarVisibility="Disabled"
                                      VerticalScrollBarVisibility="{Binding Path=(ScrollViewer.VerticalScrollBarVisibility), RelativeSource={RelativeSource TemplatedParent}}"
                                      local:FZScrollViewer.IsAtTop="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:FZScrollViewer.IsAtTop), Mode=OneWayToSource}"
                                      local:FZScrollViewer.IsAtBottom="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:FZScrollViewer.IsAtBottom), Mode=OneWayToSource}"
                                      Focusable="False"
                                      Margin="-18,0"
                                      KeepSelectionOnScreen="True">
                    <VirtualizingStackPanel Orientation="Vertical"
                                            Margin="18,0"
                                            IsItemsHost="True"
                                            VirtualizingStackPanel.VirtualizationMode="Recycling"
                                            Focusable="False" />
                </local:FZScrollViewer>
            </Border>
        </ControlTemplate>

        <Style x:Key="A_Style_ControllerButtonControl_AButton"
               TargetType="{x:Type local:ControllerButtonControl}"
               BasedOn="{StaticResource A_Style_ControllerButtonControl_Default}">
            <Setter Property="Description" Value="{local:Loc HelpButtons.IDS_Select}" />
            <Setter Property="Tag" Value="{Binding LanguageSelectedCommand, RelativeSource={RelativeSource AncestorType=Page}}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding SelectedItem.IsSelected}" Value="True">
                    <Setter Property="Description" Value="" />
                    <Setter Property="Tag" Value="{x:Null}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Page.Resources>

    <local:UIPageTransitionManager.TransitionManager>
        <local:UIPageTransitionManager Preset="{StaticResource A_UIPageTransitionManagerPreset_Popup}"/>
    </local:UIPageTransitionManager.TransitionManager>

    <Grid>
        <Control Style="{StaticResource A_Style_Control_ScreenBackground}" />

        <local:PlayerBarControl x:Name="TopBar"
                                Style="{StaticResource A_Style_PlayerBarControl}" />

        <ContentControl x:Name="BottomBar"
                        Style="{StaticResource A_Style_ButtonBar}">
            <local:ControllerButtonPanel x:Name="HelpPanel"
                                         Style="{StaticResource A_Style_ControllerButtonPanel_Default}">
                <local:ControllerButtonControl Button="A"
                                               Style="{StaticResource A_Style_ControllerButtonControl_AButton}"
                                               x:Name="AButtonControl" />
                <local:ControllerButtonControl Button="B"
                                               Description="{local:Loc HelpButtons.IDS_Back}" />
            </local:ControllerButtonPanel>
        </ContentControl>

        <Grid Style="{StaticResource A_Style_FrameworkElement_LayoutRoot}">
            <StackPanel x:Name="MainContent"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        Style="{StaticResource A_Style_FrameworkElement_DefaultTransition_0}">
                <Decorator Style="{StaticResource A_Style_FrameworkElement_DefaultTransition}"
                           local:UIPageTransitionManager.TransitionIndex="0"
                           VerticalAlignment="Top">
                    <ContentControl Content="{Binding Source={local:Loc ScreenTitles.IDS_LanguageSelect}, Converter={StaticResource A_StringToUpper}}"
                                    Style="{StaticResource A_Style_ContentControl_ScreenTitleBlock}"
                                    HorizontalAlignment="Left"
                                    local:BindingProxy.A="/ForzaUI;component/Textures/Anthem/Icons/Titles/LanguageSelect.png" />
                </Decorator>

                <local:CommonListBox x:Name="OptionsList"
                                     Style="{StaticResource A_Style_ListBox_TextMenu_Default}"
                                     ItemContainerStyle="{StaticResource A_Style_ListBoxItem_TextMenu_Default}"
                                     ItemTemplate="{StaticResource A_DataTemplate_ListBoxItem_LanguageSelectItem}"
                                     ItemsSource="{Binding LanguageCollection}"
                                     Template="{StaticResource A_Template_ListBox_LanguageSelectPopup}"
                                     SelectedItem="{Binding SelectedItem}"
                                     local:FocusOnPageFocusGained.SelectorSelectedItem="True"
                                     MaxHeight="663"
                                     Width="830" />
            </StackPanel>
            <local:FZTextBlock x:Name="DebugOnlyText"
                               Text="{Binding CommandlineLangAppliedMessage, RelativeSource={RelativeSource AncestorType=Page}}"
                               Visibility="{Binding CommandlineLangAppliedMessage, Converter={StaticResource A_StringVisConverter}}"
                               Style="{StaticResource A_Style_FZTextBlock_HorizonB}"
                               Color="#FF0000"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Bottom"
                               Margin="5"
                               CapHeight="15" />
        </Grid>

        <local:PageInputHandler Button="{x:Static local:ControllerButtonType.A}"
                                Command="{Binding Tag, ElementName=AButtonControl}"
                                local:AncestorPropertySetter.Value="{Binding Command, RelativeSource={RelativeSource Self}}"
                                local:AncestorPropertySetter.Type="{x:Type local:LanguageSelect}"
                                local:AncestorPropertySetter.Property="SelectCommand" />
    </Grid>
</Page>
