using System;
using System.Collections.Generic;
using System.Linq;
using MusicMetaTagger.Core.Model;

namespace MusicMetaTagger.Core.Utility
{
	public static class ScoreUtility
	{
		public static IEnumerable<ScoreContainer<T>> ScoreResults<T>(IList<T> results)
			where T : IResult
		{
			if (!results.Any())
				return new ScoreContainer<T>[0];
			var maxResultOrder = results.Max(a => a.ResultOrder);
			return
				from result in results
				let score = 1.0 - (1.0 * (result.ResultOrder - 1)) / maxResultOrder
				select new ScoreContainer<T>(result, score);
		}

		public static IEnumerable<ScoreContainer<T>> ScoreResults<T>(Func<string, IEnumerable<T>> getResults, string str)
			where T : IResult
		{
			return ScoreResults(StringUtility.ProgressiveCleaner(getResults, str).ToList());
		}

		public static IEnumerable<ScoreContainer<T1>> Match<T1, T2>
			(IEnumerable<ScoreContainer<T1>> t1s, IEnumerable<ScoreContainer<T2>> t2s,
			 Func<T1, T2, bool> isMatch)
		{
			return
				from t1 in t1s
				from t2 in t2s
				where isMatch(t1.Obj, t2.Obj)
				let score = (t1.Score + t2.Score) / 2.0
				orderby score descending
				select new ScoreContainer<T1>(t1.Obj, score);
		}

	}
}