using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using C1.Xaml.Chart;

namespace ChartSamples
{
  public sealed partial class MouseMarker : UserControl
  {
    public MouseMarker()
    {
      this.InitializeComponent();

      SampleData.CreateData(chart);
      chart.ChartType = ChartType.LineSymbols;
      chart.ManipulationMode = ManipulationModes.All;
      var pnl = new ChartPanel();

      var obj = new ChartPanelObject()
      {
        HorizontalAlignment = HorizontalAlignment.Right,
        VerticalAlignment = VerticalAlignment.Bottom
      };

      var bdr = new Border()
      {
        Background = new SolidColorBrush(Colors.Green) { Opacity = 0.4 },
        BorderBrush = new SolidColorBrush(Colors.Green),
        BorderThickness = new Thickness(1, 1, 3, 3),
        CornerRadius = new CornerRadius(6, 6, 0, 6),
        Padding = new Thickness(3)
      };
      //bdr.Padding = new Thickness(16, 16, 0, 0);

      var sp = new StackPanel();

      var tb1 = new TextBlock() { Foreground = new SolidColorBrush(Colors.White) };
      var bind1 = new Binding();
      bind1.Source = obj;
      //bind1.StringFormat = "x={0:#.##}";
      bind1.Path = new PropertyPath("DataPoint.X");
      tb1.SetBinding(TextBlock.TextProperty, bind1);

      var tb2 = new TextBlock() { Foreground = new SolidColorBrush(Colors.White) };
      var bind2 = new Binding();
      bind2.Source = obj;
      //bind2.StringFormat = "y={0:#.##}";
      bind2.Path = new PropertyPath("DataPoint.Y");
      tb2.SetBinding(TextBlock.TextProperty, bind2);

      sp.Children.Add(tb1);
      sp.Children.Add(tb2);

      bdr.Child = sp;

      obj.Content = bdr;
      obj.DataPoint = new Point();
      obj.Action = ChartPanelAction.MouseMove;

      // binding does not work in Win8 Consumer Preview
      obj.DataPointChanged += (s, e) =>
        {
          tb1.Text = obj.DataPoint.X.ToString("F2");
          tb2.Text = obj.DataPoint.Y.ToString("F2");
        };

      pnl.Children.Add(obj);

      chart.View.Layers.Add(pnl);

      cbAttach.ItemsSource = Utils.GetEnumValues(typeof(ChartPanelAttach));
      cbAttach.SelectedIndex = 0;
      cbAttach.SelectionChanged += (s, e) =>
      {
        obj.Attach =
          (ChartPanelAttach)Enum.Parse(typeof(ChartPanelAttach), cbAttach.SelectedItem.ToString(), true);
      };
    }
  }
}
