using System.ComponentModel;
using System.Xml.Linq;
using PCUT.Extensions;
using Windows.Foundation;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace PCUT.ViewModels
{
    public class ImageViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private ImageSource _source;
        private SvgImageSource _defaultSource = new SvgImageSource();

        public ImageSource Source
        {
            get => _source;
            private set
            {
                _source = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Source)));
                DefaultMode = value == _defaultSource;
            }
        }

        public void SetSource(ImageSource source)
        {
            if (source == null)
                return;
            Source = source;
        }

        public void SetSource(XDocument doc, TypedEventHandler<SvgImageSource, SvgImageSourceOpenedEventArgs> eventHandler = null)
        {
            Source = _defaultSource;
            if (eventHandler != null)
            {
                _defaultSource.Opened += eventHandler;
            }
            if (doc != null)
                _ = _source.LoadSvgAsync(doc);
        }

        public void ShowDefault()
        {
            Source = _defaultSource;
        }

        private bool _defaultMode = true;
        public bool DefaultMode
        {
            get => _defaultMode;
            private set
            {
                _defaultMode = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(DefaultMode)));
            }
        }
    }
}
