using System;
using System.Net;
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;

namespace WizardSample.WizardPages
{
    public class ContentPane : UserControl
    {
        string _title, _instructions;

        public ContentPane()
        {
            Loaded += ContentPane_Loaded;
        }

        void ContentPane_Loaded(object sender, RoutedEventArgs e)
        {
            Title = _title;
            Instructions = _instructions;
            WizardDialog.SelectedPageChanging += WizardDialog_SelectedPageChanging;
            WizardDialog.SelectedPageChanged += WizardDialog_SelectedPageChanged;
            OnActivating();
        }
        void WizardDialog_SelectedPageChanged(object sender, EventArgs e)
        {
            if (WizardDialog != null)
                OnActivating();
        }
        void WizardDialog_SelectedPageChanging(object sender, EventArgs e)
        {
            if (WizardDialog != null)
                OnDeactivating();
        }
        protected virtual void OnActivating()
        {
            // override in derived classes
        }
        protected virtual void OnDeactivating()
        {
            // override in derived classes
        }
        public WizardDialog WizardDialog
        {
            get
            {
                for (FrameworkElement e = VisualTreeHelper.GetParent(this) as FrameworkElement; e != null; e = VisualTreeHelper.GetParent(e) as FrameworkElement)
                {
                    if (e is WizardDialog)
                        return e as WizardDialog;
                }
                return null;
            }
        }
        public ContentPage Page
        {
            get
            {
                for (FrameworkElement e = VisualTreeHelper.GetParent(this) as FrameworkElement; e != null; e = VisualTreeHelper.GetParent(e) as FrameworkElement)
                {
                    if (e is ContentPage)
                        return e as ContentPage;
                }
                return null;
            }
        }
        public TextBlock TextBlockTitle
        {
            get
            {
                ContentPage p = Page;
                return p != null ? p._pageTitle : null;
            }
        }
        public TextBlock TextBlockInstructions
        {
            get
            {
                ContentPage p = Page;
                return p != null ? p._pageInstructions : null;
            }
        }
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                if (TextBlockTitle != null)
                {
                    TextBlockTitle.Text = value;
                }
            }
        }
        public string Instructions
        {
            get { return _instructions; }
            set
            {
                _instructions = value;
                if (TextBlockInstructions != null)
                {
                    TextBlockInstructions.Text = value;
                }
            }
        }
        public Button ButtonNext
        {
            get
            {
                WizardDialog d = WizardDialog;
                return d != null ? d._btnNext : null;
            }
        }
        public Button ButtonBack
        {
            get
            {
                WizardDialog d = WizardDialog;
                return d != null ? d._btnBack : null;
            }
        }
        public Button ButtonCancel
        {
            get
            {
                WizardDialog d = WizardDialog;
                return d != null ? d._btnCancel : null;
            }
        }
    }
}
