<UserControl
    x:Class="Wino.Calendar.Controls.CalendarItemControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="using:Wino.Mail.WinUI.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:helpers="using:Wino.Helpers"
    xmlns:local="using:Wino.Calendar.Controls"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
    d:DesignHeight="300"
    d:DesignWidth="400"
    CanDrag="True"
    DragStarting="ControlDragStarting"
    DropCompleted="ControlDropped"
    mc:Ignorable="d">

    <Grid
        x:Name="MainGrid"
        CornerRadius="4"
        DoubleTapped="ControlDoubleTapped"
        RightTapped="ControlRightTapped"
        Tapped="ControlTapped"
        ToolTipService.ToolTip="{x:Bind CalendarItem.DisplayTitle, Mode=OneWay}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <Grid.ContextFlyout>
            <local:CalendarItemCommandBarFlyout
                AlwaysExpanded="True"
                Closed="CalendarItemCommandBarFlyout_Closed"
                Opening="CalendarItemCommandBarFlyout_Opening"
                Placement="BottomEdgeAlignedLeft"
                ShowMode="Auto" />
        </Grid.ContextFlyout>


        <Grid
            x:Name="MainBackground"
            Grid.Column="1"
            Grid.ColumnSpan="2"
            Background="{x:Bind helpers:XamlHelpers.GetSolidColorBrushFromHex(CalendarItem.AssignedCalendar.BackgroundColorHex), Mode=OneWay}"
            Opacity="0.9" />

        <Rectangle
            x:Name="MainBorder"
            Grid.Column="1"
            Grid.ColumnSpan="2"
            Canvas.ZIndex="2"
            Stroke="{ThemeResource CalendarItemBorderBrush}"
            StrokeThickness="0" />

        <!--  ShowAs Status Stripe  -->
        <ContentControl
            x:Name="ShowAsStripe"
            Grid.Column="0"
            HorizontalContentAlignment="Stretch"
            VerticalContentAlignment="Stretch"
            Canvas.ZIndex="10000"
            Content="{x:Bind CalendarItem, Mode=OneWay}"
            ContentTemplateSelector="{StaticResource ShowAsStripeSelector}"
            IsTabStop="False" />

        <TextBlock
            x:Name="EventTitleTextblock"
            Grid.Column="1"
            Margin="2,0"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Center"
            CharacterSpacing="8"
            FontSize="13"
            Foreground="{x:Bind helpers:XamlHelpers.GetReadableTextColor(CalendarItem.AssignedCalendar.BackgroundColorHex), Mode=OneWay}"
            HorizontalTextAlignment="Center"
            Text="{x:Bind CalendarItem.DisplayTitle, Mode=OneWay}"
            TextTrimming="CharacterEllipsis" />


        <muxc:ProgressRing
            x:Name="BusyRing"
            Grid.Column="2"
            Width="10"
            Height="10"
            Margin="0,2,2,0"
            HorizontalAlignment="Right"
            VerticalAlignment="Top"
            IsActive="False"
            Visibility="Collapsed" />

        <!--  TODO: Event attributes  -->
        <StackPanel
            x:Name="AttributeStack"
            Grid.Column="2"
            Margin="0,4,4,0"
            HorizontalAlignment="Right"
            VerticalAlignment="Top"
            Orientation="Horizontal"
            Spacing="6">
            <controls:WinoFontIcon
                FontSize="12"
                Foreground="{x:Bind helpers:XamlHelpers.GetReadableTextColor(CalendarItem.AssignedCalendar.BackgroundColorHex), Mode=OneWay}"
                Icon="CalendarEventRepeat"
                Visibility="{x:Bind CalendarItem.IsRecurringEvent, Mode=OneWay}" />

            <controls:WinoFontIcon
                FontSize="12"
                Foreground="{x:Bind helpers:XamlHelpers.GetReadableTextColor(CalendarItem.AssignedCalendar.BackgroundColorHex), Mode=OneWay}"
                Icon="CalendarEventMuiltiDay"
                Visibility="{x:Bind CalendarItem.IsMultiDayEvent, Mode=OneWay}" />
        </StackPanel>

        <VisualStateManager.VisualStateGroups>
            <!--  TODO: Selection of items are disabled temporarily.  -->
            <VisualStateGroup x:Name="SelectionStates">
                <VisualState x:Name="NonSelected" />
                <VisualState x:Name="Selected">
                    <VisualState.Setters>
                        <Setter Target="MainBorder.StrokeThickness" Value="1" />
                        <Setter Target="MainBorder.Margin" Value="1" />
                        <Setter Target="MainBorder.Stroke" Value="{ThemeResource CalendarItemSelectedBorderBrush}" />
                    </VisualState.Setters>
                    <VisualState.StateTriggers>
                        <StateTrigger IsActive="{x:Bind CalendarItem.IsSelected, Mode=OneWay, FallbackValue=False}" />
                    </VisualState.StateTriggers>
                </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="DraggingStates">
                <VisualState x:Name="NotDragging" />
                <VisualState x:Name="Dragging">
                    <VisualState.StateTriggers>
                        <StateTrigger IsActive="{x:Bind IsDragging, Mode=OneWay}" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="MainBorder.StrokeThickness" Value="2" />
                        <Setter Target="MainBorder.Stroke" Value="{ThemeResource CalendarItemDraggingBorderBrush}" />
                        <Setter Target="MainBorder.StrokeDashArray" Value="2.5" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="BusyStates">
                <VisualState x:Name="NotBusy" />
                <VisualState x:Name="Busy">
                    <VisualState.StateTriggers>
                        <StateTrigger IsActive="{x:Bind CalendarItem.IsBusy, Mode=OneWay, FallbackValue=False}" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="MainBackground.Opacity" Value="0.55" />
                        <Setter Target="BusyRing.IsActive" Value="True" />
                        <Setter Target="BusyRing.Visibility" Value="Visible" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>

            <VisualStateGroup x:Name="EventDurationStates">
                <!--  Regular event template in the panel.  -->
                <VisualState x:Name="RegularEvent" />

                <!--  All-Day template for top area.  -->
                <VisualState x:Name="AllDayEvent">
                    <VisualState.Setters>
                        <Setter Target="EventTitleTextblock.HorizontalAlignment" Value="Stretch" />
                        <Setter Target="EventTitleTextblock.HorizontalTextAlignment" Value="Left" />
                        <Setter Target="EventTitleTextblock.Margin" Value="6,0" />
                        <Setter Target="AttributeStack.VerticalAlignment" Value="Center" />
                        <Setter Target="AttributeStack.Margin" Value="0,0,4,0" />
                        <Setter Target="MainGrid.MinHeight" Value="30" />
                        <Setter Target="MainBorder.StrokeThickness" Value="0" />
                    </VisualState.Setters>
                </VisualState>

                <!--  Multi-Day template for top area.  -->
                <VisualState x:Name="CustomAreaMultiDayEvent">
                    <VisualState.Setters>
                        <Setter Target="MainBackground.Opacity" Value="1" />
                        <Setter Target="MainBorder.StrokeThickness" Value="0" />
                        <Setter Target="AttributeStack.Visibility" Value="Visible" />
                        <Setter Target="AttributeStack.Margin" Value="0,0,4,0" />
                        <Setter Target="AttributeStack.VerticalAlignment" Value="Center" />
                        <Setter Target="MainGrid.MinHeight" Value="30" />
                        <Setter Target="EventTitleTextblock.HorizontalAlignment" Value="Stretch" />
                        <Setter Target="EventTitleTextblock.HorizontalTextAlignment" Value="Left" />
                        <Setter Target="EventTitleTextblock.Margin" Value="6,0" />
                    </VisualState.Setters>
                </VisualState>

                <!--
                    Ghost rendering for multi-day events in the panel.
                    All-Multi day area template is CustomAreaMultiDayEvent
                -->

                <VisualState x:Name="MultiDayEvent">
                    <VisualState.Setters>
                        <Setter Target="MainGrid.CornerRadius" Value="0" />
                        <Setter Target="MainBackground.Opacity" Value="0.2" />
                        <Setter Target="MainGrid.IsHitTestVisible" Value="False" />
                        <Setter Target="MainBorder.StrokeThickness" Value="0" />
                        <Setter Target="AttributeStack.Visibility" Value="Collapsed" />
                        <Setter Target="EventTitleTextblock.Visibility" Value="Collapsed" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</UserControl>

