﻿using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

namespace JPasswordUWP.Controls
{
    public sealed partial class IconButton : UserControl
    {
        public event Action Click;
        bool pressed = false;

        public ImageSource Icon
        {
            get { return GetValue(IconProperty) as ImageSource; }
            set { SetValue(IconProperty, value); }
        }
        public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(ImageSource), typeof(IconButton), null);
        public double ButtonThickness
        {
            get { return (double)GetValue(ButtonThicknessProperty); }
            set { SetValue(ButtonThicknessProperty, value); }
        }
        public static readonly DependencyProperty ButtonThicknessProperty = DependencyProperty.Register("ButtonThickness", typeof(Double), typeof(IconButton), null);
        public double IconWhiteSpace
        {
            get { return (double)GetValue(IconWhiteSpaceProperty); }
            set { SetValue(IconWhiteSpaceProperty, value); }
        }
        public static readonly DependencyProperty IconWhiteSpaceProperty = DependencyProperty.Register("IconWhiteSpace", typeof(Double), typeof(IconButton), null);
        public double ButtonCornerRadius
        {
            get { return (double)GetValue(CornerRadiusProperty); }
            set
            {
                SetValue(CornerRadiusProperty, value);
            }
        }
        public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("ButtonCornerRadius", typeof(Double), typeof(IconButton), null);
        public SolidColorBrush BorderColor
        {
            get { return GetValue(BorderColorProperty) as SolidColorBrush; }
            set
            {
                SetValue(BorderColorProperty, value);
            }
        }
        public static readonly DependencyProperty BorderColorProperty = DependencyProperty.Register("BorderColor", typeof(SolidColorBrush), typeof(IconButton), null);

        public IconButton()
        {
            InitializeComponent();
            DataContext = this; 
        }

        private void gridPressed(object sender, PointerRoutedEventArgs e)
        {
            pressed = true;
        }
        private void gridReleased(object sender, PointerRoutedEventArgs e)
        {
            if (pressed)
            {
                Click?.Invoke();
                pressed = false;
            }
        }
        private void gridExites(object sender, PointerRoutedEventArgs e)
        {
            pressed = false;
        }
    }
}
