using System;
using System.Collections.Generic;

namespace skyscraper5.Skyscraper.Plugins
{
	internal class PluginPrioritySorter : IComparer<object>
	{
		public int Compare(object x, object y)
		{
			long xv = GetPriorityFromObject(x);
			long yv = GetPriorityFromObject(y);
			return xv.CompareTo(yv);
		}

		public static int GetPriorityFromObject(object obj)
		{
			if (obj == null)
				return 0;

			Type type = obj.GetType();

			object[] customAttributes = type.GetCustomAttributes(typeof(PluginPriorityAttribute), false);
			foreach (object customAttribute in customAttributes)
			{
				PluginPriorityAttribute ppa = (PluginPriorityAttribute)customAttribute;
				return ppa.Priority;
			}

			return 0;
		}
	}
}
