// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
using ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.Editor;
using ICSharpCode.NRefactory.Xml;

namespace ICSharpCode.NRefactory.ConsistencyCheck.Xml
{
	public static class XmlReaderTest
	{
		public static void Run(string fileName)
		{
			bool includeAttributes = true;
			var textSource = new StringTextSource(File.ReadAllText(fileName));
			using (var textReader = textSource.CreateReader()) {
				using (var xmlReader = new XmlTextReader(textReader)) {
					Run(xmlReader, includeAttributes);
				}
			}
			var doc = new AXmlParser().Parse(textSource);
			using (var xmlReader = doc.CreateReader()) {
				Run(xmlReader, includeAttributes);
			}
			var xmlDocument = new XmlDocument();
			xmlDocument.Load(doc.CreateReader());
			xmlDocument.Save(Path.Combine(Program.TempPath, "savedXmlDocument.xml"));
			var xDocument = XDocument.Load(doc.CreateReader());
			xDocument.Save(Path.Combine(Program.TempPath, "savedXDocument.xml"));
			File.WriteAllText(Path.Combine(Program.TempPath, "inputDocument.xml"), textSource.Text);
		}
		
		static string CSV(IEnumerable<string> input)
		{
			return string.Join(",", input.Select(i => "\"" + i.Replace("\"", "\"\"") + "\""));
		}
		
		static readonly string[] ignoredProperties = {
			"NameTable", "CanResolveEntity", "CanReadBinaryContent", "CanReadValueChunk", "ValueType",
			"SchemaInfo", "BaseURI", "Settings"
		};
		
		public static void Run(XmlReader reader, bool includeAttributes, bool includeAttributeValues = true)
		{
			using (StreamWriter output = File.CreateText(Path.Combine(Program.TempPath, reader.GetType().Name + "-output.csv"))) {
				var properties = typeof(XmlReader).GetProperties(BindingFlags.Public | BindingFlags.Instance)
					.Where(p => p.GetIndexParameters().Length == 0 && !ignoredProperties.Contains(p.Name))
					.ToArray();
				output.WriteLine(CSV(properties.Select(p => p.Name)));
				do {
					output.WriteLine(CSV(properties.Select(p => ToString(p.GetValue(reader, null)))));
					if (includeAttributes && reader.HasAttributes) {
						for (int i = 0; i < reader.AttributeCount; i++) {
							reader.MoveToAttribute(i);
							output.WriteLine(CSV(properties.Select(p => ToString(p.GetValue(reader, null)))));
							if (includeAttributeValues) {
								reader.ReadAttributeValue();
								output.WriteLine(CSV(properties.Select(p => ToString(p.GetValue(reader, null)))));
							}
						}
					}
				} while (reader.Read());
				output.WriteLine(CSV(properties.Select(p => ToString(p.GetValue(reader, null)))));
			}
		}
		
		static string ToString(object val)
		{
			return TextWriterTokenWriter.PrintPrimitiveValue(val);
		}
	}
}
