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

namespace PCUT.ViewModels
{
    public class ComponentImageModel
    {
        public ObservableCollection<ComponentImageSource> ComponentList { get; } = new ObservableCollection<ComponentImageSource>();

        public void AddSource(string id, bool deleted, XDocument doc, TypedEventHandler<SvgImageSource, SvgImageSourceOpenedEventArgs> eventHandler = null)
        {
            var source = new SvgImageSource();
            if (eventHandler != null)
            {
                source.Opened += eventHandler;
            }
            ComponentList.Add(new ComponentImageSource { Id = id, Deleted = deleted, Source = source });
            _ = source.LoadSvgAsync(doc);
        }
    }

    public class ComponentImageSource : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ImageSource Source { get; set; }
        public string Id { get; set; }

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

    public class ComponentStyle : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;


        private double _width;
        public double Width
        {
            get => _width;
            set
            {
                _width = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Width)));
            }
        }

        private double _height;
        public double Height
        {
            get => _height;
            set
            {
                _height = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Height)));
            }
        }

        public void SetSize(RowDefinition rowDefinition, ColumnDefinition columnDefinition)
        {
            Height = rowDefinition.ActualHeight;
            Width = columnDefinition.ActualWidth;
        }

        public void SetSize(RowDefinition rowDefinition)
        {
            Width = rowDefinition.ActualHeight;
            Height = rowDefinition.ActualHeight;
        }

        public void SetSize(RowDefinition rowDefinition, double ratio)
        {
            Width = rowDefinition.ActualHeight * ratio;
            Height = rowDefinition.ActualHeight;
        }

        public void SetSize(ColumnDefinition columnDefinition, int rate = 1)
        {
            Width = rate <= 1 ? columnDefinition.ActualWidth : Math.Floor((columnDefinition.ActualWidth - 25) / rate);
            Height = rate <= 1 ? columnDefinition.ActualWidth : Math.Floor((columnDefinition.ActualWidth - 25) / rate);
        }

        public void SetSize(ColumnDefinition columnDefinition, double ratio)
        {
            Width = columnDefinition.ActualWidth;
            Height = columnDefinition.ActualWidth / ratio;
        }
    }
}
