using System;
using System.IO;

/*=====================================================================

  File:      Trace.cs for Spatial Sample
  Summary:   Implements trace debugging functionality
  Date:	     August 16, 2005

---------------------------------------------------------------------
  This spatial search library was developed by Alexander Szalay, Robert Brunner, 
  Peter Kunszt, and Gyorgy Fekete at the Department of Physics and Astronomy, 
  The Johns Hopkins University, in collaboration with Jim Gray, Microsoft Research.
  Details of the algorithms can be found at http://www.sdss.jhu.edu/htm/.
 
  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.
======================================================= */

namespace Spherical.HTM
{
	/// <summary>
	/// Trace class is used only for internal debugging
	/// It will dump text into a designated file, which is
	/// created when an instance of the object is created.
	/// The only useful method(s) dump text into the file.
	/// It does use Savecode and Markup from the spatial HTM
	/// package for formatted output, otherwise this could be a
	/// standalone class
	///
	/// </summary>
	public class Trace
	{
		private StreamWriter ws = null;
		/// <summary>
		/// No default public constructor
		/// </summary>
		private Trace() {
		}
		/// <summary>
		/// Creates an object for managing a trace or dump file
		/// </summary>
		/// <param name="filename"></param>
		/// <param name="append">if true, the file is appended, otherwise
		/// the file is truncated to zero first </param>
		public Trace(string filename, bool append) {
			try {
				ws = new StreamWriter(filename, append);
			} catch {
				ws = null;
			}
		}
		/// <summary>
		/// This method was designed to writes a description of a halfspace with color
		/// information. The output is used with a graphical debugger.
		/// </summary>
		/// <param name="x"></param>
		/// <param name="y"></param>
		/// <param name="z"></param>
		/// <param name="d"></param>
		/// <param name="color">a color designator, the index of one of these: 
		/// {B,R,B,G,Y,M,C} (B appears twice, probably a bug)</param>
		public void dump(double x, double y, double z, double d, int color){
			char[] colorchar = {'B', 'R', 'B', 'G', 'Y', 'M', 'C'};
			if (ws != null){
				ws.WriteLine("{0} {1} {2} {3} {4}", x, y, z, d, colorchar[color]);
			}
		}
		/// <summary>
		/// Writes a description of a halfpace: x y z d
		/// </summary>
		/// <param name="x"></param>
		/// <param name="y"></param>
		/// <param name="z"></param>
		/// <param name="d"></param>
		public void dump(double x, double y, double z, double d){
			if (ws != null){
				ws.WriteLine("{0} {1} {2} {3}", x, y, z, d);
			}
		}
		/// <summary>
		/// Dumps a string  that contains references to an HID and an integer
		/// </summary>
		/// <param name="fmt"></param>
		/// <param name="hid"></param>
		/// <param name="vsum">integer parameter</param>
		public void dump(string fmt, Int64 hid, int vsum){
			if (ws != null){
				ws.WriteLine(fmt, hid, vsum);
				return;
			}
		}

		///// <summary>
		///// Dumps a string with HID and a "savecode"
		///// </summary>
		///// <param name="fmt"></param>
		///// <param name="hid"></param>
		///// <param name="scode">ConvexTrixel.Savecode</param>
		//public void dump(string fmt, Int64 hid, ConvexTrixel.Savecode scode, int level) {
		//    if (ws != null) {
		//        ws.WriteLine(fmt, hid, scode, level);
		//        return;
		//    }
		//}
		///// <summary>
		///// Dumps a string with HID and a "markup"
		///// </summary>
		///// <param name="fmt"></param>
		///// <param name="hid"></param>
		///// <param name="markup">ConvexTrixel.Markup</param>
		//public void dump(string fmt, Int64 hid, ConvexTrixel.Markup markup) {
		//    if (ws != null) {
		//        ws.WriteLine(fmt, hid, markup);
		//        return;
		//    }
		//}
		//public void dump(string fmt, Int64 hid, ConvexTrixel.Markup markup, int i) {
		//    if (ws != null) {
		//        ws.WriteLine(fmt, hid, markup, i);
		//        return;
		//    }
		//}

		/// <summary>
		/// Dumps a string with HID and a "savecode"
		/// </summary>
		/// <param name="fmt"></param>
		/// <param name="hid"></param>
		/// <param name="scode">ConvexTrixel.Savecode</param>
		public void dump(string fmt, Int64 hid, Savecode scode, int level) {
			if (ws != null) {
				ws.WriteLine(fmt, hid, scode, level);
				return;
			}
		}
		/// <summary>
		/// Dumps a string with HID and a "markup"
		/// </summary>
		/// <param name="fmt"></param>
		/// <param name="hid"></param>
		/// <param name="markup">ConvexTrixel.Markup</param>
		public void dump(string fmt, Int64 hid, Markup markup) {
			if (ws != null) {
				ws.WriteLine(fmt, hid, markup);
				return;
			}
		}
		public void dump(string fmt, Int64 hid, Markup markup, int i) {
			if (ws != null) {
				ws.WriteLine(fmt, hid, markup, i);
				return;
			}
		}

		/// <summary>
		/// Dumps the hid. Message implicit in the fmt string
		/// </summary>
		/// <param name="fmt"></param>
		/// <param name="hid"></param>
		public void dump(string fmt, Int64 hid){
			if (ws != null){
				ws.WriteLine(fmt, hid);
				return;
			}
		}
		public void dump(string fmt, Int64 hid1, Int64 hid2){
			if (ws != null){
				ws.WriteLine(fmt, hid1, hid2);
				return;
			}
		}
		public void close(){
			if (ws != null) {
				ws.Flush();
				ws.Close();
				ws = null;
			}
			return;
		}
	}
}
