<Window x:Class="CefFlashBrowser.Views.MainWindow"
        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:local="clr-namespace:CefFlashBrowser"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:m="clr-namespace:CefFlashBrowser.Models"
        xmlns:data="clr-namespace:CefFlashBrowser.Data"
        xmlns:hc="https://handyorg.github.io/handycontrol"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:behaviors="clr-namespace:CefFlashBrowser.Views.Behaviors"
        mc:Ignorable="d"
        
        DataContext="{Binding Source={StaticResource Locator}, Path=MainWindowViewModel}"
        Title="{Binding WelcomeText}"
        Width="600"
        Height="450"
        MinWidth="400"
        MinHeight="300"
        FocusManager.FocusedElement="{Binding ElementName=inputTextBox}">

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

    <i:Interaction.Behaviors>
        <behaviors:WindowBehavior x:Name="windowBehavior"/>
        <behaviors:DropFileBehavior Command="{Binding DropFileCommand}"/>
    </i:Interaction.Behaviors>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Menu>
            <!--Favorites Menu-->
            <MenuItem Header="{DynamicResource common_favorites}">
                <MenuItem.Resources>
                    <DataTemplate DataType="{x:Type m:Website}">
                        <StackPanel MaxWidth="225">
                            <TextBlock Text="{Binding Name}"
                                       TextTrimming="CharacterEllipsis"/>
                            <TextBlock FontSize="10"
                                       Text="{Binding Url}"
                                       TextTrimming="CharacterEllipsis"
                                       Foreground="{DynamicResource SecondaryTextBrush}"/>
                        </StackPanel>
                    </DataTemplate>
                </MenuItem.Resources>

                <MenuItem.ItemContainerStyle>
                    <Style TargetType="MenuItem"
                           BasedOn="{StaticResource {x:Type MenuItem}}">
                        <Setter Property="Tag"
                                Value="{Binding RelativeSource={RelativeSource AncestorType=Menu}, Path=DataContext}"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ., Converter={StaticResource ObjectGetType}}"
                                         Value="{x:Type m:Website}">
                                <Setter Property="Command"
                                        Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag.OpenWebsiteCommand}"/>
                                <Setter Property="CommandParameter"
                                        Value="{Binding}"/>
                                <Setter Property="ContextMenu">
                                    <Setter.Value>
                                        <ContextMenu>
                                            <MenuItem Header="{DynamicResource favorites_remove}"
                                                      Command="{Binding Source={StaticResource Locator}, Path=FavoritesManagerViewModel.RemoveItemCommand}"
                                                      CommandParameter="{Binding}"/>
                                        </ContextMenu>
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </MenuItem.ItemContainerStyle>

                <MenuItem.ItemsSource>
                    <CompositeCollection>
                        <!--Favorites Manager-->
                        <MenuItem Padding="10,7"
                                  Header="{DynamicResource mainWindow_manageFavorites}"
                                  Command="{Binding OpenFavoritesManagerCommand}"/>

                        <!--Separator that collapses when there are no favorites-->
                        <Separator>
                            <Separator.Style>
                                <Style TargetType="Separator"
                                       BasedOn="{StaticResource {x:Type Separator}}">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Source={x:Static data:GlobalData.Favorites}, Path=Count}"
                                                     Value="0">
                                            <Setter Property="Visibility" Value="Collapsed"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Separator.Style>
                        </Separator>

                        <!--Favorites Items-->
                        <CollectionContainer Collection="{Binding Source={x:Static data:GlobalData.Favorites}}"/>
                    </CompositeCollection>
                </MenuItem.ItemsSource>
            </MenuItem>

            <!--Options Menu-->
            <MenuItem Header="{DynamicResource mainWindow_options}">

                <!--Load Swf Option-->
                <MenuItem Header="{DynamicResource mainWindow_loadSwf}"
                          Command="{Binding LoadSwfCommand}"/>

                <Separator/>

                <!--SolSaveManager-->
                <MenuItem Header="{DynamicResource mainWindow_manageSolSaveFiles}"
                          Command="{Binding OpenSolSaveManagerCommand}"/>

                <!--Open Sol File-->
                <MenuItem Header="{DynamicResource mainWindow_openSolFile}"
                          Command="{Binding OpenSolFileCommand}"/>

                <Separator/>

                <!--Languages Option-->
                <MenuItem x:Name="languageMenu"
                          Header="{DynamicResource mainWindow_languages}"
                          ItemsSource="{Binding Source={StaticResource Locator}, Path=LanguageSelectorViewModel.LanguageList}">
                    <MenuItem.ItemContainerStyle>
                        <Style TargetType="MenuItem"
                               BasedOn="{StaticResource {x:Type MenuItem}}">
                            <Setter Property="Header"
                                    Value="{Binding LanguageName, Mode=OneWay}"/>
                            <Setter Property="Command"
                                    Value="{Binding Source={StaticResource Locator}, Path=LanguageSelectorViewModel.SetLanguageCommand}"/>
                            <Setter Property="CommandParameter"
                                    Value="{Binding Language}"/>
                            <Setter Property="IsChecked"
                                    Value="{Binding IsCurrentLanguage, Mode=OneWay}"/>
                        </Style>
                    </MenuItem.ItemContainerStyle>
                </MenuItem>

                <!--Settings Option-->
                <MenuItem Header="{DynamicResource common_settings}"
                          Command="{Binding OpenSettingsWindowCommand}"/>
            </MenuItem>
        </Menu>

        <StackPanel Grid.Row="1"
                    Width="300"
                    Margin="0,0,0,20"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Center">

            <TextBlock Margin="0,0,0,35"
                       HorizontalAlignment="Center">
                <Hyperlink Foreground="{DynamicResource PrimaryTextBrush}"
                           FontSize="30"
                           TextDecorations="None"
                           ToolTip="{DynamicResource mainWindow_viewGithub}"
                           Command="{Binding ViewGithubCommand}">
                    <TextBlock Text="Flash Browser"/>
                </Hyperlink>
            </TextBlock>

            <!--The url textBox-->
            <TextBox x:Name="inputTextBox"
                     Text="{Binding InputText, UpdateSourceTrigger=PropertyChanged}">
                <i:Interaction.Behaviors>
                    <behaviors:TextBoxSelectAllOnFocus/>
                </i:Interaction.Behaviors>
                <TextBox.InputBindings>
                    <KeyBinding Key="Enter"
                                Command="{Binding OpenUrlCommand}"
                                CommandParameter="{Binding InputText}"/>
                </TextBox.InputBindings>
            </TextBox>
        </StackPanel>

        <!--The app info shows on the bottom of the main window-->
        <TextBlock Grid.Row="2"
                   Margin="5"
                   HorizontalAlignment="Center"
                   Foreground="{DynamicResource SecondaryTextBrush}"
                   Text="{Binding Source={StaticResource AppInfo}, Path=DisplayVersion, Mode=OneTime}">
        </TextBlock>
    </Grid>
</Window>
