using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Platform.Controls.BaseContainer
{
    public class ImageButton : Button
    {
        Image innerImage;

        static ImageButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
        }

        public ImageButton()
        {
            this.IsEnabledChanged += new DependencyPropertyChangedEventHandler(ImageButton_IsEnabledChanged);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            innerImage = this.Template.FindName("innerImage", this) as Image;

            if (innerImage == null) return;

            if (this.IsEnabled && ImageSource != null)
            {
                innerImage.Source = ImageSource;
            }
            else if (!this.IsEnabled && GrayImageSource != null)
            {
                innerImage.Source = GrayImageSource;
            }
        }

        void ImageButton_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (innerImage == null) return;

            if (this.IsEnabled && ImageSource != null)
            {
                innerImage.Source = ImageSource;
            }
            else if (!this.IsEnabled && GrayImageSource != null)
            {
                innerImage.Source = GrayImageSource;
            }
        }

        public ImageSource ImageSource
        {
            get { return (ImageSource)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ImageSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null));



        public ImageSource GrayImageSource
        {
            get { return (ImageSource)GetValue(GrayImageSourceProperty); }
            set { SetValue(GrayImageSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for GrayImageSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty GrayImageSourceProperty =
            DependencyProperty.Register("GrayImageSource", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null));

    }
}
