<Window x:Class="CefFlashBrowser.Views.FavoritesManager"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:data="clr-namespace:CefFlashBrowser.Data"
        xmlns:local="clr-namespace:CefFlashBrowser.Views"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:hc="https://handyorg.github.io/handycontrol"
        xmlns:svgc="http://sharpvectors.codeplex.com/svgc/"
        xmlns:behaviors="clr-namespace:CefFlashBrowser.Views.Behaviors"
        mc:Ignorable="d"
        
        Width="700"
        Height="450"
        MinWidth="500"
        MinHeight="300"
        WindowStartupLocation="CenterScreen"
        DataContext="{Binding Source={StaticResource Locator}, Path=FavoritesManagerViewModel}"
        Title="{DynamicResource favorites_managerTitle}">

    <Window.InputBindings>
        <KeyBinding Gesture="Ctrl+W"
                    Command="{Binding ElementName=windowBehavior, Path=CloseWindowCommand}"/>
    </Window.InputBindings>

    <i:Interaction.Behaviors>
        <behaviors:WindowBehavior x:Name="windowBehavior"/>
    </i:Interaction.Behaviors>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" MinWidth="150" MaxWidth="400"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0"
              Margin="10,10,0,10">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <StackPanel Grid.Column="0"
                        Margin="0,0,0,7"
                        Orientation="Horizontal">
                <Button Margin="0,0,7,0"
                        Command="{Binding AddItemCommand}">
                    <Button.Content>
                        <StackPanel Orientation="Horizontal">
                            <svgc:SvgIcon Width="12"
                                          Height="12"
                                          UriSource="/Assets/Svgs/add.svg"/>
                            <TextBlock Margin="8,0,0,0"
                                       Text="{DynamicResource favorites_add}"/>
                        </StackPanel>
                    </Button.Content>
                </Button>
            </StackPanel>

            <ListBox Grid.Row="1"
                     DisplayMemberPath="Name"
                     ItemsSource="{Binding Favorites}"
                     SelectedItem="{Binding SelectedWebsite}">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem"
                           BasedOn="{StaticResource ListBoxItemBaseStyle}">
                        <Setter Property="Tag"
                                Value="{Binding RelativeSource={RelativeSource AncestorType=ListBox}, Path=DataContext}"/>
                        <Setter Property="ContextMenu">
                            <Setter.Value>
                                <ContextMenu>
                                    <ContextMenu.Resources>
                                        <Style TargetType="MenuItem"
                                               BasedOn="{StaticResource MenuItemBaseStyle}">
                                            <Setter Property="Tag"
                                                    Value="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.Tag}"/>
                                        </Style>
                                    </ContextMenu.Resources>
                                    <MenuItem Header="{DynamicResource common_moveToTop}"
                                              Command="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag.MoveToTopCommand}"
                                              CommandParameter="{Binding}"/>
                                    <MenuItem Header="{DynamicResource common_moveToBottom}"
                                              Command="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag.MoveToBottomCommand}"
                                              CommandParameter="{Binding}"/>
                                    <Separator/>
                                    <MenuItem Header="{DynamicResource favorites_remove}"
                                              Command="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag.RemoveItemCommand}"
                                              CommandParameter="{Binding}"/>
                                </ContextMenu>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>
        </Grid>

        <GridSplitter Grid.Column="1"
                      Width="10"
                      Background="Transparent"
                      VerticalAlignment="Stretch"
                      HorizontalAlignment="Center"/>

        <GroupBox Grid.Column="2"
                  Margin="0,10,10,10"
                  Header="{DynamicResource favorites_edit}">
            <GroupBox.Style>
                <Style TargetType="GroupBox"
                       BasedOn="{StaticResource {x:Type GroupBox}}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding SelectedWebsite}"
                                     Value="{x:Null}">
                            <Setter Property="IsEnabled" Value="False"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </GroupBox.Style>
            <Grid>
                <Grid.BindingGroup>
                    <BindingGroup x:Name="itemEditingGroup"/>
                </Grid.BindingGroup>

                <StackPanel Width="300"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Center"
                            TextBox.TextChanged="TextChangedHandler">
                    <hc:TextBox x:Name="nameTextBox"
                                Text="{Binding SelectedWebsite.Name, UpdateSourceTrigger=Explicit}"
                                hc:TitleElement.Title="{DynamicResource favorites_name}">
                        <i:Interaction.Behaviors>
                            <behaviors:TextBoxSelectAllOnFocus/>
                        </i:Interaction.Behaviors>
                        <TextBox.InputBindings>
                            <KeyBinding Key="Enter"
                                        Command="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=CommitEditCommand}"/>
                        </TextBox.InputBindings>
                    </hc:TextBox>

                    <hc:TextBox x:Name="urlTextBox"
                                Margin="0,5,0,0"
                                Text="{Binding SelectedWebsite.Url, UpdateSourceTrigger=Explicit}"
                                hc:TitleElement.Title="{DynamicResource favorites_url}">
                        <i:Interaction.Behaviors>
                            <behaviors:TextBoxSelectAllOnFocus/>
                        </i:Interaction.Behaviors>
                        <TextBox.InputBindings>
                            <KeyBinding Key="Enter"
                                        Command="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=CommitEditCommand}"/>
                        </TextBox.InputBindings>
                    </hc:TextBox>

                    <DockPanel Margin="0,20,0,10"
                               LastChildFill="False">
                        <StackPanel DockPanel.Dock="Left"
                                    Orientation="Horizontal">
                            <StackPanel.Resources>
                                <Style TargetType="Button"
                                       BasedOn="{StaticResource {x:Type Button}}">
                                    <Setter Property="Width" Value="30"/>
                                    <Setter Property="Height" Value="30"/>
                                    <Setter Property="Padding" Value="0"/>
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                </Style>
                            </StackPanel.Resources>
                            <Button ToolTip="{DynamicResource common_moveUp}"
                                    Command="{Binding MoveUpCommand}"
                                    CommandParameter="{Binding SelectedWebsite}">
                                <svgc:SvgIcon Width="12"
                                              Height="12"
                                              UriSource="/Assets/Svgs/up.svg"/>
                            </Button>
                            <Button ToolTip="{DynamicResource common_moveDown}"
                                    Command="{Binding MoveDownCommand}"
                                    CommandParameter="{Binding SelectedWebsite}">
                                <svgc:SvgIcon Width="12"
                                              Height="12"
                                              UriSource="/Assets/Svgs/down.svg"/>
                            </Button>
                        </StackPanel>

                        <StackPanel DockPanel.Dock="Right"
                                    Orientation="Horizontal">
                            <StackPanel.Resources>
                                <Style TargetType="Button"
                                       BasedOn="{StaticResource {x:Type Button}}">
                                    <Setter Property="MinWidth" Value="75"/>
                                </Style>
                            </StackPanel.Resources>
                            <Button Content="{DynamicResource favorites_remove}"
                                    Command="{Binding RemoveItemCommand}"
                                    CommandParameter="{Binding SelectedWebsite}"/>
                            <Button Margin="5,0,0,0"
                                    Content="{DynamicResource favorites_save}"
                                    Command="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=CommitEditCommand}"/>
                        </StackPanel>
                    </DockPanel>
                </StackPanel>
            </Grid>
        </GroupBox>
    </Grid>
</Window>
