<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:GainLossViewModel"
             x:Class="PortfolioManager.Views.GainLossView"
             >
  <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 Margin="4">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto" />
          <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="10*" />
          <RowDefinition Height="*" />
        </Grid.RowDefinitions>
					<StackPanel Grid.Row="0" Grid.Column="0" Orientation="Vertical">
						<Label Content="Account" HorizontalAlignment="Center" ></Label>
            <ComboBox ItemsSource="{Binding Path=Accounts, Mode=OneTime}" SelectedItem="{Binding Path=SelectedAccount, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" >
							<!-- <ComboBox.ItemsPanel>
								<ItemsPanelTemplate>
									<VirtualizingStackPanel/>
								</ItemsPanelTemplate>
							</ComboBox.ItemsPanel> -->
            </ComboBox>
						<Label Content="Symbols" HorizontalAlignment="Center" ></Label>
						<ComboBox ItemsSource="{Binding Path=Symbols, Mode=OneTime}" SelectedItem="{Binding Path=SelectedSymbol, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}">
							<!-- <ComboBox.ItemsPanel>
								<ItemsPanelTemplate>
									<VirtualizingStackPanel/>
								</ItemsPanelTemplate>
							</ComboBox.ItemsPanel> -->
            </ComboBox>
						<Label Content="Total Gain/Loss" HorizontalAlignment="Center" ></Label>
            <TextBox Height="24" MinWidth="80" HorizontalAlignment="Stretch" IsReadOnly="true" Text="{Binding Path=TotalGainLoss, Mode=OneWay}" />
						<CheckBox Content="Include Dividends" IsChecked="{Binding Mode=TwoWay,Path=CheckBoxIncludeDividends}" HorizontalAlignment="Stretch" />

            <Label Content="Summary" HorizontalAlignment="Center" ></Label>
            <Border Margin="2,1,2,1" Background="LightGray" BorderBrush="Black" BorderThickness="1" CornerRadius="5" Padding="5">
              <StackPanel Orientation="Vertical">
                <Label Content="Selected Date" HorizontalAlignment="Center" ></Label>
                <TextBox Height="24" MinWidth="80" HorizontalAlignment="Stretch" IsReadOnly="true" Text="{Binding Path=SummaryDate, Mode=OneWay}" />
                <Label Content="$ Change" HorizontalAlignment="Center" ></Label>
                <TextBox Height="24" MinWidth="80" HorizontalAlignment="Stretch" IsReadOnly="true" Text="{Binding Path=SummaryChange, Mode=OneWay}" />
                <Label Content="Current G/L" HorizontalAlignment="Center" ></Label>
                <TextBox Height="24" MinWidth="80" HorizontalAlignment="Stretch" IsReadOnly="true" Text="{Binding Path=SummaryGainLoss, Mode=OneWay}" />
                <Label Content="Positions" HorizontalAlignment="Center" ></Label>
                <TextBox Height="24" MinWidth="80" HorizontalAlignment="Stretch" IsReadOnly="true" Text="{Binding Path=SummaryPositions, Mode=OneWay}" />
              </StackPanel>
            </Border>

						<Button Content="{Binding Path=PercentButtonText}" HorizontalAlignment="Stretch" Command="{Binding Path=ToggleReturnOrPercentCommand}"></Button>
						<Button Content="{Binding Path=ActiveTotalButtonText}" HorizontalAlignment="Stretch" Command="{Binding Path=ToggleActiveOrTotalCommand}"></Button> 
						<Button Content="Refresh" HorizontalAlignment="Stretch" Command="{Binding Path=PerformRefreshCommand}"/>
            <Button Content="Reset" HorizontalAlignment="Stretch" Command="{Binding Path=PerformResetCommand}"/>
          </StackPanel>
				<Grid Grid.Column="1" Grid.RowSpan="2" >
        <Grid.RowDefinitions>
          <RowDefinition Height=".7*" />
          <RowDefinition Height="2" />
          <RowDefinition Height=".5*" />
        </Grid.RowDefinitions>

					<GridSplitter Background="LightBlue" ResizeDirection="Rows" Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="1" Width="NaN" Height="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0"/>

            <Border CornerRadius="6" BorderBrush="WhiteSmoke" Background="LightGray" BorderThickness="2" Padding="8">
            <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.Series>
                        <mxc:CartesianSeries Name="GainLossSeries" 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.Series>
                        <mxc:CartesianSeries Name="MA21" DataAdapter="{Binding MA21.DataAdapter}" >
                        <mxc:CartesianLineSeriesView ShowInCrosshair="False" Color="Green" MarkerSize="4" ShowMarkers="{Binding Path=ShowMarkers, Mode=TwoWay}" Thickness="2"/>                        
                        </mxc:CartesianSeries>
                    </mxc:CartesianChart.Series>

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

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

                </mxc:CartesianChart>                
            </Grid>
            </Border>

					<Grid Grid.Row="2" Grid.Column="1" >
							<Grid.ColumnDefinitions>
								<ColumnDefinition Width="7*" />
								<ColumnDefinition Width="3" />
								<ColumnDefinition Width="7*" />
							</Grid.ColumnDefinitions>
							<Grid.RowDefinitions>
								<RowDefinition Height="*" />
							</Grid.RowDefinitions>

