using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Data.Linq;

namespace WPDatabase1
{
    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.Count == 0)
            {
                Person person = new Person()
                {
                    FirstName = "Simon",
                    LastName = "Hackfort",
                    Age = 32,
                    City = "Duisburg"
                };
                personenDataContext.PersonenTable.InsertOnSubmit(person);
                personenDataContext.SubmitChanges();
                personenDataContext.Personen.Add(person);
            }
        }

        private void btnUpdateEntry_Click(object sender, RoutedEventArgs e)
        {
            if (personenDataContext.Personen.Count > 0)
            {
                Person person = personenDataContext.Personen[0];
                person.City = "Essen";
                personenDataContext.SubmitChanges();
            }
        }

        private void btnDeleteEntry_Click(object sender, RoutedEventArgs e)
        {
            if (personenDataContext.Personen.Count > 0)
            {
                Person person = personenDataContext.Personen[0];
                personenDataContext.Personen.Remove(person); 
                personenDataContext.PersonenTable.DeleteOnSubmit(person);
                personenDataContext.SubmitChanges();
            }
        }
    }
}