using System;
using System.Diagnostics;
using System.IO;

namespace m3uTool
{
	/// <summary>
	/// Summary description for m3uFromDirectory.
	/// </summary>
	public class M3uCreate
	{
		/// <summary>
		/// Use the parent's directory name for the m3u name
		/// </summary>
		private bool mUseGeneratedOuputFilename = true;

		/// <summary>
		/// Directory to grab the files from
		/// </summary>
		private DirectoryInfo mInputDir;

		/// <summary>
		/// Ouput m3u filename, if it isn't generated
		/// </summary>
		private string mOutputFilename = null;

		/// <summary>
		/// Filter for input filenames
		/// </summary>
		private string mFileFilter = "*.mp3";

		public bool UseGeneratedOuputFilename
		{
			get
			{
				return mUseGeneratedOuputFilename;
			}
			set
			{
				mUseGeneratedOuputFilename = value;
			}
		}

		public string OutputFilename
		{
			get
			{
				if (mUseGeneratedOuputFilename)
				{
					Debug.WriteLine("OutputFilename:" + mInputDir.FullName + "\\" + mInputDir.Name + ".m3u");
					 return mInputDir.FullName + "\\" + mInputDir.Name + ".m3u";
				}
				return mOutputFilename;
			}
			set
			{
				mOutputFilename = value;
				mUseGeneratedOuputFilename = false;
			}
		}
		
		public M3uCreate(string dir) : this (new DirectoryInfo(dir))
		{
		}

		public M3uCreate(DirectoryInfo inputDir)
		{
			mInputDir = inputDir;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <remarks>
		/// dir: "\foo\bar\" file: "\foo\bar\myfile" output: ""
		/// dir: "\foo\" file: "\foo\bar\myfile" output: ".."
		/// dir: "\" file: "\foo\bar\myfile" output: "..\.."
		/// dir: "\har" file: "\foo\bar\myfile" output: "..\..\har"
		/// dir: "\foo\bar\hey\" file: "\foo\bar\myfile" output: "hey\"
		/// </remarks>
		/// <param name="dir"></param>
		/// <param name="file"></param>
		/// <returns></returns>
		private string GetPath(DirectoryInfo dir, FileInfo file)
		{
			string source = dir.FullName;
			string target = file.Directory.FullName;

			return GetPath(source, target);
		}

		/// <summary>
		/// Gets the path.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <param name="target">The target.</param>
		/// <returns></returns>
		private string GetPath(string source, string target)
		{
			int lastCommonIndex = GetLastCommonIndex(source, target);
			Debug.WriteLine(String.Format("s:{0} , t:{1} lastCommon:{2} ", source, target, lastCommonIndex));

			int slashCount = CountCharacters(source, lastCommonIndex + 1, '\\');

			string path = "";
			for (int i = 0; i < slashCount; i++)
			{
				path += "..\\";
			}
			path += target.Substring(lastCommonIndex);
			if (path.Length > 0 && path[path.Length - 1] != '\\')
				path += "\\";
			Debug.WriteLine(path);
			return path;
		}

		private int CountCharacters(string str, int startIdx, char c)
		{
			int charsCount = 0;
			int currIdx = startIdx;
			while (currIdx < str.Length && (currIdx = str.IndexOf(c, currIdx) + 1) != 0)
				charsCount++;
			return charsCount;
		}

		private int GetLastCommonIndex(string str1, string str2)
		{
			int i = 0;
			while (i < str1.Length && i < str2.Length && Char.ToLower(str1[i]) == Char.ToLower(str2[i]))
				i++;
			return i;
		}

		public void Create()
		{
			string fileList = "";
			DirectoryInfo ouputDirectory = (new FileInfo(OutputFilename)).Directory;
			foreach (FileInfo info in mInputDir.GetFiles(mFileFilter))
			{
				string fileName = GetPath(ouputDirectory, info) + info.Name;
				Debug.WriteLine(fileName);
				fileList += fileName + Environment.NewLine;
			}
			SaveToFile(fileList, OutputFilename);
		}

		private void SaveToFile(string contents, string outputFilename)
		{
			using (TextWriter tw = new StreamWriter(outputFilename))
			{
				tw.Write(contents);
			}
		}
	}
}
