/*
 * +================================================================================+
 *      __  ___  ______________  __  __  ______________   ____
 *     / / / / |/ /  _/_  __/\ \/ / / / / /_  __/  _/ /  / __/
 *    / /_/ /    // /  / /    \  / / /_/ / / / _/ // /___\ \
 *    \____/_/|_/___/ /_/     /_/  \____/ /_/ /___/____/___/
 *
 * +================================================================================+
 *
 *    "THE BEER-WARE LICENSE"
 *    -----------------------
 *
 *    As long as you retain this notice you can do whatever you want with this
 *    stuff. However it is expressly forbidden to sell or commercially distribute
 *    Unity Utils or parts of it apart from your games.
 *    If you meet any of the authors some day, and you think this stuff is
 *    worth it, you can buy them a beer in return.
 *
 *    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.
 *
 * +================================================================================+
 *
 *    Authors:
 *    David Krummenacher (dave@timbookedtwo.com)
 *    Michael Müller (mik@mikmueller.ch)
 *
 *    Partially based on
 *    CSVReader by Dock (http://starfruitgames.com)
 *
 */

using UnityEngine;
using System.Linq;
using System.Text.RegularExpressions;
using System;

/// <summary>
/// Text asset helper.
/// </summary>
public abstract class TextAssetHelper {

	/// <summary>
	/// Converts CSV data.
	/// </summary>
	/// <returns>The CSV data.</returns>
	/// <param name="asset">Asset.</param>
	public static string[,] CSVToArray(TextAsset asset) {
		string[] rows = asset.text.Split('\n');
		// Resize rows
		while (string.IsNullOrEmpty(rows[rows.Length - 1])) {
			Array.Resize<string>(ref rows, rows.Length - 1);
		}
		
		// Find the amount of columns
		int columns = 0;
		for (int i = 0; i < rows.Length; i++) {
			columns = Mathf.Max(columns, SplitCSVRow(rows[i]).Length);
		}
		
		// Create new 2D string array and fill it with the csv data
		string[,] result = new string[columns, rows.Length];
		for (int row = 0; row < rows.Length; row++) {
			string[] rowData = SplitCSVRow(rows[row]); 
			for (int column = 0; column < rowData.Length; column++) {
				result[column, row] = rowData[column];
			}
		}
		
		return result;
	}

	/// <summary>
	/// Splits a CSV row into an array.
	/// </summary>
	/// <returns>The split CSV row as array.</returns>
	/// <param name="line">The CSV row to split.</param>
	private static string[] SplitCSVRow(string line) {
		return (
			from Match m in Regex.Matches(
				line, @"(((?<x>(?=[,\r\n]+))|""(?<x>([^""]|"""")+)""|(?<x>[^,\r\n]+)),?)",
				RegexOptions.ExplicitCapture)
			select m.Groups[1].Value
		).ToArray();
	}
}
