using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightDataApp.Web;
using System.Windows.Ria.Data;

namespace SilverlightDataApp
{
    public partial class MainPage : UserControl
    {
        //An instance of the Registered window, where the DataGrid is located. 
        Registered RegisteredUC = new Registered();

        //A new instance of Domain Context used for communicating data with the database.
        public MyDomainContext context = new MyDomainContext();

        //A string for holding error messages.
        public string message;

        public MainPage()
        {
            // Required to initialize variables
            InitializeComponent();
        }

        //A method for the first button
        private void openDesc_A(object sender, RoutedEventArgs e)
        {
            //A new instance of the AboutEvent window.
            var aboutEventWindow = new AboutEvent();

            //An event handler for when the window gets closed. 
            //aboutEventWindow.Closed += aboutEventWindow_Closed;

            //A method for showing the window, when the button is clicked.
            aboutEventWindow.Show();
        }

        //void aboutEventWindow_Closed(object sender, EventArgs e) { }

        private void openReg_B(object sender, RoutedEventArgs e)
        {
            var registrationEventWindow = new Registration();
            registrationEventWindow.Closed += registrationEventWindow_Closed;
            registrationEventWindow.Show();
        }

        //This code is activated when the Registration window with the DataForm is closed
        void registrationEventWindow_Closed(object sender, EventArgs e)
        {
            //The sender is specified, so we can listen to what result is the closed window telling us. 
            var win = sender as Registration;

            //An instance of Domain Context.
            var context = RegisteredUC.dds.DomainContext as MyDomainContext;

            //If the submit button was clicked instead of cancel
            if (win.DialogResult == true)
            {
                //then add a new person to the Domain Context
                context.RegTables.Add(win.newPerson);
                //submit changes to the DomainDataSource, which then saves the new data to the Database.
                RegisteredUC.dds.SubmitChanges();
                //An event handler for when the changes are saved in the database.
                RegisteredUC.dds.SubmittedChanges += new EventHandler<System.Windows.Ria.Data.SubmittedChangesEventArgs>(dds_SubmittedChanges);
            }
        }

        //This method is used to generate an error window in case of data not being saved properly to the database.
        void dds_SubmittedChanges(object sender, System.Windows.Ria.Data.SubmittedChangesEventArgs e)
        {
            //If any errors occur
            if (e.Error != null) 
            {
                if (e.EntitiesInError != null) 
                {
                    //Save the contents of the error to the message
                    message = e.Error.ToString();                    
                }

                //Call the Error Window and pass the message string with the contents of the error to it. 
                var errorWin = new ErrorWindow(message);
                //Show the Error Window.
                errorWin.Show();
            } 

            else 
            { 
                //Else if everything is ok, get the changes from the DomainContext.
                var context = RegisteredUC.dds.DomainContext as MyDomainContext;
                EntityChangeSet changeSet = context.Entities.GetChanges();
            } 
        }

        public void openRegistered_C(object sender, System.Windows.RoutedEventArgs e)
        {
            var registeredEventWindow = new Registered();
            registeredEventWindow.Show();
        }

        public void openGallery_D(object sender, System.Windows.RoutedEventArgs e)
        {
            var galleryEventWindow = new Gallery();
            galleryEventWindow.Show();
        }
    }
}