#if UNITY_5_3_OR_NEWER
#define NOESIS
using Noesis;
#else
using System.Windows.Controls;
#endif

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using EventHandler = System.EventHandler;

namespace Testing
{
    public partial class Main : UserControl, INotifyPropertyChanged
    {
        private int _checked = 1;
        private string _lastPressedButton;
        
        public bool IsChecked1 => _checked == 1;

        public bool IsChecked2 => _checked == 2;

        public bool IsChecked3 => _checked == 3;

        public ICommand CheckCommand1 { get; }
        public ICommand CheckCommand2 { get; }
        public ICommand CheckCommand3 { get; }
        public ICommand ChangeCommand { get; }
        
        public string LastPressedButton
        {
            get => _lastPressedButton;
            set
            {
                if (value == _lastPressedButton) return;
                _lastPressedButton = value;
                OnPropertyChanged();
            }
        }

        public Main()
        {
            CheckCommand1 = new ActionCommand(() =>
            {
                LastPressedButton = "1";
                _checked = 1;
                RaiseChecked();
            });
            CheckCommand2 = new ActionCommand(() =>
            {
                LastPressedButton = "2";
                _checked = 2;
                RaiseChecked();
            });
            CheckCommand3 = new ActionCommand(() =>
            {
                LastPressedButton = "3";
                _checked = 3;
                RaiseChecked();
            });
            ChangeCommand = new ActionCommand(() =>
            {
                _checked++;
                if (_checked > 3)
                    _checked = 1;
                RaiseChecked();
            });
            InitializeComponent();
        }

        private void RaiseChecked()
        {
            OnPropertyChanged(nameof(IsChecked1));
            OnPropertyChanged(nameof(IsChecked2));
            OnPropertyChanged(nameof(IsChecked3));
        }

#if NOESIS
        private void InitializeComponent()
        {
            NoesisUnity.LoadComponent(this);
        }
#endif
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        
        public class ActionCommand : ICommand
        {
            private readonly Action _action;

            public ActionCommand(Action action)
            {
                _action = action;
            }

            public bool CanExecute(object parameter)
            {
                return true;
            }

            public void Execute(object parameter)
            {
                _action();
            }

            public event EventHandler CanExecuteChanged;
        }
    }
}