<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:s="https://github.com/canton7/Stylet"
                    xmlns:l="clr-namespace:SyncTrayzor.Localization"
                    xmlns:ni="clr-namespace:SyncTrayzor.NotifyIcon"
                    xmlns:tb="http://www.hardcodet.net/taskbar"
                    xmlns:xaml="clr-namespace:SyncTrayzor.Xaml"
                    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">

    <BitmapImage x:Key="TaskbarStoppedIcon" UriSource="pack://application:,,,/Icons/stopped.ico"/>
    <BitmapImage x:Key="TaskbarAlertIcon" UriSource="pack://application:,,,/Icons/alert_tray.ico"/>
    <BitmapImage x:Key="TaskbarPausedIcon" UriSource="pack://application:,,,/Icons/paused_tray.ico"/>
    <BitmapImage x:Key="TaskbarSyncing1Icon" UriSource="pack://application:,,,/Icons/default_tray.ico"/>
    <BitmapImage x:Key="TaskbarSyncing2Icon" UriSource="pack://application:,,,/Icons/syncing_2.ico"/>
    <BitmapImage x:Key="TaskbarSyncing3Icon" UriSource="pack://application:,,,/Icons/syncing_3.ico"/>
    <BitmapImage x:Key="TaskbarSyncing4Icon" UriSource="pack://application:,,,/Icons/syncing_4.ico"/>

    <tb:TaskbarIcon x:Key="TaskbarIcon" x:Name="TaskbarIcon"
                    Visibility="{Binding Visible, Converter={x:Static s:BoolToVisibilityConverter.Instance}}"
                    DoubleClickCommand="{s:Action DoubleClick, ActionNotFound=Disable}"
                    MenuActivation="RightClick"
                    ToolTipText="SyncTrayzor">
        <tb:TaskbarIcon.TrayPopup>
            <Popup StaysOpen="False" Placement="Relative" HorizontalOffset="-150" VerticalOffset="100" Width="300" PlacementTarget="{Binding ElementName=TaskbarIcon}" AllowsTransparency="True">
                <i:Interaction.Behaviors>
                    <xaml:PopupConductorBehaviour DataContext="{Binding FileTransfersViewModel}"/>
                </i:Interaction.Behaviors>
                
                <ContentControl s:View.Model="{Binding FileTransfersViewModel}"/>
            </Popup>
        </tb:TaskbarIcon.TrayPopup>
        <tb:TaskbarIcon.Resources>
            <Storyboard x:Key="IconAnimation">
                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(ni:NotifyIconResolutionUtilities.IconSource)" Duration="0:0:2" RepeatBehavior="Forever">
                    <ObjectAnimationUsingKeyFrames.KeyFrames>
                        <DiscreteObjectKeyFrame KeyTime="0:0:0">
                            <DiscreteObjectKeyFrame.Value>
                                <StaticResourceExtension ResourceKey="TaskbarSyncing1Icon"/>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                        <DiscreteObjectKeyFrame KeyTime="0:0:0.5">
                            <DiscreteObjectKeyFrame.Value>
                                <StaticResourceExtension ResourceKey="TaskbarSyncing2Icon"/>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                        <DiscreteObjectKeyFrame KeyTime="0:0:1.0">
                            <DiscreteObjectKeyFrame.Value>
                                <StaticResourceExtension ResourceKey="TaskbarSyncing3Icon"/>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                        <DiscreteObjectKeyFrame KeyTime="0:0:1.5">
                            <DiscreteObjectKeyFrame.Value>
                                <StaticResourceExtension ResourceKey="TaskbarSyncing4Icon"/>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames.KeyFrames>
                </ObjectAnimationUsingKeyFrames>
            </Storyboard>
        </tb:TaskbarIcon.Resources>
        <tb:TaskbarIcon.Style>
            <Style TargetType="tb:TaskbarIcon">
                <Setter Property="ni:NotifyIconResolutionUtilities.IconSource" Value="{StaticResource TaskbarSyncing1Icon}"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SyncthingWarning}" Value="True">
                        <Setter Property="ni:NotifyIconResolutionUtilities.IconSource" Value="{StaticResource TaskbarAlertIcon}"/>
                    </DataTrigger>

                    <DataTrigger Binding="{Binding SyncthingDevicesPaused}" Value="True">
                        <Setter Property="ni:NotifyIconResolutionUtilities.IconSource" Value="{StaticResource TaskbarPausedIcon}"/>
                    </DataTrigger>

                    <DataTrigger Binding="{Binding SyncthingStarted}" Value="False">
                        <Setter Property="ni:NotifyIconResolutionUtilities.IconSource" Value="{StaticResource TaskbarStoppedIcon}"/>
                    </DataTrigger>

                    <DataTrigger Binding="{Binding SyncthingSyncing}" Value="True">
                        <DataTrigger.EnterActions>
                            <BeginStoryboard Storyboard="{StaticResource IconAnimation}" Name="IconAnimation"/>
                        </DataTrigger.EnterActions>
                        <DataTrigger.ExitActions>
                            <RemoveStoryboard BeginStoryboardName="IconAnimation"/>
                        </DataTrigger.ExitActions>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </tb:TaskbarIcon.Style>

        <tb:TaskbarIcon.ContextMenu>
            <!-- Attached properties aren't inherited to ContextMenus -->
            <ContextMenu s:View.ActionTarget="{Binding}">
                <ContextMenu.Resources>
                    <s:BoolToVisibilityConverter x:Key="RestoreVisibility" TrueVisibility="Collapsed" FalseVisibility="Visible"/>
                </ContextMenu.Resources>
                <!-- TaskbarIcon starts by setting the DataContext to itself, so just ignore this... -->
                <MenuItem Header="{l:Loc TrayIcon_Menu_Restore}" FontWeight="Bold"  Command="{s:Action Restore, ActionNotFound=Disable}"
                          Visibility="{Binding MainWindowVisible, Converter={StaticResource RestoreVisibility}}"/>
                <Separator Visibility="{Binding MainWindowVisible, Converter={StaticResource RestoreVisibility}}"/>
                
                <MenuItem Header="{l:Loc TrayIcon_Menu_CloseToTray}" Command="{s:Action Minimize, ActionNotFound=Disable}"
                          Visibility="{Binding MainWindowVisible, Converter={x:Static s:BoolToVisibilityConverter.Instance}}"/>
                <Separator Visibility="{Binding MainWindowVisible, Converter={x:Static s:BoolToVisibilityConverter.Instance}}"/>
                
                <MenuItem Header="{l:Loc TrayIcon_Menu_OpenFolder}" ItemsSource="{Binding Folders}"
                          Visibility="{Binding Folders, Converter={x:Static s:BoolToVisibilityConverter.Instance}}">
                    <MenuItem.ItemContainerStyle>
                        <Style TargetType="MenuItem">
                            <Setter Property="Header" Value="{Binding FolderLabel, Converter={x:Static xaml:RemoveMnemonicsConverter.Instance}}"/>
                            <Setter Property="Command" Value="{Binding}"/>
                        </Style>
                    </MenuItem.ItemContainerStyle>
                </MenuItem>
                <Separator Visibility="{Binding Folders, Converter={x:Static s:BoolToVisibilityConverter.Instance}}"/>
                
                <MenuItem Header="{l:Loc TrayIcon_Menu_StartSyncthing}" Command="{s:Action Start, ActionNotFound=Disable}"/>
                <MenuItem Header="{l:Loc TrayIcon_Menu_StopSyncthing}" Command="{s:Action Stop, ActionNotFound=Disable}"/>
                <MenuItem Header="{l:Loc TrayIcon_Menu_RestartSyncthing}" Command="{s:Action Restart, ActionNotFound=Disable}"/>
                <Separator/>

                <MenuItem Header="{l:Loc TrayIcon_Menu_Settings}" Command="{s:Action ShowSettings, ActionNotFound=Disable}"/>
                <Separator/>
                
                <MenuItem Header="{l:Loc TrayIcon_Menu_Exit}" Command="{s:Action Exit, ActionNotFound=Disable}"/>
            </ContextMenu>
        </tb:TaskbarIcon.ContextMenu>

    </tb:TaskbarIcon>
</ResourceDictionary>