<!--
    Copyright (c) Microsoft Corporation. All rights reserved. Licensed under
    the MIT License. See LICENSE in the project root for license information.
-->
<UserControl x:Class="Microsoft.Terminal.UI.Markdown.CodeBlock"
             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:local="using:Microsoft.Terminal.UI.Markdown"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:mtu="using:Microsoft.Terminal.UI"
             xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
             HorizontalAlignment="Stretch"
             VerticalAlignment="Stretch"
             d:DesignHeight="256"
             d:DesignWidth="1024"
             mc:Ignorable="d">
    <UserControl.Resources>

        <ResourceDictionary>
            <ResourceDictionary.ThemeDictionaries>
                <ResourceDictionary x:Key="Dark">
                    <Color x:Key="BlockBackgroundColor">#1e1e1e</Color>
                    <Color x:Key="BlockBorderColor">#30363d</Color>
                    <Color x:Key="PlayButtonHoveredColor">#90ef90</Color>
                    <Color x:Key="PlayButtonNormalColor">#88888888</Color>
                </ResourceDictionary>
                <ResourceDictionary x:Key="Light">
                    <Color x:Key="BlockBackgroundColor">#f6f8fa</Color>
                    <Color x:Key="BlockBorderColor">#d3d3d3</Color>
                    <Color x:Key="PlayButtonHoveredColor">#257f01</Color>
                    <Color x:Key="PlayButtonNormalColor">#88222222</Color>
                </ResourceDictionary>
                <ResourceDictionary x:Key="HighContrast">
                    <StaticResource x:Key="BlockBackgroundColor"
                                    ResourceKey="SystemColorWindowColorBrush" />
                    <StaticResource x:Key="BlockBorderColor"
                                    ResourceKey="SystemColorWindowColorBrush" />
                    <StaticResource x:Key="PlayButtonHoveredColor"
                                    ResourceKey="SystemColorHighlightColor" />
                    <StaticResource x:Key="PlayButtonNormalColor"
                                    ResourceKey="SystemColorHighlightColor" />
                </ResourceDictionary>
            </ResourceDictionary.ThemeDictionaries>


            <Style x:Key="PlayButtonTemplate"
                   TargetType="Button">
                <Setter Property="Margin" Value="0" />
                <Setter Property="Padding" Value="4" />
                <Setter Property="BorderBrush" Value="Transparent" />
                <Setter Property="Background" Value="Transparent" />

                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border x:Name="ButtonBaseElement"
                                    Padding="{TemplateBinding Padding}"
                                    AutomationProperties.AccessibilityView="Raw"
                                    Background="{TemplateBinding Background}"
                                    BackgroundSizing="{TemplateBinding BackgroundSizing}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    CornerRadius="{TemplateBinding CornerRadius}">
                                <Viewbox Width="20"
                                         Height="20">
                                    <Grid>
                                        <FontIcon x:Name="ButtonBackgroundIcon"
                                                  FontFamily="{ThemeResource SymbolThemeFontFamily}"
                                                  Foreground="{ThemeResource PlayButtonHoveredColor}"
                                                  Glyph="&#xF5b0;"
                                                  Visibility="Collapsed" />
                                        <FontIcon x:Name="ButtonOutlineIcon"
                                                  FontFamily="{ThemeResource SymbolThemeFontFamily}"
                                                  Foreground="{ThemeResource PlayButtonNormalColor}"
                                                  Glyph="&#xE768;" />
                                        <muxc:ProgressRing x:Name="StatusProgress"
                                                           IsActive="False" />
                                    </Grid>
                                </Viewbox>

                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">

                                        <VisualState x:Name="Normal">
                                            <VisualState.Setters>
                                                <Setter Target="ButtonBackgroundIcon.Visibility" Value="Collapsed" />
                                            </VisualState.Setters>
                                        </VisualState>

                                        <VisualState x:Name="PointerOver">
                                            <VisualState.Setters>
                                                <Setter Target="ButtonBackgroundIcon.Visibility" Value="Visible" />
                                            </VisualState.Setters>
                                        </VisualState>


                                        <VisualState x:Name="Disabled" />
                                    </VisualStateGroup>

                                    <VisualStateGroup x:Name="PlayButtonStates">
                                        <VisualState x:Name="Ready">
                                            <VisualState.Setters>
                                                <Setter Target="ButtonBackgroundIcon.Glyph" Value="&#xF5b0;" />
                                                <Setter Target="ButtonOutlineIcon.Glyph" Value="&#xE768;" />
                                                <Setter Target="StatusProgress.IsActive" Value="False" />
                                            </VisualState.Setters>
                                        </VisualState>

                                        <VisualState x:Name="Running">
                                            <VisualState.Setters>
                                                <Setter Target="ButtonBackgroundIcon.Glyph" Value=" " />
                                                <Setter Target="ButtonOutlineIcon.Glyph" Value=" " />
                                                <Setter Target="StatusProgress.IsActive" Value="True" />
                                            </VisualState.Setters>
                                        </VisualState>

                                        <VisualState x:Name="AlreadyRan">
                                            <VisualState.Setters>
                                                <Setter Target="ButtonBackgroundIcon.Glyph" Value="&#xe72c;" />
                                                <Setter Target="ButtonOutlineIcon.Glyph" Value="&#xe72c;" />
                                                <Setter Target="StatusProgress.IsActive" Value="False" />
                                            </VisualState.Setters>
                                        </VisualState>
                                    </VisualStateGroup>

                                </VisualStateManager.VisualStateGroups>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>

            </Style>
            <Style x:Key="CodeBlockLineTemplate"
                   TargetType="TextBlock">
                <Setter Property="FontFamily" Value="Cascadia Mono, Consolas" />
            </Style>

        </ResourceDictionary>

    </UserControl.Resources>

    <Grid HorizontalAlignment="Stretch">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Button x:Name="RunButton"
                Grid.Column="0"
                Margin="0,9,0,0"
                Padding="0"
                VerticalAlignment="Top"
                Style="{StaticResource PlayButtonTemplate}"
                Tapped="_playPressed"
                Visibility="{x:Bind PlayButtonVisibility, Mode=OneWay}">
            <Button.Resources>
                <ResourceDictionary>
                    <ResourceDictionary.ThemeDictionaries>
                        <ResourceDictionary x:Key="Light">
                            <SolidColorBrush x:Key="ButtonForegroundPointerOver"
                                             Color="{StaticResource SystemAccentColor}" />
                            <SolidColorBrush x:Key="ButtonForegroundPressed"
                                             Color="{StaticResource SystemAccentColor}" />
                        </ResourceDictionary>
                        <ResourceDictionary x:Key="Dark">
                            <SolidColorBrush x:Key="ButtonForegroundPointerOver"
                                             Color="{StaticResource SystemAccentColor}" />
                            <SolidColorBrush x:Key="ButtonForegroundPressed"
                                             Color="{StaticResource SystemAccentColor}" />
                        </ResourceDictionary>
                        <ResourceDictionary x:Key="HighContrast">
                            <SolidColorBrush x:Key="ButtonBackground"
                                             Color="{ThemeResource SystemColorButtonFaceColor}" />
                            <SolidColorBrush x:Key="ButtonBackgroundPointerOver"
                                             Color="{ThemeResource SystemColorHighlightColor}" />
                            <SolidColorBrush x:Key="ButtonBackgroundPressed"
                                             Color="{ThemeResource SystemColorHighlightColor}" />
                            <SolidColorBrush x:Key="ButtonForeground"
                                             Color="{ThemeResource SystemColorButtonTextColor}" />
                            <SolidColorBrush x:Key="ButtonForegroundPointerOver"
                                             Color="{ThemeResource SystemColorHighlightTextColor}" />
                            <SolidColorBrush x:Key="ButtonForegroundPressed"
                                             Color="{ThemeResource SystemColorHighlightTextColor}" />
                        </ResourceDictionary>
                    </ResourceDictionary.ThemeDictionaries>
                </ResourceDictionary>
            </Button.Resources>
        </Button>
        <Border Grid.Column="1"
                Padding="0"
                HorizontalAlignment="Stretch"
                Background="{ThemeResource BlockBackgroundColor}"
                BorderBrush="{ThemeResource BlockBorderColor}"
                BorderThickness="1"
                CornerRadius="4">
            <ScrollViewer HorizontalAlignment="Stretch"
                          HorizontalScrollBarVisibility="Auto"
                          HorizontalScrollMode="Auto">
                <Grid x:Name="CommandsAndOutput"
                      Margin="14"
                      HorizontalAlignment="Stretch"
                      VerticalAlignment="Stretch">
                    <TextBlock FontFamily="Cascadia Mono, Consolas"
                               IsTextSelectionEnabled="True"
                               Text="{x:Bind Commandlines}">
                        <TextBlock.ContextFlyout>
                            <mtu:TextMenuFlyout />
                        </TextBlock.ContextFlyout>
                    </TextBlock>
                </Grid>
            </ScrollViewer>
        </Border>
    </Grid>

</UserControl>
