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 HRLibrary;

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

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var employeeQuery =
                from company in companies
                from emp in company.Employees.Values
                where emp.Level >= 10
                select emp;

            lblResult.Text = "";
            //element为Employee类对象
            foreach (var element in employeeQuery)
            {
                lblResult.Text += "(" + element.Id + ")" + element.Name + ",";
            }
        }

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

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            var employeeQuery =
                 from company in companies
                 from emp in company.Employees.Values
                 where emp.Level <= 10 && emp.Level >= 8
                 select emp;

            lblResult.Text = "";
            foreach (var element in employeeQuery)
            {
                lblResult.Text += "(" + element.Id + ")" + element.Name + ",";
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            var employeeQuery =
                from company in companies
                from emp in company.Employees.Values
                orderby emp.Level
                 select emp;

            lblResult.Text = "";
            foreach (var element in employeeQuery)
            {
                lblResult.Text += "(" + element.Level + ")" + element.Name + ",\n";
            }
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            var employeeQuery =
                 from company in companies
                 from emp in company.Employees.Values
                 orderby emp.ComputeSalary()
                 group emp by emp.Id;

            //查询结果为IGrouping泛型集合
            lblResult.Text = "所有员工数目："
                + employeeQuery.Count() + "\n员工编号列表(按薪资)：\n";

            foreach (IGrouping<string, Employee> element in employeeQuery)
            {
                lblResult.Text += "(" + element.Key + ")\n";
            }
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            var employeeQuery =
                from company in companies
                from emp in company.Employees.Values
                group emp by emp.ComputeSalary() into empGroup
                where empGroup.Key >= 70000
                select empGroup;


            lblResult.Text = "薪水70000元以上员工数目："
                + employeeQuery.Count() + "\n";

        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            var salaryQuery =
                from company in companies
                from emp in company.Employees
                let salary = emp.Value.ComputeSalary()
                select salary;

            lblResult.Text = "Salary List:\n";
            foreach (var element in salaryQuery)
            {
                lblResult.Text += element + ",";
            }
        }

        private void button7_Click(object sender, RoutedEventArgs e)
        {
            var ownerQuery =
                from computer in computers
                join emp in
                    (
                        from company in companies
                        from emp in company.Employees.Values
                        select emp
                        ) on computer.Owner equals emp.Id
                select new { CompanyID = computer.ID, OwnerName = emp.Name };

            lblResult.Text = "打印电脑保管人姓名：\n";
            foreach (var data in ownerQuery)
            {
                lblResult.Text += data.CompanyID + "=>" + data.OwnerName + "\n";
            }
        }
    }
}
