using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BS.Shared.DataContracts;
using DevExpress.Data;
using DevExpress.Utils;
using DevExpress.Xpf.Charts;
using DevExpress.Xpf.Grid;

namespace BeWo.View.Report.AnnualReport
{
    /// <summary>
    /// Interaktionslogik für AnnualReportChartControl.xaml
    /// </summary>
    public partial class AnnualReportChartControl : UserControl
    {
        public AnnualReportChartControl()
        {
            InitializeComponent();
        }

        private AnnualReportDC _Report;
        public AnnualReportDC AnnualReport
        {
            get { return _Report; }
            set 
            { 
                _Report = value;
                InitControl();

            }
        }

        private void InitControl()
        {
            groupBox.Header = _Report.Name;
            InitChart();
            InitGrid();
        }

        private void InitGrid()
        {
			 gridControl.Columns.Add(new GridColumn
		        {
			        FieldName = "Index",
					Header = "",
					Width = 50,
					SortOrder = ColumnSortOrder.Ascending,
					Visible = false
		        });
	        if (_Report.TableFieldNames != null && _Report.TableFieldNames.Length > 0)
	        {
		        for (int i = 0; i < _Report.TableFieldNames.Length; i++)
		        {
					gridControl.Columns.Add(new GridColumn
					{
						FieldName = _Report.TableFieldNames[i],
						Header = _Report.TableHeader[i],
						AllowResizing = DefaultBoolean.True
					});
		        }
	        }
	        else
	        {
		        gridControl.Columns.Add(new GridColumn
		        {
			        FieldName = "Name",
					Header = "",
					AllowResizing = DefaultBoolean.True
		        });
				gridControl.Columns.Add(new GridColumn
		        {
			        FieldName = "Value",
					Header = "Anzahl",
					AllowResizing = DefaultBoolean.True
		        });
				gridControl.Columns.Add(new GridColumn
		        {
			        FieldName = "Female",
					Header = "Weiblich",
					AllowResizing = DefaultBoolean.True
		        });
				gridControl.Columns.Add(new GridColumn
		        {
			        FieldName = "Male",
					Header = "Männlich",
					AllowResizing = DefaultBoolean.True
		        });
	        }
			
            gridControl.ItemsSource = _Report.Data;
        }

        private void InitChart()
        {
            series.Points.Clear();

            foreach (var data in _Report.Data)
            {
                if (data.Value.HasValue)
                {
                    SeriesPoint point = new SeriesPoint();
                    point.Argument = data.Name;
                    point.Value = (double)data.Value.Value;
                    series.Points.Add(point);
                }
            }
        }

        private void chart_CustomDrawSeriesPoint(object sender, CustomDrawSeriesPointEventArgs e)
        {
            e.LabelText = e.SeriesPoint.Argument;
            e.LegendText = String.Format("{0}: {1}", e.SeriesPoint.Argument, e.SeriesPoint.Value);
        }
    }
}
