<UserControl x:Class="MyPad.Views.Regions.MaintenanceContentView"
             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:i="http://schemas.microsoft.com/xaml/behaviors"
             xmlns:p="http://prismlibrary.com/"
             xmlns:m="http://metro.mahapps.com/winfx/xaml/controls"
             xmlns:mi="http://metro.mahapps.com/winfx/xaml/iconpacks"
             xmlns:le="http://wpflocalizeextension.codeplex.com"
             xmlns:qc="http://QuickConverter.CodePlex.com/"
             xmlns:ge="clr-namespace:SourceChord.GridExtra;assembly=GridExtra.Wpf"
             xmlns:lcw="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
             mc:Ignorable="d" 
             p:ViewModelLocator.AutoWireViewModel="True"
             d:DesignHeight="450" d:DesignWidth="800"
             Cursor="{qc:Binding '$P ? Cursors.Wait : Cursors.Arrow', P={Binding IsPending.Value, Mode=TwoWay}}">

    <i:Interaction.Triggers>
        <!--
        INFO: IsVisibleChanged を EventTrigger で拾えない問題への対応
        以下のように書いてもハンドリングできない。
        ``` <i:EventTrigger EventName="IsVisibleChanged"> ```
        これは仕様のためイベントの検知はあきらめて、プロパティの変化をトリガーにする。
        -->
        <i:PropertyChangedTrigger Binding="{Binding IsVisible, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Mode=OneWay}">
            <i:InvokeCommandAction Command="{Binding IsVisibleChangedHandler, Mode=OneTime}" PassEventArgsToCommand="True"/>
        </i:PropertyChangedTrigger>
    </i:Interaction.Triggers>

    <Grid Margin="20 -25 20 10" MaxWidth="{StaticResource Const.BreakPoint.XL}">
        <TabControl Padding="10 5 5 0" m:HeaderedControlHelper.HeaderFontWeight="Bold" Background="Transparent">
            <TabControl.Resources>
                <Style TargetType="{x:Type DataGrid}" BasedOn="{StaticResource Common.DataGrid}">
                    <Setter Property="Margin" Value="5"/>
                </Style>
                <Style TargetType="{x:Type Expander}" BasedOn="{StaticResource Common.Expander}">
                    <Setter Property="Margin" Value="5"/>
                </Style>
                <Style TargetType="{x:Type lcw:CartesianChart}" BasedOn="{StaticResource Common.CartesianChart}">
                    <Setter Property="Margin" Value="5"/>
                </Style>
            </TabControl.Resources>

            <!-- ********** ログビューワー ********** -->
            <TabItem Header="{le:Loc Label_LogViewer}">
                <Grid>
                    <TabControl TabStripPlacement="Bottom" Margin="0 0 0 15" m:HeaderedControlHelper.HeaderFontWeight="Bold" m:HeaderedControlHelper.HeaderFontSize="{DynamicResource MahApps.Font.Size.FloatingWatermark}">
                        <TabControl.Resources>
                            <Style TargetType="{x:Type DataGrid}" BasedOn="{StaticResource Common.DataGrid}">
                                <Setter Property="AutoGenerateColumns" Value="False"/>
                                <Setter Property="HeadersVisibility" Value="Row"/>
                                <Setter Property="RowHeaderWidth" Value="50"/>
                                <Setter Property="ColumnWidth" Value="*"/>
                            </Style>
                        </TabControl.Resources>
                        <TabItem Header="Info">
                            <DataGrid ItemsSource="{Binding SharedProperties.InfoLogs, Mode=OneWay}" LoadingRow="{qc:QuickEvent '$eventArgs.Row.Header = ($eventArgs.Row.GetIndex() + 1).ToString()'}">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Binding="{Binding Mode=OneWay}"/>
                                </DataGrid.Columns>
                            </DataGrid>
                        </TabItem>
                        <TabItem Header="Debug">
                            <DataGrid ItemsSource="{Binding SharedProperties.DebugLogs, Mode=OneWay}" LoadingRow="{qc:QuickEvent '$eventArgs.Row.Header = ($eventArgs.Row.GetIndex() + 1).ToString()'}">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Binding="{Binding Mode=OneWay}"/>
                                </DataGrid.Columns>
                            </DataGrid>
                        </TabItem>
                        <TabItem Header="Trace">
                            <DataGrid ItemsSource="{Binding SharedProperties.TraceLogs, Mode=OneWay}" LoadingRow="{qc:QuickEvent '$eventArgs.Row.Header = ($eventArgs.Row.GetIndex() + 1).ToString()'}">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Binding="{Binding Mode=OneWay}"/>
                                </DataGrid.Columns>
                            </DataGrid>
                        </TabItem>
                        <TabItem Header="Warn">
                            <DataGrid ItemsSource="{Binding SharedProperties.WarnLogs, Mode=OneWay}" LoadingRow="{qc:QuickEvent '$eventArgs.Row.Header = ($eventArgs.Row.GetIndex() + 1).ToString()'}">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Binding="{Binding Mode=OneWay}"/>
                                </DataGrid.Columns>
                            </DataGrid>
                        </TabItem>
                    </TabControl>

                    <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0 0 10 -5">
                        <Button Command="{Binding CollectLogsCommand, Mode=OneTime}" Style="{StaticResource Common.CircleButton}" ToolTip="{le:Loc Command_Refresh}" IsTabStop="False">
                            <mi:Codicons Kind="Refresh"/>
                        </Button>
                        <Border Width="10"/>
                        <Button Command="{Binding ExportLogArchiveCommand, Mode=OneTime}" Style="{StaticResource Common.CircleButton}" ToolTip="{le:Loc Command_ExportLog}" IsTabStop="False">
                            <mi:Material Kind="PackageDown"/>
                        </Button>
                    </StackPanel>
                </Grid>
            </TabItem>

            <!-- ********** パフォーマンス ********** -->
            <TabItem Header="{le:Loc Label_PerformanceMonitor}">
                <Grid ge:GridEx.RowDefinition="Auto, 5, *, 15, Auto, 5, *">
                    <Grid.Resources>
                        <Style TargetType="{x:Type lcw:LineSeries}">
                            <Setter Property="StrokeThickness" Value="2"/>
                            <Setter Property="LineSmoothness" Value="0"/>
                            <Setter Property="PointGeometrySize" Value="0"/>
                        </Style>
                        <Style TargetType="{x:Type lcw:Axis}">
                            <Setter Property="IsEnabled" Value="False"/>
                        </Style>
                    </Grid.Resources>

                    <StackPanel Grid.Row="0" Orientation="Horizontal">
                        <mi:PackIconBootstrapIcons Kind="Cpu" VerticalAlignment="Center" Margin="0 0 10 0"/>
                        <TextBlock Text="CPU usage"/>
                    </StackPanel>
                    <lcw:CartesianChart Grid.Row="2" Hoverable="False" DataTooltip="{x:Null}">
                        <lcw:CartesianChart.AxisX>
                            <!--a small visual improvement, lets hide the first points (x = 0, x=1) to get better animations-->
                            <lcw:Axis MinValue="2" ShowLabels="False"/>
                        </lcw:CartesianChart.AxisX>
                        <lcw:CartesianChart.AxisY>
                            <lcw:Axis MinValue="0" MaxValue="100"/>
                        </lcw:CartesianChart.AxisY>
                        <lcw:CartesianChart.Series>
                            <lcw:LineSeries Values="{Binding SharedProperties.CpuUsage, Mode=OneWay}" Stroke="{DynamicResource MahApps.Brushes.Highlight}">
                                <lcw:LineSeries.Fill>
                                    <SolidColorBrush Color="{DynamicResource MahApps.Colors.Highlight}" Opacity="0.2"/>
                                </lcw:LineSeries.Fill>
                            </lcw:LineSeries>
                        </lcw:CartesianChart.Series>
                    </lcw:CartesianChart>
                    
                    <StackPanel Grid.Row="4" Orientation="Horizontal">
                        <mi:PackIconFontAwesome Kind="MemorySolid" VerticalAlignment="Center" Margin="0 0 10 0"/>
                        <TextBlock Text="Memory usage"/>
                    </StackPanel>
                    <lcw:CartesianChart Grid.Row="6" Hoverable="False" DataTooltip="{x:Null}">
                        <lcw:CartesianChart.AxisX>
                            <!--a small visual improvement, lets hide the first points (x = 0, x=1) to get better animations-->
                            <lcw:Axis MinValue="2" ShowLabels="False"/>
                        </lcw:CartesianChart.AxisX>
                        <lcw:CartesianChart.AxisY>
                            <lcw:Axis MinValue="0"/>
                        </lcw:CartesianChart.AxisY>
                        <lcw:CartesianChart.Series>
                            <lcw:LineSeries Values="{Binding SharedProperties.MemoryUsage, Mode=OneWay}" Stroke="{DynamicResource MahApps.Brushes.Highlight}">
                                <lcw:LineSeries.Fill>
                                    <SolidColorBrush Color="{DynamicResource MahApps.Colors.Highlight}" Opacity="0.2"/>
                                </lcw:LineSeries.Fill>
                            </lcw:LineSeries>
                        </lcw:CartesianChart.Series>
                    </lcw:CartesianChart>
                </Grid>
            </TabItem>
        </TabControl>

        <Border d:IsHidden="True" Style="{StaticResource Common.Overlay}" Visibility="{qc:Binding '$P ? Visibility.Visible : Visibility.Collapsed', P={Binding IsPending.Value, Mode=OneWay}}">
            <StackPanel Margin="5 0" HorizontalAlignment="Stretch" VerticalAlignment="Center">
                <TextBlock Text="{le:Loc Text_Working}" HorizontalAlignment="Center" TextAlignment="Center"/>
                <Border Height="10"/>
                <m:ProgressRing/>
            </StackPanel>
        </Border>
    </Grid>

</UserControl>
