<UserControl x:Class="_1RM.Controls.ServerCardItem"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" 
             xmlns:s="clr-namespace:System;assembly=mscorlib"
             xmlns:attachProperty="clr-namespace:Shawn.Utils.WpfResources.Theme.AttachProperty;assembly=Shawn.Utils.WpfResources"
             xmlns:view="clr-namespace:_1RM.View"
             xmlns:model="clr-namespace:_1RM.Model"
             xmlns:dao="clr-namespace:_1RM.Service.DataSource.DAO"
             d:DataContext="{d:DesignInstance view:ProtocolBaseViewModel}"
             d:DesignHeight="450" d:DesignWidth="800" >
    <UserControl.Resources>
        <s:Double x:Key="CardWidth">144</s:Double>
        <RectangleGeometry x:Key="CardClip" RadiusX="5" RadiusY="5" Rect="0 0 144 144"></RectangleGeometry>

        <Style x:Key="PathSourceStatusStyle" TargetType="Path">
            <Setter Property="Data" Value="{StaticResource GeometryCloudDisconnected}"></Setter>
            <Setter Property="Fill" Value="Red"></Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding DataSource.Status}" Value="{x:Static dao:EnumDatabaseStatus.OK}">
                    <Setter Property="Visibility" Value="Collapsed"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="TextProtocolStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBlockBase}">
            <Setter Property="RenderTransform">
                <Setter.Value>
                    <TransformGroup>
                        <TranslateTransform X="-2.5" Y="25"></TranslateTransform>
                        <RotateTransform CenterX="0" CenterY="25" Angle="-45"></RotateTransform>
                    </TransformGroup>
                </Setter.Value>
            </Setter>
        </Style>


        <Style x:Key="ButtonLayerStyle" TargetType="Border">
            <Setter Property="Background" Value="Transparent"></Setter>
            <Setter Property="BorderBrush" Value="Transparent"></Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=CardNameBarMask,Path=IsMouseOver}" Value="True">
                    <Setter Property="BorderBrush" Value="{DynamicResource AccentMidBrush}"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=PopupCardSettingMenu,Path=IsOpen}" Value="True">
                    <Setter Property="BorderBrush" Value="{DynamicResource AccentMidBrush}"></Setter>
                </DataTrigger>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="BorderBrush" Value="{DynamicResource AccentMidBrush}"></Setter>
                </Trigger>
                <DataTrigger Binding="{Binding IsSelected}" Value="True">
                    <Setter Property="BorderBrush" Value="{DynamicResource AccentMidBrush}"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>


        <Style x:Key="CheckBoxSelectedStyle" TargetType="CheckBox" BasedOn="{StaticResource CheckBoxAccentStyle}">
            <Setter Property="Visibility" Value="Hidden"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="CheckBox">
                        <Border Background="{TemplateBinding Control.Background}" 
                                BorderThickness="0" 
                                CornerRadius="2,0,4,0" Name="PART_Background">
                            <Grid>
                                <Path Name="PATH_UNCHECK" Fill="{DynamicResource AccentTextBrush}" Stretch="Fill" Margin="2 2 3 3" Data="F1 M 20 0 L 20 20 L 0 20 L 0 0 Z " VerticalAlignment="Center" HorizontalAlignment="Center" />
                                <Path Name="PATH_CHECK" Fill="{DynamicResource AccentTextBrush}" Stretch="Fill" Margin="2 2 3 3" Data="F1 M 17.939453 5.439453 L 7.5 15.888672 L 2.060547 10.439453 L 2.939453 9.560547 L 7.5 14.111328 L 17.060547 4.560547 Z " VerticalAlignment="Center" HorizontalAlignment="Center" />
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter TargetName="PATH_UNCHECK" Property="Visibility" Value="Collapsed"></Setter>
                                <Setter TargetName="PATH_CHECK" Property="Visibility" Value="Visible"></Setter>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="False">
                                <Setter TargetName="PATH_UNCHECK" Property="Visibility" Value="Visible"></Setter>
                                <Setter TargetName="PATH_CHECK" Property="Visibility" Value="Collapsed"></Setter>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="PART_Background" Property="Opacity" Value="0.8"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=CardRealBorder,Path=IsMouseOver}" Value="True">
                    <Setter Property="Visibility" Value="Visible"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=PopupCardSettingMenu,Path=IsOpen}" Value="True">
                    <Setter Property="Visibility" Value="Visible"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding IsSelected}" Value="True">
                    <Setter Property="Visibility" Value="Visible"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>


        <Style x:Key="TextNameStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBlockBase}">
            <Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}"></Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=CardRealBorder,Path=IsMouseOver}" Value="True">
                    <Setter Property="Foreground" Value="{DynamicResource AccentTextBrush}"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=CardNameBarMask,Path=IsMouseOver}" Value="True">
                    <Setter Property="Foreground" Value="{DynamicResource AccentTextBrush}"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=PopupCardSettingMenu,Path=IsOpen}" Value="True">
                    <Setter Property="Foreground" Value="{DynamicResource AccentTextBrush}"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="GridNameStyle" TargetType="Grid">
            <Setter Property="Opacity" Value="0.1"></Setter>
            <Setter Property="Background" Value="Black"></Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=CardRealBorder,Path=IsMouseOver}" Value="True">
                    <Setter Property="Visibility" Value="Hidden"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=CardNameBarMask,Path=IsMouseOver}" Value="True">
                    <Setter Property="Visibility" Value="Hidden"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=PopupCardSettingMenu,Path=IsOpen}" Value="True">
                    <Setter Property="Visibility" Value="Hidden"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="ButtonSettingStyle" TargetType="Button" BasedOn="{StaticResource ButtonBarAccentStyle}">
            <Setter Property="Background" Value="{StaticResource AccentMidBrush}"></Setter>
            <Setter Property="Foreground" Value="{StaticResource AccentTextBrush}"></Setter>
            <Setter Property="Visibility" Value="Hidden"></Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=CardRealBorder,Path=IsMouseOver}" Value="True">
                    <Setter Property="Visibility" Value="Visible"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=PopupCardSettingMenu,Path=IsOpen}" Value="True">
                    <Setter Property="Visibility" Value="Visible"></Setter>
                    <Setter Property="Background" Value="{StaticResource PrimaryMidBrush}"></Setter>
                    <Setter Property="Foreground" Value="{StaticResource PrimaryTextBrush}"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>


        <Style x:Key="ListBoxSettingStyle" TargetType="{x:Type ListBox}">
            <Setter Property="FocusVisualStyle" Value="{x:Null}" />
            <Setter Property="Cursor" Value="Hand" />
            <Setter Property="Background" Value="{DynamicResource PrimaryMidBrush}" />
            <Setter Property="ItemContainerStyle">
                <Setter.Value>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="ListBoxItem">
                                    <Grid>
                                        <Border x:Name="GridContainer">
                                        </Border>
                                        <ContentPresenter x:Name="ContentPresenter"/>
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Setter.Value>
            </Setter>
            <Setter Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate DataType="model:ProtocolAction">
                        <Button Padding="10" Style="{StaticResource ButtonBarPrimaryStyle}" 
                                                                        Click="ServerMenuButton_OnClick"
                                                                        CommandParameter="{Binding}"
                                                                        HorizontalContentAlignment="Left"
                                                                        Content="{Binding ActionName}">
                        </Button>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Grid Width="{StaticResource CardWidth}" Height="{StaticResource CardWidth}" Clip="{StaticResource CardClip}">
        <!-- Card Content Middle Layer - with icon -->
        <Grid.InputBindings>
            <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding CmdConnServer}"></MouseBinding>
            <MouseBinding MouseAction="RightClick" Command="{Binding CmdEditServer}"></MouseBinding>
        </Grid.InputBindings>
        <Grid Name="CardContent" Background="{DynamicResource PrimaryMidBrush}" >
            <Grid.RowDefinitions>
                <RowDefinition Height="20"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="20"></RowDefinition>
            </Grid.RowDefinitions>

            <Grid Grid.Row="0" Opacity="0.4" Grid.RowSpan="3" 
                  Background="{Binding Server.ColorHex, Converter={StaticResource ConverterColorHexString2Brush}, Mode=OneWay}"></Grid>

            <Grid Grid.Row="0" Grid.RowSpan="3">
                <Image Source="{Binding Server.IconImg, Mode=OneWay}" 
                       RenderOptions.BitmapScalingMode="LowQuality"
                       Width="80" Height="80"
                       Stretch="UniformToFill">
                </Image>
            </Grid>

            <Grid Grid.Row="0" Grid.RowSpan="3">
                <TextBlock Name="TbServerType" 
                           FontWeight="Bold" Text="{Binding Server.ProtocolDisplayName, Mode=OneWay}" 
                           Width="40"
                           TextAlignment="Center"
                           Style="{StaticResource TextProtocolStyle}"
                           Opacity="0.3"
                           Foreground="{DynamicResource PrimaryTextBrush}"
                           HorizontalAlignment="Left" VerticalAlignment="Top"
                >
                    <TextBlock.FontSize>
                        <MultiBinding Converter="{StaticResource ConverterTextWidthAndContent2PresentationSize}" ConverterParameter="FontSize">
                            <Binding RelativeSource="{RelativeSource Self}" Path="Text"></Binding>
                            <Binding RelativeSource="{RelativeSource Self}" Path="Width"></Binding>
                            <Binding RelativeSource="{RelativeSource Self}" Path="FontFamily"></Binding>
                            <Binding RelativeSource="{RelativeSource Self}" Path="FontStyle"></Binding>
                            <Binding RelativeSource="{RelativeSource Self}" Path="FontWeight"></Binding>
                            <Binding RelativeSource="{RelativeSource Self}" Path="FontStretch"></Binding>
                        </MultiBinding>
                    </TextBlock.FontSize>
                </TextBlock>
            </Grid>

            <Path Grid.Row="0" Height="14" Width="14" Margin="3 3" 
                  UseLayoutRounding="True" 
                  Stretch="Uniform" 
                  Style="{StaticResource PathSourceStatusStyle}"
                  HorizontalAlignment="Center">
            </Path>
        </Grid>

        <!-- Card Border: Hover Button Layer -->
        <Border Name="CardRealBorder" CornerRadius="4" BorderThickness="2" Style="{StaticResource ButtonLayerStyle}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="20"></RowDefinition>
                </Grid.RowDefinitions>

                <CheckBox Width="20" Height="20" 
                          HorizontalAlignment="Left" VerticalAlignment="Top" 
                          Click="ItemsCheckBox_OnClick"
                          IsChecked="{Binding IsSelected}"
                          Background="{DynamicResource AccentMidBrush}"
                          Style="{StaticResource CheckBoxSelectedStyle}">
                </CheckBox>

                <ContentControl Grid.Row="0"
                                HorizontalAlignment="Right" VerticalAlignment="Top" 
                                Content="{Binding HoverNoteDisplayControl}"></ContentControl>

                <Grid Name="GridNameBar" Grid.Row="1" Background="{Binding ElementName=CardRealBorder,Path=BorderBrush}" >
                    <Grid HorizontalAlignment="Center" Margin="0 4 0 0">
                        <TextBlock VerticalAlignment="Center" FontSize="12" TextTrimming="CharacterEllipsis"
                                   Style="{StaticResource TextNameStyle}"
                                   Text="{Binding Server.DisplayName, Mode=OneWay}">
                        </TextBlock>
                    </Grid>
                    <Grid>
                        <!-- setting menu button -->
                        <Button x:Name="BtnSettingMenu"
                                Width="20" HorizontalAlignment="Right" 
                                BorderThickness="0"
                                Style="{StaticResource ButtonSettingStyle}"
                                Click="BtnSettingMenu_OnClick">
                            <Path Margin="4 5 4 2"
                                  Name="Symbol"
                                  Data="M 2.707031 0 L 49.97266 0 L 49.97266 5 L 2.707031 5 Z M 2.707031 20 L 49.97266 20 L 49.97266 25 L 2.707031 25 Z M 2.707031 40 L 49.97266 40 L 49.97266 45 L 2.707031 45 Z"
                                  Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType=Button}}"
                                  Stretch="Uniform" />
                        </Button>
                        <Popup Name="PopupCardSettingMenu"
                               AllowsTransparency="True"
                               PlacementTarget="{Binding ElementName=BtnSettingMenu}" StaysOpen="False">
                            <Border Padding="0 0 4 4">
                                <Grid MinWidth="80" Background="{DynamicResource PrimaryDarkBrush}">
                                    <ListBox ItemsSource="{Binding Actions}" 
                                             Style="{StaticResource ListBoxSettingStyle}"
                                             BorderThickness="0">
                                    </ListBox>
                                </Grid>
                            </Border>
                        </Popup>
                    </Grid>
                </Grid>
            </Grid>
        </Border>

        <!-- Card Title Mask Layer -->
        <Grid Name="CardNameBarMask" 
              Style="{StaticResource GridNameStyle}"
              VerticalAlignment="Bottom" 
              Height="20" >
        </Grid>
    </Grid>
</UserControl>
    