using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using LeafWeb.Core.Parsers;
using LeafWeb.Core.Utility;
using NUnit.Framework;

namespace LeafWeb.Core.Tests.Parsers
{
	[TestFixture]
	public class FluxnetSiteCsvParserTests
	{
		private const string ContentDirectory = @"Parsers\FluxnetSiteData\";

		[Test]
		public void Parse_Valid()
		{
			var fileInfo = FileUtility.GetContentFile(ContentDirectory, "fluxnet_site_list_all_October2015_with_joins.csv");
			var parser = new FluxnetSiteCsvParser(fileInfo);
			foreach (var fns in parser.Parse())
			{
				Console.WriteLine($"{fns.FluxnetId} / {fns.SiteName}");
			}
		}

		[Test]
		public void Parse_Find_Misencoded_Characters()
		{
			var fileInfo = FileUtility.GetContentFile(ContentDirectory, "fluxnet_site_list_all_October2015_with_joins.csv");
			var parser = new FluxnetSiteCsvParser(fileInfo);
			foreach (var fns in parser.Parse().Where(f => Regex.IsMatch(f.SiteName, @"[^\x00-\x7F]") || f.SiteName.Contains("?")))
			{
				Console.WriteLine($"{fns.FluxnetId} / {fns.SiteName}");
			}
		}

		[Test]
		public void Parse_Find_Duplicate_Ids()
		{
			var fileInfo = FileUtility.GetContentFile(ContentDirectory, "fluxnet_site_list_all_October2015_with_joins.csv");
			var parser = new FluxnetSiteCsvParser(fileInfo);
			var fluxnetSites = parser.Parse();

			var dupes =
				from f in fluxnetSites
				group f by f.FluxnetId.ToUpper()
				into g
				where g.Count() > 1
				select g.Key;

			foreach (var dupe in dupes)
			{
				Console.WriteLine($"{dupe}");
			}
		}
	}
}