<commit_before>﻿// (c) Copyright ESRI.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Toolkit.Controls.Primitives;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
#if NETFX_CORE
using Windows.UI.Xaml;
#else
#endif

namespace Esri.ArcGISRuntime.Toolkit.Controls
{
	/// <summary>
	/// Internal class encapsulating a layer item representing a map layer.
	/// This class manages the events coming from the layer and the legend refresh process.
	/// </summary>
	internal sealed class MapLayerItem : LayerItemViewModel
	{
		#region Constructors
		internal const string MapLayerType = "MapLayer Layer";
		private bool _isQuerying;
		private double _serviceMinScale = double.PositiveInfinity; // Max and Min scale coming from the service : to combine with max and min scale coming from the layer at client side
		private double _serviceMaxScale;

		/// <summary>
		/// Initializes a new instance of the <see cref="MapLayerItem"/> class.
		/// </summary>
		/// <param name="layer">The layer.</param>
		internal MapLayerItem(Layer layer)
			: base(layer)
		{
			LayerType = MapLayerType;

			Label = layer.DisplayName;
			MinimumScale = layer.MinScale;
			MaximumScale = layer.MaxScale;
			IsVisible = layer.IsVisible;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="MapLayerItem"/> class. Only useful in Design.
		/// </summary>
		internal MapLayerItem()
		{
			LayerType = MapLayerType;
		}

		#endregion

		#region Events Handler
		private void AttachLayerEventHandler(Layer layer)
		{
			Debug.Assert(layer != null);
			if (layer is ILegendSupport)
				(layer as ILegendSupport).LegendChanged += Layer_LegendChanged;

			layer.PropertyChanged += Layer_PropertyChanged;
			layer.AddPropertyChangedHandler("DisplayName", Layer_PropertyChanged);
			layer.AddPropertyChangedHandler("MinScale", Layer_PropertyChanged);
			layer.AddPropertyChangedHandler("MaxScale", Layer_PropertyChanged);
			layer.AddPropertyChangedHandler("IsVisible", Layer_PropertyChanged);

		}

		private void DetachLayerEventHandler(Layer layer)
		{
			if (layer != null)
			{
				if (layer is ILegendSupport)
					(layer as ILegendSupport).LegendChanged -= Layer_LegendChanged;

				layer.PropertyChanged -= Layer_PropertyChanged;
				layer.RemovePropertyChangedHandler("DisplayName", Layer_PropertyChanged);
				layer.RemovePropertyChangedHandler("MinScale", Layer_PropertyChanged);
				layer.RemovePropertyChangedHandler("MaxScale", Layer_PropertyChanged);
				layer.RemovePropertyChangedHandler("IsVisible", Layer_PropertyChanged);
			}
		}

		private void Layer_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			var layer = sender as Layer;
			if (layer == null)
				return;

			if (e.PropertyName == "MinScale")
			{
				MinimumScale = Layer.MinScale != 0 && !double.IsNaN(Layer.MinScale)
					               ? Math.Min(_serviceMinScale, Layer.MinScale)
					               : _serviceMinScale;
				if (LegendTree != null)
					LegendTree.UpdateLayerVisibilities();
			}
			else if (e.PropertyName == "MaxScale")
			{
				MaximumScale = !double.IsNaN(Layer.MaxScale)
								   ? Math.Max(_serviceMaxScale, Layer.MaxScale)
								   : _serviceMaxScale;
				if (LegendTree != null)
					LegendTree.UpdateLayerVisibilities();
			}
			else if (e.PropertyName == "IsVisible")
			{
				if (LegendTree != null)
					LegendTree.UpdateLayerVisibilities();
			}
			else if (e.PropertyName == "DisplayName")
			{
				Label = layer.DisplayName;
			}
			else if (e.PropertyName == "IsInitialized")
			{
				//if (!(sender is GroupLayerBase)) // For group layers, we don't wait for initialized event 
				Refresh();
			}
		}

		private void Layer_LegendChanged(object sender, EventArgs e)
		{
			// Structure of legend items has changed -> refresh
			Refresh();
		}

		#endregion

		#region Refresh<commit_msg>Updated to match latest API changes (Layer.Status instead of IsInitialized) and to use same internals as TemplatePicker<commit_after>﻿// (c) Copyright ESRI.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Toolkit.Controls.Primitives;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using Esri.ArcGISRuntime.Toolkit.Internal;

namespace Esri.ArcGISRuntime.Toolkit.Controls
{
	/// <summary>
	/// Internal class encapsulating a layer item representing a map layer.
	/// This class manages the events coming from the layer and the legend refresh process.
	/// </summary>
	internal sealed class MapLayerItem : LayerItemViewModel
	{
		#region Constructors
		internal const string MapLayerType = "MapLayer Layer";
		private bool _isQuerying;
		private double _serviceMinScale = double.PositiveInfinity; // Max and Min scale coming from the service : to combine with max and min scale coming from the layer at client side
		private double _serviceMaxScale;
        // Listen for layers DP changes
        private DependencyPropertyChangedListener<MapLayerItem> _layerDisplayNameChangedListener;
        private DependencyPropertyChangedListener<MapLayerItem> _layerMinScaleChangedListener;
        private DependencyPropertyChangedListener<MapLayerItem> _layerMaxScaleChangedListener;
        private DependencyPropertyChangedListener<MapLayerItem> _layerStatusChangedListener;
        private DependencyPropertyChangedListener<MapLayerItem> _layerIsVisibleChangedListener;

