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 System.Xml.Linq;
using HRLibrary;

namespace Ch12Sample_02
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<Company> companies = new List<Company>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            XElement company =
                new XElement("Company",
                    new XElement("Employee",
                        new XElement("ID", "AA111"),
                        new XElement("Name", "John")),
                    new XElement("Employee",
                        new XElement("ID", "AA222"),
                        new XElement("Name", "David")
                 )
            );
            txtResult.Text = company.ToString();   
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                XElement rssdata = XElement.Load(
                    "http://tw.nextmedia.com/rss/create/type/1077");
                txtResult.Text = rssdata.ToString();
            }
            catch (Exception ex)
            {
                txtResult.Text = "请检查网络连接环境是否正常!!";
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            XElement root = new XElement("Root",
                from company in companies
                select new XElement("Company",
                    from emp in company.Employees.Values
                    select new XElement("Employee",
                        new XAttribute("ID", emp.Id),
                        new XAttribute("Name", emp.Name))));

            txtResult.Text = root.ToString();
            root.Save(@"EmployeesData.xml");      
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            companies.Add(CompanyFactory.CreateCompanyA());
            companies.Add(CompanyFactory.CreateCompanyB());
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            XElement data = XElement.Load(@"EmployeesData.xml");
            IEnumerable<XElement> employeesData
                = data.Descendants(XName.Get("Employee"));
            var empQuery =
                from emp in employeesData
                let empName = emp.Attribute(XName.Get("Name")).Value
                where empName[0] == 'J'
                select new
                {
                    EmpID = emp.Attribute(XName.Get("ID")).Value,
                    EmpName = empName
                };
            txtResult.Text = "名字为J开头的员工：" + Environment.NewLine;
            foreach (var result in empQuery)
            {
                txtResult.Text += "(" + result.EmpID + ")" + result.EmpName
                    + Environment.NewLine;
            }
        }
    }
}