<!-- Grid GainLossSummary (Left) Goes Here Column 0-->
              <Grid Grid.Row="0" Grid.Column="0" Background="WhiteSmoke"> 
                <Grid.RowDefinitions>
                  <RowDefinition Height="*" />
                </Grid.RowDefinitions>

              <DataGrid IsEnabled="true" Focusable="true" Margin="20" Background="WhiteSmoke"
                ItemsSource="{Binding Path=GainLossCompoundModelCollection,Mode=OneWay}"
                SelectedItem="{Binding SelectedGainLossCompoundItem, Mode=TwoWay}" 
                IsReadOnly="True"
                CanUserReorderColumns="True"
                CanUserResizeColumns="True"
                CanUserSortColumns="False"
                GridLinesVisibility="All"
                BorderThickness="1" BorderBrush="Gray">

                <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="Date" >
                        <DataGridTemplateColumn.CellTemplate>
                          <DataTemplate>
                            <TextBlock Text="{Binding Date,StringFormat='{}{0:MM/dd/yyyy}'}" />
                          </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                      <DataGridTemplateColumn.CellEditingTemplate/>
                      </DataGridTemplateColumn>

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

                      <!-- <DataGridTemplateColumn Header="Dividends" >
                        <DataGridTemplateColumn.CellTemplate>
                          <DataTemplate>
                            <TextBlock Text="{Binding Path=TotalDividendsPaid,StringFormat='{}{0:C}'}" />
                          </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                      <DataGridTemplateColumn.CellEditingTemplate/>
                      </DataGridTemplateColumn> -->

                      <DataGridTemplateColumn Header="Active G/L" >
                        <DataGridTemplateColumn.CellTemplate>
                          <DataTemplate>
                            <TextBlock Text="{Binding ActiveGainLoss, Converter={StaticResource CurrencyFormat},ConverterParameter=2}" />
                          </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                      <DataGridTemplateColumn.CellEditingTemplate/>
                      </DataGridTemplateColumn>

                      <DataGridTemplateColumn Header="Active G/L(%)" >
                        <DataGridTemplateColumn.CellTemplate>
                          <DataTemplate>
                            <TextBlock Text="{Binding Path=ActiveGainLossPercent,StringFormat='{}{0:N3}'}" />
                          </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                      <DataGridTemplateColumn.CellEditingTemplate/>
                      </DataGridTemplateColumn>

                      <DataGridTemplateColumn Header="Total G/L" >
                        <DataGridTemplateColumn.CellTemplate>
                          <DataTemplate>
                            <TextBlock Text="{Binding Path=TotalGainLoss, Converter={StaticResource CurrencyFormat},ConverterParameter=2}" />
                          </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                      <DataGridTemplateColumn.CellEditingTemplate/>
                      </DataGridTemplateColumn>

                      <DataGridTemplateColumn Header="Total G/L(%)">
                        <DataGridTemplateColumn.CellTemplate>
                          <DataTemplate>
                            <TextBlock Text="{Binding Path=TotalGainLossPercent,StringFormat='{}{0:N3}'}" />
                          </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                      <DataGridTemplateColumn.CellEditingTemplate/>
                      </DataGridTemplateColumn>

                    </DataGrid.Columns>
                  </DataGrid>
                </Grid>
