﻿//The MIT License(MIT)
//
//Copyright(c) 2016 universalappfactory
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Sharpend.UAP.Controls.Buttons
{
    [TemplatePart(Name = IconButton.PART_Symbol,Type = typeof(SymbolIcon))]
    public class IconButton : Button
    {
        public const string PART_Symbol = "PART_Symbol";

        //Symbol
        public Symbol Symbol
        {
            get { return (Symbol)GetValue(SymbolProperty); }
            set { SetValue(SymbolProperty, value); }
        }

        public static readonly DependencyProperty SymbolProperty =
            DependencyProperty.Register("Symbol", typeof(Symbol), typeof(IconButton), 
                new PropertyMetadata(0, SymbolChanged));

        //IconWidth
        public double IconWidth
        {
            get { return (double)GetValue(IconWidthProperty); }
            set { SetValue(IconWidthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IconWidth.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IconWidthProperty =
            DependencyProperty.Register("IconWidth", typeof(double), typeof(IconButton), new PropertyMetadata(15));


        //IconHeight
        public double IconHeight
        {
            get { return (double)GetValue(IconHeightProperty); }
            set { SetValue(IconHeightProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IconHeight.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IconHeightProperty =
            DependencyProperty.Register("IconHeight", typeof(double), typeof(IconButton), new PropertyMetadata(15));


        //IconPadding
        public Thickness IconPadding
        {
            get { return (Thickness)GetValue(IconPaddingProperty); }
            set { SetValue(IconPaddingProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IconPadding.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IconPaddingProperty =
            DependencyProperty.Register("IconPadding", typeof(Thickness), typeof(IconButton), new PropertyMetadata(new Thickness(0)));




        public IconButton()
        {
        }
                
        private static void SymbolChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var btn = d as IconButton;
            if ((btn != null) && (btn.Content != null))
            {
                var symbolIcon = (btn.Content as SymbolIcon);
                if (symbolIcon != null)
                {
                    symbolIcon.Symbol = btn.Symbol;
                }
            } else if (btn != null)
            {
                var symbolIcon = btn.GetTemplateChild("PART_Symbol") as SymbolIcon;
                if (symbolIcon != null)
                {
                    symbolIcon.Symbol = btn.Symbol;
                }
            }
        }

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

            var symbolIcon = GetTemplateChild(PART_Symbol) as SymbolIcon;
            if (symbolIcon == null)
            {
                symbolIcon = new SymbolIcon();
                this.Content = symbolIcon;
            }
            
            symbolIcon.Symbol = this.Symbol;
            
        }
    }
}
