<Page
    x:Class="Microsoft.PowerToys.Settings.UI.Views.KeyboardManagerPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Lib="using:Microsoft.PowerToys.Settings.UI.Library"
    xmlns:controls="using:Microsoft.PowerToys.Settings.UI.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:tkcontrols="using:CommunityToolkit.WinUI.Controls"
    xmlns:tkconverters="using:CommunityToolkit.WinUI.Converters"
    xmlns:ui="using:CommunityToolkit.WinUI"
    AutomationProperties.LandmarkType="Main"
    mc:Ignorable="d">

    <Page.Resources>
        <tkconverters:CollectionVisibilityConverter x:Key="CollectionVisibilityConverter" />
        <tkconverters:BoolToVisibilityConverter
            x:Key="BoolToInvertedVisibilityConverter"
            FalseValue="Visible"
            TrueValue="Collapsed" />
        <tkconverters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />

        <Style x:Name="KeysListViewContainerStyle" TargetType="ListViewItem">
            <Setter Property="IsTabStop" Value="False" />
        </Style>

        <DataTemplate x:Key="OriginalKeyTemplate" x:DataType="x:String">
            <controls:KeyVisual Content="{Binding}" VisualType="SmallOutline" />
        </DataTemplate>

        <DataTemplate x:Key="RemappedKeyTemplate" x:DataType="x:String">
            <controls:KeyVisual Content="{Binding}" VisualType="Small" />
        </DataTemplate>

        <!--<DataTemplate x:Name="KeysListViewTemplate" x:DataType="Lib:KeysDataModel">
            <StackPanel
                Name="KeyboardManager_RemappedKeysListItem"
                x:Uid="KeyboardManager_RemappedKeysListItem"
                Orientation="Horizontal"
                Height="56">

            </StackPanel>
        </DataTemplate>-->
        <!--<DataTemplate x:Name="ShortcutKeysListViewTemplate" x:DataType="Lib:AppSpecificKeysDataModel">
            <StackPanel
                Name="KeyboardManager_RemappedShortcutsListItem"
                x:Uid="KeyboardManager_RemappedShortcutsListItem"
                Orientation="Horizontal"
                Height="56">

        </DataTemplate>-->
    </Page.Resources>

    <controls:SettingsPageControl x:Uid="KeyboardManager" ModuleImageSource="ms-appx:///Assets/Settings/Modules/KBM.png">
        <controls:SettingsPageControl.ModuleContent>
            <StackPanel ChildrenTransitions="{StaticResource SettingsCardsAnimations}" Orientation="Vertical">
                <tkcontrols:SettingsCard
                    x:Uid="KeyboardManager_EnableToggle"
                    HeaderIcon="{ui:BitmapIcon Source=/Assets/Settings/Icons/KeyboardManager.png}"
                    IsEnabled="{x:Bind ViewModel.IsEnabledGpoConfigured, Mode=OneWay, Converter={StaticResource BoolNegationConverter}}">
                    <ToggleSwitch x:Uid="ToggleSwitch" IsOn="{x:Bind ViewModel.Enabled, Mode=TwoWay}" />
                    <tkcontrols:SettingsCard.Description>
                        <HyperlinkButton NavigateUri="https://aka.ms/powerToysCannotRemapKeys">
                            <TextBlock x:Uid="KBM_KeysCannotBeRemapped" FontWeight="SemiBold" />
                        </HyperlinkButton>
                    </tkcontrols:SettingsCard.Description>
                </tkcontrols:SettingsCard>
                <InfoBar
                    x:Uid="GPO_SettingIsManaged"
                    IsClosable="False"
                    IsOpen="{x:Bind ViewModel.IsEnabledGpoConfigured, Mode=OneWay}"
                    IsTabStop="{x:Bind ViewModel.IsEnabledGpoConfigured, Mode=OneWay}"
                    Severity="Informational" />

                <controls:SettingsGroup x:Uid="KeyboardManager_Keys" IsEnabled="{x:Bind ViewModel.Enabled, Mode=OneWay}">
                    <tkcontrols:SettingsCard
                        x:Uid="KeyboardManager_RemapKeyboardButton"
                        ActionIcon="{ui:FontIcon Glyph=&#xE8A7;}"
                        Command="{Binding Path=RemapKeyboardCommand}"
                        HeaderIcon="{ui:FontIcon Glyph=&#xE92E;}"
                        IsClickEnabled="True" />

                    <ListView
                        x:Name="RemapKeysList"
                        x:Uid="RemapKeysList"
                        IsSwipeEnabled="False"
                        ItemsSource="{x:Bind Path=ViewModel.RemapKeys, Mode=OneWay}"
                        SelectionMode="None"
                        Visibility="{x:Bind Path=ViewModel.RemapKeys, Mode=OneWay, Converter={StaticResource CollectionVisibilityConverter}}">
                        <ListView.ItemTemplate>
                            <DataTemplate x:DataType="Lib:KeysDataModel">
                                <tkcontrols:SettingsCard ContentAlignment="Left">
                                    <StackPanel Orientation="Horizontal">
                                        <ItemsControl
                                            IsTabStop="False"
                                            ItemTemplate="{StaticResource OriginalKeyTemplate}"
                                            ItemsSource="{x:Bind GetMappedOriginalKeys()}">
                                            <ItemsControl.ItemsPanel>
                                                <ItemsPanelTemplate>
                                                    <StackPanel Orientation="Horizontal" Spacing="4" />
                                                </ItemsPanelTemplate>
                                            </ItemsControl.ItemsPanel>
                                        </ItemsControl>
                                        <controls:IsEnabledTextBlock
                                            x:Uid="To"
                                            Margin="8,0,8,0"
                                            VerticalAlignment="Center"
                                            Style="{StaticResource SecondaryIsEnabledTextBlockStyle}" />
                                        <ItemsControl
                                            Name="KeyboardManager_RemappedTo"
                                            x:Uid="KeyboardManager_RemappedTo"
                                            IsTabStop="False"
                                            ItemTemplate="{StaticResource RemappedKeyTemplate}"
                                            ItemsSource="{x:Bind GetMappedNewRemapKeys(50)}">
                                            <ItemsControl.ItemsPanel>
                                                <ItemsPanelTemplate>
                                                    <StackPanel Orientation="Horizontal" Spacing="4" />
                                                </ItemsPanelTemplate>
                                            </ItemsControl.ItemsPanel>
                                        </ItemsControl>
                                    </StackPanel>
                                </tkcontrols:SettingsCard>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </controls:SettingsGroup>

                <controls:SettingsGroup x:Uid="KeyboardManager_Shortcuts" IsEnabled="{x:Bind ViewModel.Enabled, Mode=OneWay}">
                    <tkcontrols:SettingsCard
                        x:Uid="KeyboardManager_RemapShortcutsButton"
                        ActionIcon="{ui:FontIcon Glyph=&#xE8A7;}"
                        Command="{Binding Path=EditShortcutCommand}"
                        HeaderIcon="{ui:FontIcon Glyph=&#xE713;}"
                        IsClickEnabled="True" />

                    <ListView
                        x:Name="RemapShortcutsList"
                        x:Uid="RemapShortcutsList"
                        IsSwipeEnabled="False"
                        ItemsSource="{x:Bind Path=ViewModel.RemapShortcuts, Mode=OneWay}"
                        SelectionMode="None"
                        Visibility="{x:Bind Path=ViewModel.RemapShortcuts, Mode=OneWay, Converter={StaticResource CollectionVisibilityConverter}}">
                        <ListView.ItemTemplate>
                            <DataTemplate x:DataType="Lib:AppSpecificKeysDataModel">

                                <tkcontrols:SettingsCard>
                                    <tkcontrols:SettingsCard.Description>
                                        <StackPanel Orientation="Horizontal">
                                            <ItemsControl
                                                IsTabStop="False"
                                                ItemTemplate="{StaticResource OriginalKeyTemplate}"
                                                ItemsSource="{x:Bind GetMappedOriginalKeysWithoutChord()}">
                                                <ItemsControl.ItemsPanel>
                                                    <ItemsPanelTemplate>
                                                        <StackPanel Orientation="Horizontal" Spacing="4" />
                                                    </ItemsPanelTemplate>
                                                </ItemsControl.ItemsPanel>
                                            </ItemsControl>

                                            <TextBlock
                                                Padding="6,0,6,6"
                                                VerticalAlignment="Bottom"
                                                Text=","
                                                Visibility="{x:Bind Path=HasChord, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}" />

                                            <ItemsControl
                                                IsTabStop="False"
                                                ItemTemplate="{StaticResource OriginalKeyTemplate}"
                                                ItemsSource="{x:Bind GetMappedOriginalKeysOnlyChord()}"
                                                Visibility="{x:Bind Path=HasChord, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}">
                                                <ItemsControl.ItemsPanel>
                                                    <ItemsPanelTemplate>
                                                        <StackPanel Orientation="Horizontal" Spacing="4" />
                                                    </ItemsPanelTemplate>
                                                </ItemsControl.ItemsPanel>
                                            </ItemsControl>

                                            <controls:IsEnabledTextBlock
                                                x:Uid="To"
                                                Margin="8,0,8,0"
                                                VerticalAlignment="Center"
                                                Style="{StaticResource SecondaryIsEnabledTextBlockStyle}"
                                                Visibility="{x:Bind Path=IsOpenUriOrIsRunProgram, Mode=OneWay, Converter={StaticResource BoolToInvertedVisibilityConverter}}" />

                                            <controls:IsEnabledTextBlock
                                                x:Uid="Starts"
                                                Margin="8,0,8,0"
                                                VerticalAlignment="Center"
                                                Style="{StaticResource SecondaryIsEnabledTextBlockStyle}"
                                                Visibility="{x:Bind Path=IsOpenUriOrIsRunProgram, Mode=OneWay}" />


                                            <ItemsControl
                                                Name="KeyboardManager_RemappedTo"
                                                x:Uid="KeyboardManager_RemappedTo"
                                                IsTabStop="False"
                                                ItemTemplate="{StaticResource RemappedKeyTemplate}"
                                                ItemsSource="{x:Bind GetMappedNewRemapKeys(50)}">
                                                <ItemsControl.ItemsPanel>
                                                    <ItemsPanelTemplate>
                                                        <StackPanel Orientation="Horizontal" Spacing="4" />
                                                    </ItemsPanelTemplate>
                                                </ItemsControl.ItemsPanel>
                                            </ItemsControl>
                                            <Border
                                                Margin="16,0,0,0"
                                                Padding="12,4,12,6"
                                                VerticalAlignment="Center"
                                                CornerRadius="12">
                                                <Border.Background>
                                                    <SolidColorBrush Opacity="0.3" Color="{ThemeResource SystemAccentColor}" />
                                                </Border.Background>
                                                <TextBlock Text="{x:Bind TargetApp}" />
                                            </Border>

                                        </StackPanel>
                                    </tkcontrols:SettingsCard.Description>
                                </tkcontrols:SettingsCard>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </controls:SettingsGroup>
            </StackPanel>
        </controls:SettingsPageControl.ModuleContent>
        <controls:SettingsPageControl.PrimaryLinks>
            <controls:PageLink x:Uid="LearnMore_KBM" Link="https://aka.ms/PowerToysOverview_KeyboardManager" />
        </controls:SettingsPageControl.PrimaryLinks>
    </controls:SettingsPageControl>
</Page>
