<?xml version="1.0" encoding="utf-8"?>
<UserControl x:Class="InvExplorer.PortfolioView" mc:Ignorable="d" d:DesignHeight="500" d:DesignWidth="600" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:InvExplorer">

  <Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
      <RowDefinition Height="auto" />
      <RowDefinition />
      <RowDefinition Height="auto" />
      <RowDefinition />
    </Grid.RowDefinitions>

    <!-- title -->
    <Border Padding="4 12">
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="portfolio" Style="{StaticResource _stTitle}" />
        <ComboBox VerticalAlignment="Bottom" MinWidth="200" Padding="8 4" ItemsSource="{Binding PortfoliosView}" DisplayMemberPath="Name" />
      </StackPanel>
    </Border>

    <!-- commands: add/remove items -->
    <Border Background="Black" HorizontalAlignment="Right">
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="+/-" Style="{StaticResource _stTitle}" />
        <local:SearchBox x:Name="_txtAddAsset" ItemsSource="{Binding SymbolNamesView}" SearchMemberPaths="Symbol,Name" DisplayMemberPath="Symbol" Style="{StaticResource _stAddBox}" />
        <HyperlinkButton Content="add" Command="{Binding AddAssetCommand}" CommandParameter="{Binding SelectedItem, ElementName=_txtAddAsset}" />
        <HyperlinkButton Content="remove selected" Command="{Binding RemoveAssetCommand}" CommandParameter="{Binding SelectedItems, ElementName=_fgPortfolio}" />
      </StackPanel>
    </Border>

    <!-- grid -->
    <c1:C1FlexGrid Name="_fgPortfolio" Grid.Row="1" FrozenColumns="1" ItemsSource="{Binding Path=PortfoliosView.CurrentItem.ItemsView}">
      <c1:C1FlexGrid.Columns>
        <c1:Column Binding="{Binding Symbol}" FontWeight="Bold" Width="160">
          <c1:Column.CellTemplate>
            <DataTemplate>
              <StackPanel Orientation="Horizontal">
                <local:ColorCell VerticalAlignment="Center" />
                <TextBlock Text="{Binding Symbol}" VerticalAlignment="Center" />
              </StackPanel>
            </DataTemplate>
          </c1:Column.CellTemplate>
        </c1:Column>
        <c1:Column Binding="{Binding Name}" MinWidth="280" Width="*" />
        <c1:Column Binding="{Binding LastPrice, StringFormat='#,##0.00;; '}" HeaderHorizontalAlignment="Right" />
        <c1:Column Binding="{Binding Change, StringFormat='#,##0.00;; '}" HeaderHorizontalAlignment="Right">
          <c1:Column.CellTemplate>
            <DataTemplate>
              <TextBlock Text="{Binding Change, StringFormat='#,##0.00;; '}" Foreground="{Binding Change, Converter={StaticResource _cvtNumberColor}}" Style="{StaticResource _tbCell}" />
            </DataTemplate>
          </c1:Column.CellTemplate>
        </c1:Column>
        <c1:Column Binding="{Binding ChangePercent}" Header="%" HeaderHorizontalAlignment="Right">
          <c1:Column.CellTemplate>
            <DataTemplate>
              <TextBlock Text="{Binding ChangePercent, StringFormat='#,##0.00%;; '}" Foreground="{Binding Change, Converter={StaticResource _cvtNumberColor}}" Style="{StaticResource _tbCell}" />
            </DataTemplate>
          </c1:Column.CellTemplate>
        </c1:Column>
        <c1:Column Binding="{Binding ShowChart, Mode=TwoWay}" Header="Chart" Background="{StaticResource _brEditableColumn}" HeaderHorizontalAlignment="Center" />
        <c1:Column Binding="{Binding Units, Mode=TwoWay, StringFormat='#,##0.00;; '}" Header="Shares" Background="{StaticResource _brEditableColumn}" HeaderHorizontalAlignment="Right" />
        <c1:Column Binding="{Binding PurchasePrice, Mode=TwoWay, StringFormat='#,##0.00;; '}" Header="Price" Background="{StaticResource _brEditableColumn}" HeaderHorizontalAlignment="Right" />
        <c1:Column Binding="{Binding CostBasis, StringFormat='#,##0.00;; '}" GroupAggregate="Sum" HeaderHorizontalAlignment="Right" />
        <c1:Column Binding="{Binding MarketValue, StringFormat='#,##0.00;; '}" Header="Mkt Value" GroupAggregate="Sum" HeaderHorizontalAlignment="Right" />
        <c1:Column Binding="{Binding Gain, StringFormat='#,##0.00;; '}" GroupAggregate="Sum" HeaderHorizontalAlignment="Right">
          <c1:Column.CellTemplate>
            <DataTemplate>
              <TextBlock Text="{Binding Gain, StringFormat='#,##0.00;; '}" Foreground="{Binding Gain, Converter={StaticResource _cvtNumberColor}}" Style="{StaticResource _tbCell}" />
            </DataTemplate>
          </c1:Column.CellTemplate>
        </c1:Column>
        <c1:Column Binding="{Binding GainPercent}" Header="%" HeaderHorizontalAlignment="Right">
          <c1:Column.CellTemplate>
            <DataTemplate>
              <TextBlock Text="{Binding GainPercent, StringFormat='#,##0.00%;; '}" Foreground="{Binding Gain, Converter={StaticResource _cvtNumberColor}}" Style="{StaticResource _tbCell}" />
            </DataTemplate>
          </c1:Column.CellTemplate>
        </c1:Column>
        <c1:Column Binding="{Binding DaysGain, StringFormat='#,##0.00;; '}" Header="Day's gain" GroupAggregate="Sum" HeaderHorizontalAlignment="Right">
          <c1:Column.CellTemplate>
            <DataTemplate>
              <TextBlock Text="{Binding DaysGain, StringFormat='#,##0.00;; '}" Foreground="{Binding DaysGain, Converter={StaticResource _cvtNumberColor}}" Style="{StaticResource _tbCell}" />
            </DataTemplate>
          </c1:Column.CellTemplate>
        </c1:Column>
        <c1:Column Binding="{Binding Gain, StringFormat='#,##0.00;; '}" Header="Return" GroupAggregate="Sum" HeaderHorizontalAlignment="Right">
          <c1:Column.CellTemplate>
            <DataTemplate>
              <TextBlock Text="{Binding Gain, StringFormat='#,##0.00;; '}" Foreground="{Binding Gain, Converter={StaticResource _cvtNumberColor}}" Style="{StaticResource _tbCell}" />
            </DataTemplate>
          </c1:Column.CellTemplate>
        </c1:Column>
      </c1:C1FlexGrid.Columns>
      <c1:C1FlexGrid.ColumnFooterRows>
        <c1:GroupRow />
      </c1:C1FlexGrid.ColumnFooterRows>
    </c1:C1FlexGrid>

    <!-- splitter between grid and chart -->
    <local:GridSplitter Grid.Row="2" HorizontalAlignment="Stretch" />

    <!-- chart -->
    <Grid Grid.Row="3" Margin="6">
      <local:PortfolioChart x:Name="_chart" Background="White" Portfolio="{Binding PortfoliosView.CurrentItem}" />
      <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="6" Background="White">
        <HyperlinkButton Content="YTD" Command="{Binding SetMinDate, ElementName=_chart}" CommandParameter="YTD" />
        <HyperlinkButton Content="6m" Command="{Binding SetMinDate, ElementName=_chart}" CommandParameter="6" />
        <HyperlinkButton Content="12m" Command="{Binding SetMinDate, ElementName=_chart}" CommandParameter="12" />
        <HyperlinkButton Content="24m" Command="{Binding SetMinDate, ElementName=_chart}" CommandParameter="24" />
        <HyperlinkButton Content="36m" Command="{Binding SetMinDate, ElementName=_chart}" CommandParameter="36" />
        <HyperlinkButton Content="All" Command="{Binding SetMinDate, ElementName=_chart}" CommandParameter="All" />
      </StackPanel>
      <local:ChartCursor Chart="{Binding ElementName=_chart}" />
    </Grid>

  </Grid>
</UserControl>