﻿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 MenuButton : 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(MenuButton), null);
        public string Title
        {
            get { return GetValue(TitleProperty) as string; }
            set { SetValue(TitleProperty, value); }
        }
        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(MenuButton), null);
        public int IconSize
        {
            get { return (int)GetValue(IconSizeProperty); }
            set { SetValue(IconSizeProperty, value); }
        }
        public static readonly DependencyProperty IconSizeProperty = DependencyProperty.Register("IconSize", typeof(int), typeof(MenuButton), null);
        public bool IconPlus
        {
            get { return (bool)GetValue(IsRightOnlyIconProperty); }
            set
            {
                SetValue(IsRightOnlyIconProperty, value);
                if (value)
                    VisualStateManager.GoToState(this, "IconPlusText", false);
                else
                    VisualStateManager.GoToState(this, "OnlyText", false);
            }
        }
        public static readonly DependencyProperty IsRightOnlyIconProperty = DependencyProperty.Register("IconPlus", typeof(bool), typeof(MenuButton), null);
        public bool IconOnly
        {
            get { return (bool)GetValue(IconOnlyProperty); }
            set
            {
                SetValue(IconOnlyProperty, value);
                if (value)
                    VisualStateManager.GoToState(this, "OnlyIcon", false);
                else if (IconPlus)
                    VisualStateManager.GoToState(this, "IconPlusText", false);
                else
                    VisualStateManager.GoToState(this, "OnlyText", false);
            }
        }
        public static readonly DependencyProperty IconOnlyProperty = DependencyProperty.Register("IconOnly", typeof(bool), typeof(MenuButton), null);
        public bool Square
        {
            get { return (bool)GetValue(SquareProperty); }
            set
            {
                if(value)
                {
                    imgRound.Visibility = Visibility.Visible;
                    imgSquare.Visibility = Visibility.Collapsed;
                }
                else
                {
                    imgRound.Visibility = Visibility.Collapsed;
                    imgSquare.Visibility = Visibility.Visible;
                }
                SetValue(SquareProperty, value);
            }
        }
        public static readonly DependencyProperty SquareProperty = DependencyProperty.Register("Square", typeof(bool), typeof(MenuButton), null);

        public MenuButton()
        {
            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;
        }
    }
}
