<UserControl x:Class="ChatController.ChatMainControl"
             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" 
             xmlns:local="clr-namespace:ChatController"
             mc:Ignorable="d" x:Name="ChatControlling"
             d:DesignHeight="300" d:DesignWidth="300" >
    <Grid>
      
      <GroupBox Header="Chat" x:Name="rootGroupBox">
        <!--Style="{DynamicResource MainContentGroupBoxWithoutMaximizeBtnStyle}"-->
        <Grid   >
          <!--Background="{StaticResource ObjectEditBackgroundBrush}"-->
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2.7*" MaxWidth="250" />
            <ColumnDefinition Width="7.2*"/>
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
            <RowDefinition Height="10*"/>
          </Grid.RowDefinitions>


          <!-- ##########################  Linkes panel ########################### -->
          <Grid Grid.Column="0" Grid.Row="0" >
            <Grid.RowDefinitions>
              <RowDefinition  Height="0.8*"/>
              <RowDefinition  Height="9.20*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="10*"/>
            </Grid.ColumnDefinitions>

            <Grid Grid.Column="0" Grid.Row="0" >
              <Grid.RowDefinitions>
                <RowDefinition  Height="3*"/>
                <RowDefinition  Height="5*"/>
              </Grid.RowDefinitions>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="8*"/>
                <ColumnDefinition Width="2*"/>
              </Grid.ColumnDefinitions>

              <!--<StackPanel Grid.Row="0" Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Left" Visibility="Collapsed">
                <Label Content="Mein Status: " MaxWidth="80"/>
                <RadioButton GroupName="status" x:Name="RadioButtonOnline" BorderBrush="Green" MaxWidth="15" ToolTip="Online" VerticalAlignment="Center"  Foreground="Green"/>
                //Checked="ComboBox_SelectionChanged"
                <RadioButton GroupName="status" x:Name="RadioButtonOffline" BorderBrush="Red" MaxWidth="15" ToolTip="Offline" VerticalAlignment="Center"  Foreground="Red"/>
                //Checked="ComboBox_SelectionChanged"
                <RadioButton GroupName="status" x:Name="RadioButtonBeschaeftigt" BorderBrush="DarkOrange" MaxWidth="15" ToolTip="Beschäftigt" VerticalAlignment="Center"  Foreground="DarkOrange"/>
                //Checked="ComboBox_SelectionChanged"
              </StackPanel> / Style="{DynamicResource IconButtonStyle}"  /-->

              <StackPanel x:Name="ReloadGruppen" Grid.Row="0" Grid.Column="1" MinHeight="25" MaxWidth="25" HorizontalAlignment="Right" Orientation="Horizontal" VerticalAlignment="Bottom"  Visibility="Collapsed" Margin="0,-2,0,-2">
                <Button Background="Transparent" HorizontalAlignment="Right"  BorderThickness="0" VerticalAlignment="Center" VerticalContentAlignment="Center"  Width="25" Height="25" Click="ButtonReloadGruppen_OnClick" >
                  <Image ToolTip="Gruppen Aktualisieren"  Width="20" Height="20" Source="pack://application:,,,/ChatController;component/Ressourcen/SymbolRefresh32.png" RenderOptions.BitmapScalingMode="HighQuality" HorizontalAlignment="Center" />
                </Button>
              </StackPanel>

              <!--  <StackPanel Grid.Row="0" MaxWidth="20" HorizontalAlignment="Right" Orientation="Horizontal" VerticalAlignment="Bottom"  Visibility="Collapsed">
                <Button Style="{DynamicResource IconButtonStyle}" x:Name="ButtonSortLabel" Background="Transparent" BorderBrush="Transparent"  HorizontalAlignment="Right"  VerticalAlignment="Center"  Width="20" Height="20"  >
                  Click="ButtonSortLabel_OnClick"-->
              <!-- <Image x:Name="SortLabel" ToolTip="Sortierung ändern" Source="pack://application:,,,/Ressources/icons/unsortiert.png"   />
                </Button>
              </StackPanel>-->

              <!-- KeyDown="Suche_OnKeyDown" TextChanged="Suche_OnTextChanged"   -->
             <Label Grid.Row="0" Grid.Column="0"  MinHeight="25">Suche:</Label>
              <!-- <GroupBox Grid.Column="0" Grid.Row="1" Header="Suche:" Style="{x:Null}"> -->           
                <TextBox x:Name="Suche" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Margin="3,3,3,3" MinHeight="25" Height="25" VerticalContentAlignment="Center" TextChanged="Suche_OnTextChanged"  VerticalAlignment="Top" ToolTip="Suchen Sie hier nach einem User in der Liste"   >
                  <TextBox.Resources>
                    <Style TargetType="{x:Type Border}">
                      <Setter Property="CornerRadius" Value="4"/>
                      <Setter Property="Padding" Value="5,5,5,5" />
                    </Style>
                  </TextBox.Resources>
                </TextBox>
             <!-- </GroupBox> -->
            </Grid>


            <Grid Grid.Column="0" Grid.Row="1"  >
              <Grid.RowDefinitions>
                <RowDefinition  Height="1*"/>
              </Grid.RowDefinitions>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="10*"/>
              </Grid.ColumnDefinitions>
              
              <!--###################################### Liste aller User ###################################################### -->
              <ListView x:Name="Clientlist" HorizontalAlignment="Stretch" Grid.Row="0" Grid.Column="0"   VerticalAlignment="Stretch" Margin="3"  Style="{DynamicResource ResourceKey=styleListBox}" ItemTemplate="{DynamicResource DataTemplate1}" SelectionChanged="Clientlist_OnSelectionChanged" >
                <ListView.Resources>
                  <DataTemplate x:Key="DataTemplate1">
                    <Grid Margin="7,0,0,0" x:Name="GridMargin">
                      <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="35"/>
                        <ColumnDefinition Width="7"/>
                        <ColumnDefinition Width="120"/>
                        <ColumnDefinition Width="25"/>
                        <ColumnDefinition Width="12"/>
                      </Grid.ColumnDefinitions>


                      <Border BorderThickness="0.25" CornerRadius="3" Grid.RowSpan="2" Grid.ColumnSpan="5" Background="White"  >
                        <Border.BorderBrush>
                          <SolidColorBrush  Color="Black" />
                        </Border.BorderBrush>
                      </Border>

                      <Ellipse Width='30' Height='30' RenderOptions.BitmapScalingMode="HighQuality">
                        <Ellipse.Fill>
                          <ImageBrush ImageSource='{Binding Image}'  Stretch='Fill' RenderOptions.BitmapScalingMode="HighQuality" />
                        </Ellipse.Fill>
                      </Ellipse>

                      <!-- <StackPanel Grid.Column="1" Height="30"  > Online status anzeige 
                        <Ellipse Width='7' Height='7' Fill="{Binding Path=Color}" Margin="-23,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="Fill"  />
                      </StackPanel>-->


                      <Grid Grid.Column="2">
                        <Grid.RowDefinitions>
                          <RowDefinition Height="*" />
                          <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <StackPanel Grid.Row="0" Margin="1" >
                          <StackPanel Orientation="Horizontal" TextBlock.FontWeight="Bold" >
                            <TextBlock Text="{Binding Path=Name, FallbackValue=FirstName}" />
                          </StackPanel>
                          <!-- <TextBlock Text="{Binding Path=StatusTyp, FallbackValue=Message}" />-->
                        </StackPanel>

                        <!--Für die anzeige der anzahl ungelesener Nachrichten Height="30"-->
                        <StackPanel Margin="1" Grid.Row="1">
                          <TextBlock Text="{Binding Path=ReceiveMs}" FontWeight="Bold" FontSize="8"  Foreground="{Binding Path=Color}"  />
                        </StackPanel>
                      </Grid>

                      <StackPanel Grid.Column="4" Margin="7,0,0,0">
                        <Rectangle Fill="{Binding Path=ColorType}"  Stretch="Fill"  Height="30" Width="2" />
                      </StackPanel>

                    </Grid>
                  </DataTemplate>
                </ListView.Resources>

                <!--Uberschríft Mitarbeiter, ItemCount-->
                <ListView.GroupStyle>
                  <GroupStyle>
                    <GroupStyle.ContainerStyle>
                      <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                          <Setter.Value>
                            <ControlTemplate>
                              <Expander IsExpanded="True">
                                <Expander.Header>
                                  <StackPanel Orientation="Horizontal"  >
                                    <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="Black" FontSize="18" VerticalAlignment="Bottom" Width="148" />
                                    <TextBlock Text="{Binding ItemCount}" FontSize="12" Foreground="Black" FontWeight="Bold" Margin="10,0,0,1" VerticalAlignment="Bottom" HorizontalAlignment="Right" />
                                  </StackPanel>
                                </Expander.Header>
                                <ItemsPresenter />
                              </Expander>
                            </ControlTemplate>
                          </Setter.Value>
                        </Setter>
                      </Style>
                    </GroupStyle.ContainerStyle>
                  </GroupStyle>
                </ListView.GroupStyle>

                <!--   <ListView.ItemContainerStyle>                                          
                                                <Style TargetType="ListViewItem">
                                                  <Style.Triggers>
                                                    <Trigger Property="IsMouseOver" Value="True">
                                                      <Setter Property="Background" Value="White" />
                                                      <Setter Property="BorderBrush" Value="Transparent" />
                                                      <Setter Property="BorderThickness" Value="0" />
                                                    </Trigger>
                                                  </Style.Triggers>
                                                </Style>
                                              </ListView.ItemContainerStyle> -->
              </ListView>
            </Grid>
          </Grid>



          <!-- #######################  Rechtes Panel ################################-->
          <Grid Grid.Column="1" Grid.Row="0" x:Name="GridRechts">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="10*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
              <RowDefinition Height="0.80*"/>
              <RowDefinition Height="8.45*" />
              <RowDefinition Height="0.75*"/>
            </Grid.RowDefinitions>


            <ListBox x:Name="AktChatUser"  HorizontalAlignment="Left" Grid.Row="0"  Background="Transparent" BorderBrush="Transparent" VerticalAlignment="Bottom"  ItemTemplate="{DynamicResource DataTemplate2}" MouseDoubleClick="AktChatUser_OnMouseDoubleClick" >           
              <ListBox.Resources>
                <DataTemplate x:Key="DataTemplate2">
                  <Grid>
                    <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="50"/>
                      <ColumnDefinition Width="150"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                      <RowDefinition Height="10*" />
                    </Grid.RowDefinitions>
                   
                    <Ellipse Width='30' Height='30' VerticalAlignment="Center" RenderOptions.BitmapScalingMode="HighQuality">
                      <Ellipse.Fill >
                        <ImageBrush ImageSource='{Binding Image}'  Stretch='Fill'/>
                      </Ellipse.Fill>
                    </Ellipse>
                   
                    <StackPanel Grid.Column="1" Margin="5" VerticalAlignment="Center">
                      <StackPanel Orientation="Horizontal" TextBlock.FontWeight="Bold" >
                        <TextBlock Text="{Binding Path=Name, FallbackValue=FirstName}" />
                      </StackPanel>
                      <!-- <TextBlock Text="{Binding Path=StatusTyp, FallbackValue=Message}" />-->
                    </StackPanel>
                  </Grid>
                </DataTemplate>
              </ListBox.Resources>
            </ListBox>

            <ListBox x:Name="Chat" HorizontalAlignment="Stretch"  ScrollViewer.CanContentScroll="False" VerticalAlignment="Stretch" Margin="3" Grid.Row="1" VirtualizingStackPanel.IsVirtualizing="True" 
                     VirtualizingStackPanel.VirtualizationMode="Recycling" SelectionMode="Extended" MouseDoubleClick="Chat_OnMouseDoubleClick" ScrollViewer.ScrollChanged="Chat_OnScrollChanged"
                      ContextMenuOpening="Chat_OnContextMenuOpening">
              <!--MouseRightButtonDown="OnRightClickEvent"-->
              <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                  <Setter Property="HorizontalAlignment" Value="{Binding Posi}" />
                </Style>
              </ListBox.ItemContainerStyle>
              <ListBox.Resources>
                <Style TargetType="Border">
                  <Setter Property="CornerRadius" Value="3" />
                  <Setter Property="Padding" Value="10,0,0,0" />
                </Style>
              </ListBox.Resources>
              <ListBox.ItemTemplate>
                <DataTemplate>
                  <StackPanel>

                    <Border BorderBrush="{Binding ChatColor}" BorderThickness="1,1,1,1" CornerRadius="8,8,8,8" Background="{Binding ChatColor}" Visibility="{Binding MainVisibility}" >
                      <DockPanel MinHeight="15" Margin="5" LastChildFill="True" Background="{Binding ChatColor}" Visibility="{Binding MainVisibility}" >

                        <!-- Message Bereich  -->
                        <StackPanel DockPanel.Dock="Bottom" Visibility="{Binding ChatVisibility}" >
                          <TextBlock  VerticalAlignment="Center"  Text="{Binding UserMessage}" MinWidth="0" MaxWidth="500" TextWrapping="Wrap" />
                         <!--  <RichTextBox VerticalAlignment="Center" IsReadOnly="True"   BorderBrush="Transparent"  Background="Transparent" local:RichTextBoxHelper.DocumentXaml="{Binding UserMessage}" MinWidth="0" MaxWidth="500" />-->
                        </StackPanel>
                        
                        <!-- MEssageBereich mit Url verwaltung -->
                        <StackPanel DockPanel.Dock="Bottom"  Visibility="{Binding HyperlinkVisibility}" >
                          <TextBlock>
                            <Hyperlink  NavigateUri="{Binding UserMessage}" RequestNavigate="Hyperlink_OnRequestNavigate" >
                              <TextBlock  VerticalAlignment="Center" Text="{Binding UserMessage}" MinWidth="0" MaxWidth="500" TextWrapping="Wrap" /> 
                            </Hyperlink>
                          </TextBlock>
                          <!--  <RichTextBox VerticalAlignment="Center" IsReadOnly="True"   BorderBrush="Transparent"  Background="Transparent" local:RichTextBoxHelper.DocumentXaml="{Binding UserMessage}" MinWidth="0" MaxWidth="500" />-->
                        </StackPanel>

                        
                        <!-- Bild bereich-->
                        <DockPanel DockPanel.Dock="Bottom" Visibility="{Binding PictureVisibility}" >
                          <StackPanel Orientation="Vertical">
                            <Image  Stretch="Uniform" MaxHeight="200" MaxWidth="300" Source="{Binding ImageSources}" RenderOptions.BitmapScalingMode="HighQuality"  />

                            <TextBlock  Text="{Binding UserMessage}" VerticalAlignment="Center" HorizontalAlignment="Left" />  
                          </StackPanel>
                        </DockPanel>

                        <!-- Gif BEreich ??    <MediaElement Source="" ></MediaElement>  villeicht gif erstmal runterladen und dann den pfad ausgeben ?  200
                      <DockPanel DockPanel.Dock="Bottom" Visibility="{Binding GifPictureVisibility}"  >
                        <StackPanel Orientation="Vertical">
                          <MediaElement LoadedBehavior="Play" UnloadedBehavior="Manual" MediaFailed="MediaElement_OnMediaFailed" MediaEnded="MediaElement_OnMediaEnded" Source="{Binding UserMessage}" Stretch="Uniform" MaxHeight="200" MaxWidth="300"   >
                            <MediaElement.OpacityMask>
                              <ImageBrush ImageSource="{Binding UserMessage}"/>
                            </MediaElement.OpacityMask>
                          </MediaElement>
                          
                          <TextBlock  Text="{Binding UserMessage}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        </StackPanel>
                      </DockPanel>-->



                        <!--Dokument bereich -->
                        <DockPanel DockPanel.Dock="Bottom" Visibility="{Binding DokumentVisibility}" >
                          <StackPanel Orientation="Vertical">
                              <Image ToolTip="Dokument öffnen" Height="100" Width="150" Source="pack://application:,,,/ChatController;component/Ressourcen/ClipboardDisabled.png" Stretch="Uniform" VerticalAlignment="Center" HorizontalAlignment="Center" />
                              <TextBlock Margin="2,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding UserMessage}"/>
                              <Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="Dokument Anzeigen"  Height="25"  Tag="{Binding Dokpfad}" Click="OpenDokument_OnClick " >
                                <Button.Resources>
                                  <Style TargetType="{x:Type Border}">
                                    <Setter Property="CornerRadius" Value="3"/>
                                    <Setter Property="Padding" Value="1,1,1,1" />
                                  </Style>
                                </Button.Resources>
                              </Button>
                          </StackPanel>                         
                        </DockPanel>

                        
                        
                        <StackPanel DockPanel.Dock="Left"  Margin="0,0,0,5" >
                          <TextBlock Text="{Binding Username}" FontWeight="Bold" MaxWidth="150" HorizontalAlignment="Left"  TextAlignment="Left" VerticalAlignment="Center"/>
                        </StackPanel>

                        <StackPanel DockPanel.Dock="Right" Margin="5,0,0,5" >
                          <TextBlock Text="{Binding SendTime}"  MaxWidth="350"   HorizontalAlignment="Right" TextAlignment="Right" VerticalAlignment="Center"/>
                        </StackPanel>

                      </DockPanel>
                    </Border>
                  </StackPanel>
                </DataTemplate>
              </ListBox.ItemTemplate>
            </ListBox>


            <Grid Grid.Row="2" Margin="3,0,3,3" >
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="8.5*"/>
                <ColumnDefinition Width=".5*"/>
                <ColumnDefinition Width="1*"/>
              </Grid.ColumnDefinitions>
              <Grid.RowDefinitions>
                <RowDefinition Height="10*"/>
              </Grid.RowDefinitions>

              <Grid Grid.Column="0" Grid.ColumnSpan="2" MinHeight="25">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="9*"/>
                  <ColumnDefinition Width="30"/>              
                  <ColumnDefinition Width="30"/>
                </Grid.ColumnDefinitions>

                <TextBox  x:Name="Chatbox" HorizontalAlignment="Stretch" Grid.Column="0" Grid.ColumnSpan="3" TextWrapping="Wrap" Text="Nachricht schreiben"  VerticalScrollBarVisibility="Hidden" MaxLines="5" MaxHeight="70" Visibility="Visible"
                   VerticalContentAlignment="Center" MinHeight="25" Padding="10,0,60,0" ToolTip="Schreiben Sie hier eine Nachricht hinein"  Foreground="DarkGray" VerticalAlignment="Bottom" GotFocus="Chatbox_OnGotFocus" KeyDown="Chatbox_OnKeyDownHandler">
                  <TextBox.Resources>
                    <Style TargetType="{x:Type Border}">
                      <Setter Property="CornerRadius" Value="3"/>
                      <!-- <Setter Property="Padding" Value="10,0,60,0" />-->
                    </Style>
                  </TextBox.Resources>
                </TextBox>

                <!-- StackPanel Zur test zwecken  Style="{x:Null}"-->
                <StackPanel Grid.Column="1" Grid.ColumnSpan="2" Margin="1" Orientation="Horizontal"> 
                <Button Grid.Column="1" Margin="1,0,0,0" x:Name="MediaButton"  Background="Transparent" Width="25" BorderThickness="0"  VerticalAlignment="Bottom" ToolTip="Datei einfügen" Click="MediaButton_OnClick" >
                  <Image Height="15" Width="15" Source='pack://application:,,,/ChatController;component/Ressourcen/paperclip2.png' Margin="3" RenderOptions.BitmapScalingMode="HighQuality" HorizontalAlignment="Center"/>  
                </Button>
                
                <Button Grid.Column="2" Width="25"  BorderThickness="0"  Background="Transparent" x:Name="EmojiButton" Margin="2,0,0,0" VerticalAlignment="Bottom" ToolTip="Emoji einfügen" Click="EmojiButton_OnClick">
                  <Image Height="15" Width="15" Source='pack://application:,,,/ChatController;component/Ressourcen/glucklicher.png' Margin="3"  RenderOptions.BitmapScalingMode="HighQuality" HorizontalAlignment="Center" />              
                </Button>
                </StackPanel>

                <!-- Stackpanel zu 12 testZwecken -->
                
              </Grid>

              <Button x:Name="SendButton" Content="Senden" HorizontalAlignment="Stretch" Margin="2,0,0,0" Grid.Column="2" VerticalAlignment="Bottom" Click="SendButton_OnClick" Background="#ff5e00" Height="25" >
                <Button.Resources>
                  <Style TargetType="{x:Type Border}">
                    <Setter Property="CornerRadius" Value="3"/>
                    <Setter Property="Padding" Value="1,1,1,1" />
                  </Style>
                </Button.Resources>
              </Button>

            </Grid>
          </Grid>
        </Grid>
      </GroupBox>
    </Grid>
</UserControl>