		/// <summary>
		/// Initializes a new instance of the <see cref="MapLayerItem"/> class.
		/// </summary>
		/// <param name="layer">The layer.</param>
		internal MapLayerItem(Layer layer)
			: base(layer)
		{
			LayerType = MapLayerType;

			Label = layer.DisplayName;
			MinimumScale = layer.MinScale;
			MaximumScale = layer.MaxScale;
			IsVisible = layer.IsVisible;
        }

		/// <summary>
		/// Initializes a new instance of the <see cref="MapLayerItem"/> class. Only useful in Design.
		/// </summary>
		internal MapLayerItem()
		{
			LayerType = MapLayerType;
		}

		#endregion

		#region Events Handler
		private void AttachLayerEventHandler(Layer layer)
		{
			Debug.Assert(layer != null);
			if (layer is ILegendSupport)
				(layer as ILegendSupport).LegendChanged += OnLayerLegendChanged;

		    _layerDisplayNameChangedListener = new DependencyPropertyChangedListener<MapLayerItem>(this, layer, "DisplayName")
            {
                OnEventAction = (instance, source, eventArgs) => instance.OnLayerPropertyChanged(source, eventArgs)
            };
            _layerMinScaleChangedListener = new DependencyPropertyChangedListener<MapLayerItem>(this, layer, "MinScale")
            {
                OnEventAction = (instance, source, eventArgs) => instance.OnLayerPropertyChanged(source, eventArgs)
            };
            _layerMaxScaleChangedListener = new DependencyPropertyChangedListener<MapLayerItem>(this, layer, "MaxScale")
            {
                OnEventAction = (instance, source, eventArgs) => instance.OnLayerPropertyChanged(source, eventArgs)
            };
            _layerIsVisibleChangedListener = new DependencyPropertyChangedListener<MapLayerItem>(this, layer, "IsVisible")
            {
                OnEventAction = (instance, source, eventArgs) => instance.OnLayerPropertyChanged(source, eventArgs)
            };
            _layerStatusChangedListener = new DependencyPropertyChangedListener<MapLayerItem>(this, layer, "Status")
            {
                OnEventAction = (instance, source, eventArgs) => instance.OnLayerPropertyChanged(source, eventArgs)
            };
        }

		private void DetachLayerEventHandler(Layer layer)
		{
			if (layer != null)
			{
				if (layer is ILegendSupport)
					(layer as ILegendSupport).LegendChanged -= OnLayerLegendChanged;
                _layerDisplayNameChangedListener.Detach();
                _layerMinScaleChangedListener.Detach();
                _layerMaxScaleChangedListener.Detach();
                _layerIsVisibleChangedListener.Detach();
                _layerStatusChangedListener.Detach();
            }
		}

        private void OnLayerPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			var layer = sender as Layer;
			if (layer == null)
				return;

			if (e.PropertyName == "MinScale")
			{
				MinimumScale = Layer.MinScale != 0 && !double.IsNaN(Layer.MinScale)
					               ? Math.Min(_serviceMinScale, Layer.MinScale)
					               : _serviceMinScale;
				if (LegendTree != null)
					LegendTree.UpdateLayerVisibilities();
			}
			else if (e.PropertyName == "MaxScale")
			{
				MaximumScale = !double.IsNaN(Layer.MaxScale)
								   ? Math.Max(_serviceMaxScale, Layer.MaxScale)
								   : _serviceMaxScale;
				if (LegendTree != null)
					LegendTree.UpdateLayerVisibilities();
			}
			else if (e.PropertyName == "IsVisible")
			{
				if (LegendTree != null)
					LegendTree.UpdateLayerVisibilities();
			}
			else if (e.PropertyName == "DisplayName")
			{
				Label = layer.DisplayName;
			}
			else if (e.PropertyName == "Status")
			{
				//if (!(sender is GroupLayerBase)) // For group layers, we don't wait for initialized event 
				Refresh();
			}
		}

		private void OnLayerLegendChanged(object sender, EventArgs e)
		{
			// Structure of legend items has changed -> refresh
			Refresh();
		}

		#endregion

		#region Refresh
{'fraction_non_alphanumeric': 0.09627393838467943, 'fraction_numerical': 0.0014571190674437969, 'mean_word_length': 3.407798165137615, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3873458', 'n_tokens_mistral': 3080, 'n_tokens_neox': 2611, 'n_words': 681}