using System.ComponentModel.Composition;
using System.Windows;
using TaskIt.Core;
using TaskIt.Modules.Dashboard.ViewModels;

namespace TaskIt.Modules.Dashboard.Views
{
    public partial class TasksOverview
    {
        public TasksOverview()
        {
            InitializeComponent();
            CompositionInitializer.SatisfyImports(this);
            DataContext = ViewModel;
            Loaded += OnLoaded;            

            ViewModel.DataLoaded += OnDataLoaded;
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            var brushes = ViewModel.Brushes;

            chtData.PaletteBrushes.Clear();
            foreach (var brush in brushes)
            {
                chtData.PaletteBrushes.Add(brush);
            }            

            //// NOTE: Because the data for this chart is coming from an ISingleResult in WCF RIA Services, I have to create a custom legend
            var legend = chtData.DefaultView.ChartLegend;
            legend.UseAutoGeneratedItems = false;

            var item1 = Utils.GetChartLegendItem(ViewModel.ResourceLegendItemOverdue, brushes[0]);
            legend.Items.Add(item1);
            var item2 = Utils.GetChartLegendItem(ViewModel.ResourceLegendItemDueThisWeek, brushes[1]);
            legend.Items.Add(item2);
            var item3 = Utils.GetChartLegendItem(ViewModel.ResourceLegendItemDueThisMonth, brushes[2]);
            legend.Items.Add(item3);
            var item4 = Utils.GetChartLegendItem(ViewModel.ResourceLegendItemCompletedThisWeek, brushes[3]);
            legend.Items.Add(item4);
            var item5 = Utils.GetChartLegendItem(ViewModel.ResourceLegendItemCompletedThisMonth, brushes[4]);
            legend.Items.Add(item5);

            // This is a hack to hide the X axis label that displays "1"
            chtData.DefaultView.ChartArea.AxisX.TickPoints[0].Label = " ";
        }

        void OnDataLoaded(object sender, RoutedEventArgs e)
        {
            // This is a hack to hide the X axis label that displays "1"
            chtData.DefaultView.ChartArea.AxisX.TickPoints[0].Label = " ";
        }

        [Import]
        public TasksOverviewViewModel ViewModel { get; set; }
    }
}