using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MileageTraker.Web.Utility;
using NUnit.Framework;

namespace Web.Tests.DAL
{
	[TestFixture]
	public class CityNameCleaner
	{
		[Test]
		[Ignore]
		public void Clean()
		{
			const string cityFilename = @"C:\Temp\cities.csv";
			const string unknownCityFilename = @"C:\Temp\cities_unknown.csv";
			var unknownCities =
				from line in File.ReadAllLines(unknownCityFilename)
				select new {CityName = line, First = (line.Split(',')).FirstOrDefault()};
			var cities =
				File.ReadAllLines(cityFilename);

			var b =
				from uc in unknownCities
				let topMatch = (
								  from c in cities
								  let s = Algorithms.Similarity(uc.First, c)
								  orderby s descending
								  select new { c, s }).First()
				select new
					{
						uc.CityName, topMatch.c, topMatch.s
					};
			foreach (var l in b)
			{
				Console.WriteLine(
					"UPDATE Log SET Notes=LEFT(CASE WHEN Notes IS NULL THEN CityName ELSE Notes + ', ' + CityName END, 64)" +
					", CityName='{1}' " +
					"WHERE CityName='{0}'",
					l.CityName.Replace("'", "''"), l.c);
			}
		}
	}
}
