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.Browser;
using Telerik.Windows.Controls;

namespace SilverlightGridExport
{
    [ScriptableType]
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();

            RadGridView1.ItemsSource = GetCustomers(XDocument.Load("Customers.xml"));

            HtmlPage.RegisterScriptableObject("Page", this);
        }

        [ScriptableMember]
        public string ToHtml()
        {
            return RadGridView1.ToHtml();
        }

        [ScriptableMember]
        public string ToCsv()
        {
            return RadGridView1.ToCsv();
        }

        protected IEnumerable<Customer> GetCustomers(XDocument document)
        {
            var query = from c in document.Descendants("Customers")
                        select new Customer((string)c.Element("CustomerID"),
                            (string)c.Element("CompanyName"),
                            (string)c.Element("Country"),
                            (string)c.Element("City"),
                            (string)c.Element("ContactName")
                        );

            return query;
        }
    }

    public class Customer
    {
        public Customer(string CustomerID, string ContactName, string CompanyName, string Country, string City)
        {
            this.CustomerID = CustomerID;
            this.ContactName = ContactName;
            this.CompanyName = CompanyName;
            this.Country = Country;
            this.City = City;
        }

        public string CustomerID { get; set; }
        public string CompanyName { get; set; }
        public string ContactName { get; set; }
        public string Country { get; set; }
        public string City { get; set; }
    }

}
