<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:vm="using:PortfolioManager.ViewModels"
             xmlns:vw="using:PortfolioManager.Views"
             xmlns:md="using:PortfolioManager.Models"
             xmlns:local="using:PortfolioManager.UIUtils"		
             xmlns:li="using:LoadingIndicators.Avalonia"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             xmlns:mxc="https://schemas.eremexcontrols.net/avalonia/charts"                      
             x:DataType="vm:CMTrendViewModel"
             x:Class="PortfolioManager.Views.CMTrendView"
             >
  <UserControl.Resources>
      <local:CurrencyValueConverter x:Key="CurrencyFormat"/>
      <local:DoubleValueConverter x:Key="DoubleFormat"/>
      <local:IntValueConverter x:Key="IntFormat"/>
      <local:DateValueConverter x:Key="DateFormat"/>
      <local:RMultipleValueConverter x:Key="RMultipleFormat"/>
  </UserControl.Resources>

  <Grid Background="LightGray">
  <li:LoadingIndicator ZIndex="1" IsActive="{Binding IsBusy}" Mode="Arcs" SpeedRatio="1.2" Width="200" Height="200"/>
    <DockPanel>
      <Grid DockPanel.Dock="Left" Margin="0,2,4,2">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="2*" />
          <ColumnDefinition Width="3" />
          <ColumnDefinition Width="11*" />
          <ColumnDefinition Width="3" />
          <ColumnDefinition Width="11*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="35" />
          <RowDefinition Height="3" />
          <RowDefinition Height="1*" />
          <RowDefinition Height="3" />
          <RowDefinition Height="2*" />
        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="5" FontSize="16" Text="{Binding Path=Title, Mode=OneWay}" HorizontalAlignment="Center"></TextBlock>              

        <StackPanel Orientation="Vertical" Grid.Row="2" Grid.RowSpan="3" Grid.Column="0" Margin="0,5,0,-5">
        <Label Content="Trade Date" HorizontalAlignment="Center" ></Label>
        <TextBox MinWidth="80" Height="24" IsReadOnly="true" Text="{Binding Path=TradeDate, Mode=OneWay}" />

        <Label Content="Parameters" HorizontalAlignment="Center" ></Label>        
        <ComboBox ItemsSource="{Binding Path=Parameters, Mode=OneWay}" SelectedItem="{Binding Path=SelectedParameter}"></ComboBox>
        <TextBox Height="24" MinWidth="80" HorizontalAlignment="Stretch" IsReadOnly="true" Text="{Binding Path=ParameterValue, Mode=OneWay}" />

        <Label Content="Tradeable Cash" HorizontalAlignment="Center" ></Label>
        <TextBox Height="24" MinWidth="80" HorizontalAlignment="Stretch" IsReadOnly="true" Text="{Binding Path=CashBalance, Mode=OneWay}" />

        <Label Content="Non-Tradeable Cash" HorizontalAlignment="Center" ></Label>
        <TextBox Height="24" MinWidth="80" HorizontalAlignment="Stretch" IsReadOnly="true" Text="{Binding Path=NonTradeableCash, Mode=OneWay}" />

        <Button Content="Load Trade File" HorizontalAlignment="Stretch" Command="{Binding Path=LoadFileCommand}"></Button>

        <Button Content="Reload" HorizontalAlignment="Stretch" Command="{Binding Path=ReloadCommand}" IsEnabled="{Binding Path=ReloadEnabled}"></Button>        

        <Button Content="{Binding Path=PercentButtonText}" HorizontalAlignment="Stretch" Command="{Binding Path=ToggleReturnOrPercentCommand}"></Button>

        <TextBox Background="WhiteSmoke" Focusable="false" Height="24" MinWidth="80" HorizontalAlignment="Stretch" IsReadOnly="true" 
         Text="{Binding Path=ModelExpectation, Mode=OneWay}" Foreground="{Binding Path=ExpectationColor}">
            <ToolTip.Tip>
                <Border Margin="2,1,2,1" Background="AntiqueWhite" BorderBrush="Black" BorderThickness="1" CornerRadius="15" Padding="5">
                    <TextBlock FontWeight="Normal" FontSize="12" Text="{Binding $parent[vw:CMTrendView].((vm:CMTrendViewModel)DataContext).ExpectationDescription}, Mode=OneWay" />
                </Border>
            </ToolTip.Tip>
        </TextBox>

        </StackPanel>

        <DockPanel x:Name="DockPanelA" Grid.Row="2" Grid.Column="2" Width="NaN" Height="NaN" >
