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 Microsoft.Phone.Data.Linq;

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

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            personenDataContext = new PersonenDataContext(PersonenDataContext.DBConnectionString);
            DatabaseSchemaUpdater schemaUpdate;
            if (!personenDataContext.DatabaseExists())
            {
                personenDataContext.CreateDatabase();
                schemaUpdate = personenDataContext.CreateDatabaseSchemaUpdater();
                schemaUpdate.DatabaseSchemaVersion = personenDataContext.SCHEMAVERSION;
                schemaUpdate.Execute();

            }
            else
            {
                schemaUpdate = personenDataContext.CreateDatabaseSchemaUpdater();
                if (schemaUpdate.DatabaseSchemaVersion < personenDataContext.SCHEMAVERSION)
                {
                    schemaUpdate.AddColumn<Person>("Gender");
                    schemaUpdate.DatabaseSchemaVersion = personenDataContext.SCHEMAVERSION;
                    schemaUpdate.Execute();
                }
            }
            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.Gender = Gender.Male;
                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();
            }
        }
    }
}