using System;
using System.Collections.Generic;
using System.IO;
using CsvHelper;
using LeafWeb.Core.Entities;
using LeafWeb.Core.Utility;

namespace LeafWeb.Core.Parsers
{
	public class LeafInputCsvParser : CsvParserBase
	{
		public LeafInputCsvParser(FileSystemInfo csvFile) : base(csvFile)
		{
		}

		public LeafInputCsvParser(byte[] fileContents) : base(fileContents)
		{
		}

		public LeafInputData Parse()
		{
			// First 10 lines
			var leafInput = ParseLeafInput();

			// filename will be stored in LeafInputFile
			//leafInput.FileName = CsvFile.Name;

			// Next 3 (Header, Units, and Values)
			leafInput.Site = ParseLeafInputSite();

			// Next 3 (Header, Units, and Values)
			leafInput.Photosynthetic = ParseLeafInputPhotosynthetic();

			// Remaining lines (Header, Units, and Data)
			leafInput.Data = ParseLeafInputData();
			
			return leafInput;
		}

		private LeafInputData ParseLeafInput()
		{
			var items = new List<string>();
			for (var i = 0; i < 10; i++)
			{
				if (!CsvReader.Read())
					throw new ParseException("Could not read line number " + CsvReader.Context.Row);

				string field;
				if (!CsvReader.TryGetField(0, out field))
					throw new ParseException("Could not read first field on line number " + CsvReader.Context.Row);
				items.Add(field);
			}
			return ParsedObjectFactory<LeafInputData>.Create(items.ToArray());
		}

		private LeafInputDataSite ParseLeafInputSite()
		{
			var titles = GetNextCsvRowValues();
			if (titles == null)
				throw new ParseException("Could not read site header row on line number " + CsvReader.Context.Row);
			var units = GetNextCsvRowValues();
			if (units == null)
				throw new ParseException("Could not read site units row on line number " + CsvReader.Context.Row);
			var values = GetNextCsvRowValues();
			if (values == null)
				throw new ParseException("Could not read site value row on line number " + CsvReader.Context.Row);
		
			var items = new List<Tuple<string, string>>();
			for (var i = 0; i < titles.Length && i < values.Length; i++)
			{
				var item = Tuple.Create(titles[i], values[i]);
				items.Add(item);
			}
			return ParsedObjectFactory<LeafInputDataSite>.Create(items.ToArray());
		}

		private LeafInputDataPhotosynthetic ParseLeafInputPhotosynthetic()
		{
			var titles = GetNextCsvRowValues();
			if (titles == null)
				throw new ParseException("Could not read photosynthetic header row on line number " + CsvReader.Context.Row);
			var units = GetNextCsvRowValues();
			if (units == null)
				throw new ParseException("Could not read photosynthetic units row on line number " + CsvReader.Context.Row);
			var values = GetNextCsvRowValues();
			if (values == null)
				throw new ParseException("Could not read photosynthetic value row on line number " + CsvReader.Context.Row);
		
			var items = new List<Tuple<string, string>>();
			for (var i = 0; i < titles.Length && i < values.Length; i++)
			{
				var item = Tuple.Create(titles[i], values[i]);
				items.Add(item);
			}
			return ParsedObjectFactory<LeafInputDataPhotosynthetic>.Create(items.ToArray());
		}

		private LeafInputDataCurve[] ParseLeafInputData()
		{
			var titles = GetNextCsvRowValues();
			if (titles == null)
				throw new ParseException("Could not read data header row on line number " + CsvReader.Context.Row);
			var units = GetNextCsvRowValues();
			if (units == null)
				throw new ParseException("Could not read data units row on line number " + CsvReader.Context.Row);

			var valueArrays = new List<string[]>();
			while (true)
			{
				var values = GetNextCsvRowValues();
				if (values == null)
					break;
				valueArrays.Add(values);
			}
			return ParsedObjectFactory<LeafInputDataCurve>.Create(titles, valueArrays.ToArray());
		}

		public static void ExportCsv(string filename, IEnumerable<LeafInputData> leafInputs)
		{
			using (var textWriter = new StreamWriter(filename))
			{
				var csv = new CsvWriter(textWriter);
				csv.WriteRecords(leafInputs);
			}
		}
	}
}
