using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
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;

namespace WizardSample
{
    public partial class WizardDialog : C1.Silverlight.C1Window
    {
        // ** fields
        int _index = -1;
        ObservableCollection<UIElement> _pages;
        bool _canceled = true;

        // ** ctor
        public WizardDialog()
        {
            InitializeComponent();
            HeaderFontSize = 14;
            _pages = new ObservableCollection<UIElement>();
            _pages.CollectionChanged += _pages_CollectionChanged;
        }

        // ** object model
        public ObservableCollection<UIElement> Pages
        {
            get { return _pages; }
        }
        public int SelectedPageIndex
        {
            get { return _index; }
            set
            {
                int oldIndex = _index;
                _index = value;
                if (_index != oldIndex)
                {
                    OnSelectedPageChanging(EventArgs.Empty);
                }

                _content.Content = SelectedPage;
                _btnNext.IsEnabled = _index < _pages.Count - 1;
                _btnBack.IsEnabled = _index > 0;

                if (_index != oldIndex)
                {
                    OnSelectedPageChanged(EventArgs.Empty);
                }
            }
        }
        public UIElement SelectedPage
        {
            get { return _index > -1 && _index < _pages.Count ? _pages[_index] : null; }
            set { SelectedPageIndex = _pages.IndexOf(value); }
        }
        public bool Canceled
        {
            get { return _canceled; }
            set { _canceled = value; }
        }

        // ** raise events
        public event EventHandler SelectedPageChanging;
        protected virtual void OnSelectedPageChanging(EventArgs e)
        {
            if (SelectedPageChanging != null)
                SelectedPageChanging(this, e);
        }
        public event EventHandler SelectedPageChanged;
        protected virtual void OnSelectedPageChanged(EventArgs e)
        {
            if (SelectedPageChanged != null)
                SelectedPageChanged(this, e);
        }

        // ** handle events
        void _pages_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    if (_index < 0)
                        _index = 0;
                    break;
                case NotifyCollectionChangedAction.Remove:
                    if (_index > _pages.Count - 1)
                        _index = _pages.Count - 1;
                    break;
            }
            SelectedPageIndex = _index;
        }
        void _btnNext_Click(object sender, RoutedEventArgs e)
        {
            SelectedPageIndex++;
        }
        void _btnBack_Click(object sender, RoutedEventArgs e)
        {
            SelectedPageIndex--;
        }
        void _btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
