using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Windows.Controls.DataVisualization.Charting;

namespace SLControlsInfoV3
{
    public partial class MainPage : UserControl
    {

        IEnumerable<ControlInfo> controls;
        IEnumerable<ControlInfo> filteredControls;

        public MainPage()
        {
            InitializeComponent();
        }

        private void NamespacesPie_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }


        private void GetControls()
        {
            //Load the ControlDetails.xml file from the Data folder in the .xap file...
            XDocument doc = XDocument.Load("Data/ControlDetails.xml");
            controls = from c in doc.Root.Elements("Control")
                       select new ControlInfo()
                       {
                           ControlID = int.Parse(c.Attribute("ControlID").Value),
                           ControlName = c.Attribute("ControlName").Value,
                           IsInCommonSilverlightControlsToolbox = bool.Parse(c.Attribute("IsInCommonSilverlightControlsToolbox").Value),
                           IsInAllSilverlightControlsToolbox = bool.Parse(c.Attribute("IsInAllSilverlightControlsToolbox").Value),
                           NamespaceName = c.Attribute("NamespaceName").Value,
                           AssemblyName = c.Attribute("AssemblyName").Value,
                           AssemblyPath = c.Attribute("AssemblyPath").Value
                       };
        }

        private void ShowControlData()
        {
            ShowControlData("", "");
        }

        private void ShowControlData(string filterby, string value)
        {

            if (filterby.ToLower().Trim() == "assemblyname")
            {
                filteredControls =
                    from c in controls
                    where c.AssemblyName == value
                    select c;
            }
            else if (filterby.ToLower().Trim() == "namespacename")
            {
                filteredControls =
                    from c in controls
                    where c.NamespaceName == value
                    select c;
            }
            else
            {
                filteredControls = controls;
            }
            ControlDetailsGrid.ItemsSource = filteredControls;
        }

        private void PopulateNamespacesChart()
        {
            IEnumerable<KeyCount> namespaces = from c in controls
                                               group c by c.NamespaceName into n
                                               orderby n.Count() descending
                                               select new KeyCount()
                                               {
                                                   Key = n.Key,
                                                   Count = n.Count()
                                               };
            ((PieSeries)NamespacesChart.Series[0]).ItemsSource = namespaces;
        }

        private void PopulateAssembliesChart()
        {
            IEnumerable<KeyCount> assemblies = from c in controls
                                               group c by c.AssemblyName into a
                                               orderby a.Count() descending
                                               select new KeyCount()
                                               {
                                                   Key = a.Key,
                                                   Count = a.Count()
                                               };
            ((PieSeries)AssembliesChart.Series[0]).ItemsSource = assemblies;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            GetControls();
            PopulateNamespacesChart();
            PopulateAssembliesChart();
            ShowControlData();
        }

        private void Pie_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PieSeries pie = (PieSeries)sender;
            KeyCount keyCount = (KeyCount)e.AddedItems[0];
            ShowControlData(pie.Tag.ToString(), keyCount.Key);
        }
    }
}
