<n:BaseRecorder xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:n="clr-namespace:ScreenToGif.Controls"
                  xmlns:c="clr-namespace:ScreenToGif.Util.Converters"
                  xmlns:t="clr-namespace:ScreenToGif.Settings"
                  x:Class="ScreenToGif.Windows.Webcam" x:Name="WebcamWindow" 
                  Title="{DynamicResource S.Webcam.Title}" Height="400" Width="600" MinWidth="200" MinHeight="200" 
                  Icon="/Resources/Logo.ico" WindowStartupLocation="CenterScreen" Background="{DynamicResource Panel.Background}"
                  Loaded="Window_Loaded" LocationChanged="Window_LocationChanged" Closing="Window_Closing">

    <Window.Resources>
        <c:StageToButtonString x:Key="StageToButtonStringConverter"/>
        <c:StageToCanvas x:Key="StageToCanvasConverter"/>
        <c:KeysToString x:Key="KeysToStringConverter"/>
        <c:ShortcutKeys x:Key="ShortcutKeys"/>

        <Storyboard x:Key="ShowDiscardStoryboard">
            <BooleanAnimationUsingKeyFrames Storyboard.TargetName="DiscardButton" Storyboard.TargetProperty="IsHitTestVisible" Duration="0:0:0" >
                <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0"/>
            </BooleanAnimationUsingKeyFrames>

            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DiscardButton" Storyboard.TargetProperty="Visibility" Duration="0:0:0">
                <DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" KeyTime="0:0:0"/>
            </ObjectAnimationUsingKeyFrames>

            <DoubleAnimation Storyboard.TargetName="DiscardButton" Storyboard.TargetProperty="(Button.Opacity)" From="0" To="1" Duration="0:0:1">
                <DoubleAnimation.EasingFunction>
                    <PowerEase Power="8" />
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>

        <Storyboard x:Key="HideDiscardStoryboard">
            <BooleanAnimationUsingKeyFrames Storyboard.TargetName="DiscardButton" Storyboard.TargetProperty="IsHitTestVisible" Duration="0:0:0" >
                <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:0"/>
            </BooleanAnimationUsingKeyFrames>

            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DiscardButton" Storyboard.TargetProperty="Visibility" Duration="0:0:0">
                <DiscreteObjectKeyFrame Value="{x:Static Visibility.Collapsed}" KeyTime="0:0:0"/>
            </ObjectAnimationUsingKeyFrames>

            <DoubleAnimation Storyboard.TargetName="DiscardButton" Storyboard.TargetProperty="(Button.Opacity)" 
                             From="{Binding ElementName=DiscardButton,Path=Opacity}" To="0" Duration="0:0:1">
                <DoubleAnimation.EasingFunction>
                    <PowerEase Power="8" />
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>
    </Window.Resources>

    <Window.CommandBindings>
        <CommandBinding Command="{StaticResource Command.Options}" CanExecute="NotRecording_CanExecute" Executed="Options_Executed"/>
        <CommandBinding Command="{StaticResource Command.CheckVideoDevices}" CanExecute="NotRecording_CanExecute" Executed="CheckVideoDevices_Executed"/>
        <CommandBinding Command="Stop" CanExecute="Stop_CanExecute" Executed="Stop_Executed"/>
    </Window.CommandBindings>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="31"/>
        </Grid.RowDefinitions>

        <n:WebcamControl Grid.Row="0" x:Name="WebcamControl"/>

        <Label Grid.Row="0" x:Name="NoVideoLabel" Content="{DynamicResource S.Webcam.NoVideo}" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" 
               Visibility="Hidden" FontSize="24" Foreground="{DynamicResource Element.Foreground.Header}"/>

        <Grid Grid.Row="1" x:Name="LowerGrid" Height="31" VerticalAlignment="Bottom" UseLayoutRounding="True" SnapsToDevicePixels="True">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="30"/>
                <ColumnDefinition Width="26"/>
                <ColumnDefinition Width="26"/>
                <ColumnDefinition Width="26"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="11"/>
                <ColumnDefinition Width="31"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="11"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>

            <n:ExtendedButton Grid.Column="1" x:Name="OptionsButton" Icon="{StaticResource Vector.Options}" TabIndex="0" ContentWidth="20" ContentHeight="20"
                           Style="{StaticResource Style.Button.NoText}" HorizontalContentAlignment="Center" Padding="3" Command="{StaticResource Command.Options}">
                <n:ExtendedButton.ToolTip>
                    <ToolTip HorizontalOffset="-5" Placement="Bottom" Content="{DynamicResource S.StartUp.Options}"/>
                </n:ExtendedButton.ToolTip>
            </n:ExtendedButton>

            <n:ExtendedButton Grid.Column="2" x:Name="ScaleButton" Icon="{StaticResource Vector.Resize}" TabIndex="1" ContentWidth="20" ContentHeight="20" Padding="3" 
                              Style="{StaticResource Style.Button.NoText}" HorizontalContentAlignment="Center" Click="ScaleButton_Click"
                              IsEnabled="{Binding ElementName=VideoDevicesComboBox, Path=IsEnabled}">
                <n:ExtendedButton.ToolTip>
                    <ToolTip Placement="Bottom" HorizontalOffset="-5" Content="{DynamicResource S.Webcam.ChangeScale}" />
                </n:ExtendedButton.ToolTip>
            </n:ExtendedButton>

            <n:ExtendedButton Grid.Column="3" x:Name="RefreshButton" Icon="{StaticResource Vector.Repeat}" TabIndex="1" ContentWidth="20" ContentHeight="20" Padding="3" 
                              Style="{StaticResource Style.Button.NoText}" HorizontalContentAlignment="Center" Command="{StaticResource Command.CheckVideoDevices}">
                <n:ExtendedButton.ToolTip>
                    <ToolTip Placement="Bottom" HorizontalOffset="-5" Content="{DynamicResource S.Webcam.CheckVideoDevices}" />
                </n:ExtendedButton.ToolTip>
            </n:ExtendedButton>

            <ComboBox Grid.Column="4" x:Name="VideoDevicesComboBox" MinWidth="120" Margin="0,3" TabIndex="2" IsEnabled="False" SelectionChanged="VideoDevicesComboBox_SelectionChanged"/>

            <Separator Grid.Column="5" Width="1" Margin="5,2"/>

            <Viewbox Grid.Column="6" ClipToBounds="True" Focusable="False">
                <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Margin="-4" Opacity="{DynamicResource Element.Opacity}">
                    <n:CircularProgressBar StrokeThickness="2" Percentage="100" SegmentColor="Gray" Radius="24"/>
                    <n:CircularProgressBar StrokeThickness="22" Percentage="100" SegmentColor="#FFF0F1F1" Radius="10"/>

                    <n:CircularProgressBar StrokeThickness="2" Value="{Binding ElementName=FpsNumericUpDown, Path=Value, Mode=OneWay}" 
                                           IsInverted="True" Minimum="1" Maximum="60" SegmentColor="#FFE28A73" Radius="24"/>
                    <n:CircularProgressBar StrokeThickness="22" Value="{Binding ElementName=FpsNumericUpDown, Path=Value, Mode=OneWay}" 
                                           IsInverted="True" Minimum="1" Maximum="60" SegmentColor="#FFE28A73" Radius="10" />
                </Grid>

                <Viewbox.ToolTip>
                    <ToolTip HorizontalOffset="-5" Placement="Bottom" Content="{DynamicResource S.Recorder.Fps.Range}"/>
                </Viewbox.ToolTip>
            </Viewbox>

            <n:IntegerUpDown Grid.Column="7" x:Name="FpsNumericUpDown" Margin="1,3" StepValue="1" Minimum="1" Maximum="60" TabIndex="3"
                             Value="{Binding LatestFps, Mode=TwoWay, Source={x:Static t:UserSettings.All}}" MinWidth="45" IsEnabled="False">
                <n:IntegerUpDown.ToolTip>
                    <ToolTip HorizontalOffset="-5" Placement="Bottom" Content="{DynamicResource S.Recorder.Fps}"/>
                </n:IntegerUpDown.ToolTip>
            </n:IntegerUpDown>

            <TextBlock Grid.Column="8" Text="fps" FontSize="12" FontFamily="Segoe UI" Margin="1,0,0,0" VerticalAlignment="Center" Padding="0" Foreground="{DynamicResource Element.Foreground.Medium}"/>

            <Separator Grid.Column="9" Width="1" Margin="5,2"/>

            <n:ExtendedButton Grid.Column="10" x:Name="DiscardButton" Text="{DynamicResource S.Recorder.Discard}" Icon="{StaticResource Vector.Remove}" Visibility="Collapsed" 
                              Click="DiscardButton_Click" Style="{StaticResource Style.Button.Horizontal}" UseLayoutRounding="True" ContentWidth="16" ContentHeight="16" TabIndex="4" 
                              MinWidth="{Binding RelativeSource={RelativeSource Mode=Self}, Path=ActualHeight}" KeyGesture="{Binding Converter={StaticResource ShortcutKeys}, ConverterParameter='3'}"/>

            <n:ExtendedButton Grid.Column="11" x:Name="RecordPauseButton" IsEnabled="False" TabIndex="5"
                              Text="{Binding Path=Stage, ElementName=WebcamWindow, Converter={StaticResource StageToButtonStringConverter}, FallbackValue={StaticResource S.Recorder.Record}}" 
                              Icon="{Binding Path=Stage, ElementName=WebcamWindow, Converter={StaticResource StageToCanvasConverter}, FallbackValue={StaticResource Vector.Record}}" 
                              KeyGesture="{Binding Converter={StaticResource ShortcutKeys}, ConverterParameter='1'}"
                              ContentWidth="16" ContentHeight="16" Click="RecordPauseButton_Click" Style="{StaticResource Style.Button.Horizontal}"/>

            <n:ExtendedButton Grid.Column="12" x:Name="StopButton" Text="{DynamicResource S.Recorder.Stop}" Icon="{StaticResource Vector.Stop}" ContentWidth="16" ContentHeight="16" Margin="0" 
                              Command="Stop" Style="{StaticResource Style.Button.Horizontal}" UseLayoutRounding="True" TabIndex="6"
                              KeyGesture="{Binding Converter={StaticResource ShortcutKeys}, ConverterParameter='2'}"/>
        </Grid>

        <Popup Grid.Row="0" x:Name="ScalePopup" Placement="Bottom" PlacementTarget="{Binding ElementName=ScaleButton}" StaysOpen="False" Width="200" Height="50">
            <Border BorderThickness="1" BorderBrush="{DynamicResource Element.Border}" Background="{DynamicResource Panel.Background}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>

                    <n:LabelSeparator Grid.Row="0" Text="{Binding ElementName=ScaleSlider, Path=Value, StringFormat={StaticResource S.Webcam.Scale}}"/>

                    <Slider Grid.Row="1" x:Name="ScaleSlider" Margin="5,3" Minimum="0.4" Maximum="1.5" Value="0.5" LargeChange="0.1" TickFrequency="0.1" TickPlacement="None" ValueChanged="Slider_ValueChanged"/>
                </Grid>
            </Border>
        </Popup>
    </Grid>
</n:BaseRecorder>