<!-- Grid GainLossSummary (Right) Goes Here Column 2-->
              <Grid Grid.Row="0" Grid.Column="2" Background="WhiteSmoke"> 
                <Grid.RowDefinitions>
                  <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                  <DataGrid IsEnabled="true" Focusable="true" Margin="20" Background="WhiteSmoke"
                    ItemsSource="{Binding Path=GainLossSummaryItemCollection,Mode=OneWay}"
                    SelectedItem="{Binding SelectedGainLossSummaryItem, Mode=TwoWay}" 
                    IsReadOnly="True"
                    CanUserReorderColumns="True"
                    CanUserResizeColumns="True"
                    CanUserSortColumns="False"
                    GridLinesVisibility="All"
                    BorderThickness="1" BorderBrush="Gray">

                  <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.ContextMenu>
                    <ContextMenu ItemsSource="{Binding GainLossMenuItems}">
                    </ContextMenu>
                  </DataGrid.ContextMenu>                  

                    <DataGrid.Columns>

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

                      <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:GainLossView].((vm:GainLossViewModel)DataContext).Parity}, Mode=OneWay"/>
                                </Border>
                              </ToolTip.Tip>
                              <ToolTip.ShowDelay>125</ToolTip.ShowDelay> 
                            </TextBlock>
                          </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                      <DataGridTemplateColumn.CellEditingTemplate/>
                      </DataGridTemplateColumn>                      
 
                      <DataGridTemplateColumn Header="Current G/L" >
                        <DataGridTemplateColumn.CellTemplate>
                          <DataTemplate>
                            <TextBlock Text="{Binding CurrentGainLoss, Converter={StaticResource CurrencyFormat},ConverterParameter=2}" />
                          </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                      <DataGridTemplateColumn.CellEditingTemplate/>
                      </DataGridTemplateColumn>                      

                      <DataGridTemplateColumn Header="Previous G/L" >
                        <DataGridTemplateColumn.CellTemplate>
                          <DataTemplate>
                            <TextBlock Text="{Binding PreviousGainLoss, Converter={StaticResource CurrencyFormat},ConverterParameter=2}" />
                          </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                      <DataGridTemplateColumn.CellEditingTemplate/>
                      </DataGridTemplateColumn>       

                      <DataGridTemplateColumn Header="$ Change" >
                        <DataGridTemplateColumn.CellTemplate>
                          <DataTemplate>
                            <TextBlock Text="{Binding Path=Change, Converter={StaticResource CurrencyFormat},ConverterParameter=2}"/>                       
                          </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                      <DataGridTemplateColumn.CellEditingTemplate/>
                      </DataGridTemplateColumn>  

                      <DataGridTemplateColumn Header="$ Change(%)" >
                        <DataGridTemplateColumn.CellTemplate>
                          <DataTemplate>
                            <TextBlock Text="{Binding Path=ChangePercent,StringFormat='{}{0:N3}'}">
                               <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:GainLossView].((vm:GainLossViewModel)DataContext).DollarChangePercent}, Mode=OneWay"/>
                                </Border>
                              </ToolTip.Tip>
                              <ToolTip.ShowDelay>125</ToolTip.ShowDelay> 
                            </TextBlock>
                          </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                      <DataGridTemplateColumn.CellEditingTemplate/>
                      </DataGridTemplateColumn>                                        

                    </DataGrid.Columns>
                  </DataGrid>
                </Grid>

							<GridSplitter Background="LightBlue" ResizeDirection="Columns" Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0"/>
						</Grid>
				</Grid>
				</Grid>
			</DockPanel>
		</Grid>
</UserControl>
