<UserControl x:Class="HistoryView.Controls.StatusPlot"
             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:HistoryView.Controls"
             xmlns:convert="clr-namespace:HistoryView.Converters"
             mc:Ignorable="d" 
             x:Name="This"
             d:DesignHeight="450" d:DesignWidth="800">

    <UserControl.Resources>
        <convert:Mini8StatusToBrush x:Key="Mini8Brush"/>
        <convert:Mini8StatusToDisconnect x:Key="DisconnectConverter"/>
        <convert:Mini8StatusToBrushMulti x:Key="Mini8Multi"/>
        <convert:AuthorityToVis x:Key="Authority"/>
    </UserControl.Resources>

    <Border Background="#e6e6e6" Width="auto" Height="auto" VerticalAlignment="Center" HorizontalAlignment="Center" BorderThickness="3"  CornerRadius="8">
        <Border.BorderBrush>
            <MultiBinding Converter="{StaticResource Mini8Multi}" ConverterParameter="Bright">
                <Binding ElementName="This" Path="DataSource.Status"/>
                <Binding ElementName="This" Path="DataSource.IsConnected" />
            </MultiBinding>
        </Border.BorderBrush>

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Border  CornerRadius="8,8,0,0" >
                <Border.BorderBrush>
                    <MultiBinding Converter="{StaticResource Mini8Multi}" ConverterParameter="Bright">
                        <Binding ElementName="This" Path="DataSource.Status"/>
                        <Binding ElementName="This" Path="DataSource.IsConnected" />
                    </MultiBinding>
                </Border.BorderBrush>
                <Border.Background>
                    <MultiBinding Converter="{StaticResource Mini8Multi}" ConverterParameter="Light">
                        <Binding ElementName="This" Path="DataSource.Status"/>
                        <Binding ElementName="This" Path="DataSource.IsConnected" />
                    </MultiBinding>
                </Border.Background>
                <Grid Margin="8,4">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto"/>
                        <ColumnDefinition Width="4"/>
                        <ColumnDefinition Width="auto"/>
                        <ColumnDefinition Width="4"/>
                        <ColumnDefinition Width="auto"/>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="auto"/>
                        <!--<ColumnDefinition Width="8"/>
                        <ColumnDefinition Width="auto"/>-->
                        <!--<ColumnDefinition Width="6"/>-->
                    </Grid.ColumnDefinitions>
                    <TextBlock FontSize="12" FontWeight="Bold" VerticalAlignment="Center" >
                        <!--<Run Text="{Binding ElementName=This, Path=DataSource.Index}"/>
                        <Run>-</Run>-->
                        <Run Text="{Binding ElementName=This, Path=DataSource.Name}"/>
                    </TextBlock>
                    <!--<TextBlock Grid.Column="2" VerticalAlignment="Center">
                         <Run Text="{Binding ElementName=This, Path=DataSource.Address}"/>
                         <Run>:</Run>
                         <Run Text="{Binding ElementName=This, Path=DataSource.Port}"/>
                    </TextBlock>-->

                    <!--<Button Grid.Column="4"  Background="Transparent" Command="{Binding ElementName=This, Path=Reconnect}" CommandParameter="{Binding ElementName=This, Path=ReconnectParameter}">
                        <Button.Visibility>
                            <MultiBinding Converter="{StaticResource DisconnectConverter}" >
                                <Binding ElementName="This" Path="DataSource.IsConnected"/>
                                <Binding ElementName="This" Path="DataSource.Enable" />
                            </MultiBinding>
                        </Button.Visibility>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Margin="4,0">Reconnect</TextBlock>
                        </StackPanel>
                    </Button>-->
                    <Image Grid.Column="4" x:Name="DisconnectIcon" VerticalAlignment="Center" Source="{StaticResource lan_disconnectDrawingImage}" Height="20">
                        <Image.Visibility>
                            <MultiBinding Converter="{StaticResource DisconnectConverter}" >
                                <Binding ElementName="This" Path="DataSource.IsConnected"/>
                                <Binding ElementName="This" Path="DataSource.Enable" />
                            </MultiBinding>
                        </Image.Visibility>
                    </Image>

                    <Button x:Name="FunctionButton" Click="FunctionButton_Click" Grid.Column="6" Background="Transparent" BorderBrush="Transparent" Width="60">
                        <Image Source="{StaticResource Icon_Setting}" Height="20" Margin="4,0"/>
                    </Button>
                    <Popup x:Name="FunctionPop" IsOpen="False" StaysOpen="False" Placement="Relative" PopupAnimation="Slide" PlacementTarget="{Binding ElementName=FunctionButton}" HorizontalOffset="-40" VerticalOffset="30"  AllowsTransparency="True">
                        <Border Width="auto" Height="auto" Background="{StaticResource BackgroundColor}" BorderBrush="{StaticResource DarkBorderColor}" BorderThickness="2" CornerRadius="8">
                            <Grid Margin=" 8">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="auto"/>
                                    <RowDefinition Height="16"/>
                                    <RowDefinition Height="auto"/>
                                    <RowDefinition Height="16"/>
                                    <RowDefinition Height="auto"/>
                                    <RowDefinition Height="16"/>
                                    <RowDefinition Height="auto"/>
                                    <RowDefinition Height="16"/>
                                    <RowDefinition Height="auto"/>
                                </Grid.RowDefinitions>
                                <TextBlock  VerticalAlignment="Center" HorizontalAlignment="Center">
                                     <Run Text="{Binding ElementName=This, Path=DataSource.Address}"/>
                                     <Run>:</Run>
                                     <Run Text="{Binding ElementName=This, Path=DataSource.Port}"/>
                                </TextBlock>

                                <Button Grid.Row="2" Style="{StaticResource FunctionButton}" Height="32"
                                        Click="Button_Click"
                                        Command="{Binding ElementName=This, Path=MultiSelectCommand}" 
                                        CommandParameter="{Binding ElementName=This, Path=DataSource}"
                                        Visibility="{Binding ElementName=This, Path=User.Authority, Converter={StaticResource Authority}, ConverterParameter=Operator}">
                                    MultiChannel Edit
                                </Button>
                                <Button Grid.Row="4" Style="{StaticResource FunctionButton}" Height="32"
                                        Command="{Binding ElementName=This, Path=TraceLogCommand}" 
                                        CommandParameter="{Binding ElementName=This, Path=DataSource}"
                                        Click="Button_Click">Dashboard</Button>

                                <Button Grid.Row="6" Style="{StaticResource FunctionButton}" Height="32"
                                        Command="{Binding ElementName=This, Path=MultiTraceCommand}" 
                                        CommandParameter="{Binding ElementName=This, Path=DataSource}"
                                        Click="Button_Click">Trace Log</Button>
                                <StackPanel Grid.Row="8" Orientation="Horizontal">
                                    <Button Grid.Row="8" Style="{StaticResource FunctionButton}" Background="{StaticResource NiceGreen}" Height="32" Width="80" Margin="0,0,8,0"
                                            Click="Button_Click_1">Connect</Button>
                                    <Button Grid.Row="10" Style="{StaticResource FunctionButton}" Background="{StaticResource DisableColor}" Height="32" Width="88"
                                            Click="Button_Click_2">Disconnect</Button>
                                </StackPanel>
                            </Grid>
                        </Border>
                    </Popup>
                    <!--<StackPanel Grid.Column="8" Orientation="Horizontal" VerticalAlignment="Center">
                        <CheckBox VerticalAlignment="Center" Style="{StaticResource SwitchCheckBox}"
                              IsChecked="{Binding ElementName=This, Path=DataSource.Enable, Mode=TwoWay}"
                              Visibility="{Binding ElementName=This, Path=User.Authority, Converter={StaticResource Authority}, ConverterParameter=Operator}"/>
                    </StackPanel>-->
                </Grid>
            </Border>

            <ItemsControl  Grid.Row="1"  Margin="4"  ItemsSource="{Binding ElementName=This, Path=Channels}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Style="{StaticResource ClearButton}" Background="Transparent"  CommandParameter="{Binding}"
                                Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:StatusPlot}, Path=EditChannel, Mode=TwoWay}">
                            <local:TempStatus Source="{Binding}" Margin="4,4"
                                              Mini8Status="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:StatusPlot},Path=DataSource.Status}"
                                              IsConnected="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:StatusPlot},Path=DataSource.IsConnected}"
                                              IsEnable="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:StatusPlot},Path=DataSource.Enable}"/>
                        </Button>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Rows="{Binding ElementName=This, Path=Rows}"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>

        </Grid>
    </Border>
</UserControl>
