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 Microsoft.Phone.Controls;
using System.Collections.ObjectModel;

namespace WPCompiledQuery
{
    public partial class MainPage : PhoneApplicationPage
    {
        private PersonenDataContext personenDataContext;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            personenDataContext = new PersonenDataContext(PersonenDataContext.DBConnectionString);
            this.DataContext = personenDataContext;

        }
        private void btnCreateEntry_Click(object sender, RoutedEventArgs e)
        {
            if (personenDataContext.Personen == null)
            {
                Person person = new Person()
                {
                    FirstName = "Simon",
                    LastName = "Hackfort",
                    Age = 32,
                    City = "Duisburg"
                };
                personenDataContext.PersonenTable.InsertOnSubmit(person);
                person = new Person()
                {
                    FirstName = "Patrick",
                    LastName = "Getzmann",
                    Age = 30,
                    City = "Essen"
                };
                personenDataContext.PersonenTable.InsertOnSubmit(person);
                person = new Person()
                {
                    FirstName = "Peter",
                    LastName = "Nowak",
                    Age = 33,
                    City = "Mülheim"
                };
                personenDataContext.PersonenTable.InsertOnSubmit(person);
                personenDataContext.SubmitChanges();
                var personenInDB = from Person pers in personenDataContext.PersonenTable
                                   select pers;
                personenDataContext.Personen = new ObservableCollection<Person>(personenInDB);

            }
        }

        private void btnFilterNowak_Click(object sender, RoutedEventArgs e)
        {
            string outputdata;
            DateTime start = DateTime.Now;
            personenDataContext.Personen = new System.Collections.ObjectModel.ObservableCollection<Person>(
                from Person pers in personenDataContext.PersonenTable
                where pers.LastName.Contains("Nowak")
                select pers);
            DateTime end = DateTime.Now;
            outputdata = "Dynamische Abfrage: " + end.Subtract(start).TotalMilliseconds + " ms\r\n";
            start = DateTime.Now;
            personenDataContext.Personen = new System.Collections.ObjectModel.ObservableCollection<Person>(
                PersonenDataContext.FilteredPersons(personenDataContext, "Nowak"));
            end = DateTime.Now;
            outputdata += "Kompilierte Abfrage: " + end.Subtract(start).TotalMilliseconds + " ms";
            MessageBox.Show(outputdata);
        }

        private void btnFilterHackfort_Click(object sender, RoutedEventArgs e)
        {
            personenDataContext.Personen = new System.Collections.ObjectModel.ObservableCollection<Person>(
                PersonenDataContext.FilteredPersons(personenDataContext, "Hackfort"));

        }

    }
}