using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;

namespace SqlFilter.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class NorthwindDataService
    {
        /// <summary>
        /// Get all invoices.
        /// </summary>
        [OperationContract]
        public List<Invoice> GetInvoices()
        {
            var ctx = new NorthwindDataClassesDataContext();
            var invoices = 
                from inv in ctx.Invoices
                select inv;
            return invoices.ToList();
        }
        /// <summary>
        /// Get all customers.
        /// </summary>
        [OperationContract]
        public List<string> GetCustomers()
        {
            var ctx = new NorthwindDataClassesDataContext();
            var customers = 
                (from inv in ctx.Invoices
                    select inv.CustomerName).Distinct();
            return customers.ToList();
        }
        /// <summary>
        /// Get all invoices for a specific set of customers.
        /// </summary>
        [OperationContract]
        public List<Invoice> GetCustomerInvoices(params string[] customers)
        {
            // build hashset
            var hash = new HashSet<string>();
            foreach (string c in customers)
            {
                hash.Add(c);
            }
            string[] customerList = hash.ToArray();

            // get invoices for customers in the list
            var ctx = new NorthwindDataClassesDataContext();
            var invoices =
                from inv in ctx.Invoices
                where customerList.Contains(inv.CustomerName)
                select inv;
            return invoices.ToList();
        }
    }
}
