using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace RadMenuBound_WPF
{
    class ZoomControl : ObservableCollection<ZoomLevel>, INotifyPropertyChanged
    {
        public ZoomControl()
        {
            this.Add(new ZoomLevel(this, 0.25, "25%"));
            this.Add(new ZoomLevel(this, 0.50, "50%"));
            this.Add(new ZoomLevel(this, 0.75, "75%"));
            ZoomLevel def = new ZoomLevel(this, 1.00, "100%");
            this.Add(def);
            this.Add(new ZoomLevel(this, 2.00, "200%"));
            this.Add(new ZoomLevel(this, 4.00, "400%"));
            this.Add(new ZoomLevel(this, 6.00, "600%"));
            this.Add(new ZoomLevel(this, 8.00, "800%"));

            this.SelectedZoomLevel = def;
        }

        private ZoomLevel _selectedZoomLevel;
        public ZoomLevel SelectedZoomLevel
        {
            get
            {
                return this._selectedZoomLevel;
            }
            set
            {
                if (value != this._selectedZoomLevel && value != null)
                {
                    if (this._selectedZoomLevel != null)
                    {
                        this._selectedZoomLevel.Unselect();
                    }
                    this._selectedZoomLevel = value;
                    this._selectedZoomLevel.Select();
                    this.OnPropertyChanged("SelectedZoomLevel");
                }
            }
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    class ZoomLevel : INotifyPropertyChanged
    {
        public ZoomLevel(ZoomControl owner, double level, string displayName)
        {
            this.Level = level;
            this.DisplayName = displayName;
            this.Owner = owner;
        }

        public double Level { get; private set; }
        public string DisplayName { get; private set; }
        public ZoomControl Owner { get; private set; }

        private bool _isSelected;
        public bool IsSelected
        {
            get
            {
                return this._isSelected;
            }
            set
            {
                if (value == true && this.IsSelected == false)
                {
                    this.Owner.SelectedZoomLevel = this;
                }
            }
        }

        internal void Unselect()
        {
            if (this._isSelected == true)
            {
                this._isSelected = false;
                this.OnPropertyChanged("IsSelected");
            }
        }
        internal void Select()
        {
            if (this._isSelected == false)
            {
                this._isSelected = true;
                this.OnPropertyChanged("IsSelected");
            }
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