<!-- RadGrid for Candidates             -->
          
        </DockPanel>
        <DockPanel x:Name="DockPanelB" Grid.Row="2" Grid.Column="4"  Width="NaN" Height="NaN" LastChildFill="False">
        <Grid>
        <Grid.RowDefinitions>
          <RowDefinition Height="24" />
          <RowDefinition Height="*" />
        </Grid.RowDefinitions>
          <TextBlock Grid.Row="0" FontSize="16" Text="{Binding Path=GraphTitle}"  HorizontalAlignment="Center"></TextBlock>            
            <mxc:CartesianChart Grid.Row="1">

              <mxc:CartesianChart.AxesY>
                  <mxc:AxisY Title="Gain/Loss" >
                      <mxc:AxisYRange AlwaysShowZeroLevel="False" />
                  </mxc:AxisY>
              </mxc:CartesianChart.AxesY>

              <mxc:CartesianChart.AxesX>
                  <mxc:AxisX Title="Date"/> 
              </mxc:CartesianChart.AxesX>   

                <mxc:CartesianChart.Series>
                    <mxc:CartesianSeries Name="PerformanceSeries" DataAdapter="{Binding Data.DataAdapter}" >
                    <mxc:CartesianLineSeriesView ShowInCrosshair="False" Color="MidnightBlue" MarkerSize="4" ShowMarkers="{Binding Path=ShowMarkers, Mode=TwoWay}" Thickness="2"/>                        
                    </mxc:CartesianSeries>
                </mxc:CartesianChart.Series>
            </mxc:CartesianChart>    
        </Grid>
        </DockPanel>

        <DockPanel x:Name="DockPanel2" Grid.Row="4" Grid.Column="2" Grid.ColumnSpan="3" Width="NaN" Height="NaN">
          <Grid>
            <Grid x:Name="PositionsView" >
              <DataGrid IsEnabled="true" Focusable="true" Margin="20" SelectedItem="{Binding Path=SelectedPosition, Mode=TwoWay}" ItemsSource="{Binding Path=AllPositions}"
                        IsReadOnly="True"
                        CanUserReorderColumns="True"
                        CanUserResizeColumns="True"
                        CanUserSortColumns="False"
                        GridLinesVisibility="All"
                        BorderThickness="1" BorderBrush="Gray">

                <DataGrid.ContextMenu>
                  <ContextMenu ItemsSource="{Binding PositionsMenuItems}">
                  </ContextMenu>
                </DataGrid.ContextMenu> 

                <DataGrid.Styles>
                  <Style Selector="DataGridRow:selected">
                      <Setter Property="FontWeight" Value="Bold" />
                      <Setter Property="Foreground" Value="Black" />
                      <Setter Property="Background" Value="White" />
                      <Setter Property="Opacity" Value=".50" />
                  </Style>
                 </DataGrid.Styles>                

                <DataGrid.Columns>
                  <DataGridTemplateColumn Header="Symbol"  >
                    <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding Symbol}" >
                            <ToolTip.Tip>
                            <Border Margin="2,1,2,1" Background="AntiqueWhite" BorderBrush="Black" BorderThickness="1" CornerRadius="15" Padding="5">
                              <TextBlock FontWeight="Normal" FontSize="12" Text="{Binding $parent[vw:CMTrendView].((vm:CMTrendViewModel)DataContext).CompanyDescriptionSelectedPosition}, Mode=OneWay"/>
                            </Border>
                          </ToolTip.Tip>
                          <ToolTip.ShowDelay>125</ToolTip.ShowDelay>                             
                        </TextBlock>
                      </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate/>                    
                  </DataGridTemplateColumn>

                  <DataGridTemplateColumn Header="Purchased">
                    <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding PurchaseDate,StringFormat='{}{0:MM/dd/yyyy}'}" Foreground="{Binding PurchaseDateColor}" />
                      </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate/>
                  </DataGridTemplateColumn>

                  <DataGridTemplateColumn Header="Sold">
                    <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding SellDate, StringFormat='{}{0:MM/dd/yyyy}'}" Foreground="{Binding SellDateColor}" />
                      </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate/>
                  </DataGridTemplateColumn>

                  <DataGridTextColumn Header="Shares" Binding="{Binding Shares, StringFormat='{}{0:N0}'}" />

                  <DataGridTemplateColumn Header="Exposure" >
                    <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding ActiveExposure, Converter={StaticResource CurrencyFormat},ConverterParameter=2}"  Foreground="{Binding ActiveExposureColor}" />
                      </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate/>
                  </DataGridTemplateColumn>

                  <DataGridTemplateColumn Header="Market Value" >
                    <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding ActiveMarketValue, Converter={StaticResource CurrencyFormat},ConverterParameter=2}" Foreground="{Binding ActiveMarketValueColor}" />
                      </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate/>
                  </DataGridTemplateColumn>

                  <DataGridTemplateColumn Header="GainLoss"  >
                    <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding GainLoss, Converter={StaticResource CurrencyFormat},ConverterParameter=2}"  Foreground="{Binding GainLossColor}" />
                      </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate/>
                  </DataGridTemplateColumn>

                  <DataGridTemplateColumn Header="GainLoss(%)"  >
                    <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding GainLossPcnt,StringFormat='{}{0:P3}'}"  Foreground="{Binding GainLossPcntColor}" />
                      </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate/>                    
                  </DataGridTemplateColumn>

                  <DataGridTextColumn Header="Days" Binding="{Binding DaysHeld}" />                        

                  <DataGridTemplateColumn Header="Purch.Price" >
                    <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding PurchasePrice, Converter={StaticResource CurrencyFormat},ConverterParameter=3}" >
                            <ToolTip.Tip>
                            <Border Margin="2,1,2,1" Background="AntiqueWhite" BorderBrush="Black" BorderThickness="1" CornerRadius="15" Padding="5">
                              <TextBlock FontWeight="Normal" FontSize="12" Text="{Binding $parent[vw:CMTrendView].((vm:CMTrendViewModel)DataContext).ToolTipPurchasePrice}, Mode=OneWay"/>
                            </Border>
                          </ToolTip.Tip>
                          <ToolTip.ShowDelay>125</ToolTip.ShowDelay>                            
                        </TextBlock>
                      </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate/>
                  </DataGridTemplateColumn>      

                  <DataGridTemplateColumn Header="CurrentPrice" >
                    <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding CurrentPrice, Converter={StaticResource CurrencyFormat},ConverterParameter=3}" Foreground="{Binding CurrentPriceColor}" >
                            <ToolTip.Tip>
                            <Border Margin="2,1,2,1" Background="AntiqueWhite" BorderBrush="Black" BorderThickness="1" CornerRadius="15" Padding="5">
                              <TextBlock FontWeight="Normal" FontSize="12" Text="{Binding $parent[vw:CMTrendView].((vm:CMTrendViewModel)DataContext).ToolTipCurrentPrice}, Mode=OneWay"/>
                            </Border>
                          </ToolTip.Tip>
                          <ToolTip.ShowDelay>125</ToolTip.ShowDelay>                            
                        </TextBlock>
                      </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate/>
                  </DataGridTemplateColumn>      

                  <DataGridTemplateColumn Header="Price Low" >
                    <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding Path=CurrentPriceLowAsString,StringFormat='{}{0:S}'}" Foreground="{Binding CurrentPriceLowAsStringColor}" >
                            <ToolTip.Tip>
                            <Border Margin="2,1,2,1" Background="AntiqueWhite" BorderBrush="Black" BorderThickness="1" CornerRadius="15" Padding="5">
                              <TextBlock FontWeight="Normal" FontSize="12" Text="{Binding $parent[vw:CMTrendView].((vm:CMTrendViewModel)DataContext).ToolTipCurrentPriceLow}, Mode=OneWay"/>
                            </Border>
                          </ToolTip.Tip>
                          <ToolTip.ShowDelay>125</ToolTip.ShowDelay>                            
                        </TextBlock>
                      </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate/>
                  </DataGridTemplateColumn>   

                  <DataGridTemplateColumn Header="Initial Stop"  >
                    <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding InitialStopLimit, Converter={StaticResource CurrencyFormat},ConverterParameter=2}"  Foreground="{Binding InitialStopLimitColor}" >
                            <ToolTip.Tip>
                            <Border Margin="2,1,2,1" Background="AntiqueWhite" BorderBrush="Black" BorderThickness="1" CornerRadius="15" Padding="5">
                              <TextBlock FontWeight="Normal" FontSize="12" Text="{Binding $parent[vw:CMTrendView].((vm:CMTrendViewModel)DataContext).ToolTipInitialStop}, Mode=OneWay"/>
                            </Border>
                          </ToolTip.Tip>
                          <ToolTip.ShowDelay>125</ToolTip.ShowDelay>                            
                        </TextBlock>
                      </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate/>                    
                  </DataGridTemplateColumn>          

                  <DataGridTemplateColumn Header="Trailing Stop">
                    <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding TrailingStopLimit, Converter={StaticResource CurrencyFormat},ConverterParameter=2}"  Foreground="{Binding TrailingStopLimitColor}" >
                            <ToolTip.Tip>
                            <Border Margin="2,1,2,1" Background="AntiqueWhite" BorderBrush="Black" BorderThickness="1" CornerRadius="15" Padding="5">
                              <TextBlock FontWeight="Normal" FontSize="12" Text="{Binding $parent[vw:CMTrendView].((vm:CMTrendViewModel)DataContext).ToolTipTrailingStop}, Mode=OneWay"/>
                            </Border>
                          </ToolTip.Tip>
                          <ToolTip.ShowDelay>125</ToolTip.ShowDelay>                            
                        </TextBlock>
                      </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate/>                    
                  </DataGridTemplateColumn>     

                  <DataGridTemplateColumn Header="R/Share" >
                    <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding R, Converter={StaticResource CurrencyFormat},ConverterParameter=2}"  Foreground="{Binding RColor}" >
                            <ToolTip.Tip>
                            <Border Margin="2,1,2,1" Background="AntiqueWhite" BorderBrush="Black" BorderThickness="1" CornerRadius="15" Padding="5">
                              <TextBlock FontWeight="Normal" FontSize="12" Text="{Binding $parent[vw:CMTrendView].((vm:CMTrendViewModel)DataContext).ToolTipR}, Mode=OneWay"/>
                            </Border>
                          </ToolTip.Tip>
                          <ToolTip.ShowDelay>125</ToolTip.ShowDelay>                            
                        </TextBlock>
                      </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate/>                    
                  </DataGridTemplateColumn>   

                  <DataGridTemplateColumn Header="Risk"  >
                    <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding TotalRiskExposure, Converter={StaticResource CurrencyFormat},ConverterParameter=2}"  Foreground="{Binding TotalRiskExposureColor}" >
                            <ToolTip.Tip>
                            <Border Margin="2,1,2,1" Background="AntiqueWhite" BorderBrush="Black" BorderThickness="1" CornerRadius="15" Padding="5">
                              <TextBlock FontWeight="Normal" FontSize="12" Text="{Binding $parent[vw:CMTrendView].((vm:CMTrendViewModel)DataContext).ToolTipTotalRiskExposure}, Mode=OneWay"/>
                            </Border>
                          </ToolTip.Tip>
                          <ToolTip.ShowDelay>125</ToolTip.ShowDelay>                            
                        </TextBlock>
                      </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate/>                      
                  </DataGridTemplateColumn>    

                  <DataGridTemplateColumn Header="RMultiple" >
                    <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding RMultipleAsString,StringFormat='{}{0:S}'}"  Foreground="{Binding RMultipleColor}" >
                            <ToolTip.Tip>
                            <Border Margin="2,1,2,1" Background="AntiqueWhite" BorderBrush="Black" BorderThickness="1" CornerRadius="15" Padding="5">
                              <TextBlock FontWeight="Normal" FontSize="12" Text="{Binding $parent[vw:CMTrendView].((vm:CMTrendViewModel)DataContext).ToolTipRMultiple}, Mode=OneWay"/>
                            </Border>
                          </ToolTip.Tip>
                          <ToolTip.ShowDelay>125</ToolTip.ShowDelay>                               
                        </TextBlock>
                      </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate/>                      
                  </DataGridTemplateColumn>       

                  <DataGridTemplateColumn Header="E-Ratio" >
                    <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                        <TextBlock Text="{Binding Path=EdgeRatioAsString,StringFormat='{}{0:S}'}"  Foreground="{Binding EdgeRatioAsStringColor}" >
                            <ToolTip.Tip>
                            <Border Margin="2,1,2,1" Background="AntiqueWhite" BorderBrush="Black" BorderThickness="1" CornerRadius="15" Padding="5">
                              <TextBlock FontWeight="Normal" FontSize="12" Text="{Binding $parent[vw:CMTrendView].((vm:CMTrendViewModel)DataContext).ToolTipEdgeRatio}, Mode=OneWay"/>
                            </Border>
                          </ToolTip.Tip>
                          <ToolTip.ShowDelay>125</ToolTip.ShowDelay>                               
                        </TextBlock>
                      </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate/>                      
                  </DataGridTemplateColumn>       

                  <DataGridTextColumn Header="Last Stop Adj." Binding="{Binding LastStopAdjustment, Converter={StaticResource DateFormat}}" />

                  <DataGridTextColumn Header="Days" Binding="{Binding DaysSinceLastStopAdjustment, Converter={StaticResource IntFormat},ConverterParameter=0}}" />                    
             
                  <DataGridTextColumn Header="Updated" Binding="{Binding LastUpdated, StringFormat='{}{0:MM/dd/yyyy HH:mm:ss}'}" />

                </DataGrid.Columns>
                
              </DataGrid>
            </Grid>
          </Grid>
        </DockPanel>
        <GridSplitter Background="LightBlue" ResizeDirection="Rows"  Grid.Column="2" Grid.ColumnSpan="5"  Grid.Row="3" Height="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0"/>
        <GridSplitter Grid.Row="1" Grid.Column="1" ResizeDirection="Columns" Grid.RowSpan="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="LightBlue" Width="3"/>
        <GridSplitter Grid.Row="1" Grid.Column="3" ResizeDirection="Columns" Grid.RowSpan="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="LightBlue" Width="3"/>
      </Grid>
    </DockPanel>
  </Grid>
</UserControl>
