using System;
using System.Configuration;
using System.IO;
using System.Linq;
using LeafWeb.Core.Remote;
using LeafWeb.Core.Utility;
using NUnit.Framework;

namespace LeafWeb.Core.Tests.Remote
{
	[TestFixture]
	public class PiscalSshClientTests
	{
		private const string ContentDirectory = @"Parsers\LeafInputData\";

		private readonly PiscalLeafInput _testInput =
			new PiscalLeafInput
			{
				LeafInputId = 1,
				PiscalDirectoryName = "TestDirectory",
				PhotosyntheticType = "C3_photosynthesis_leafweb",
				InputFiles = new[]
				{
					new PiscalLeafInputFile
					{
						Filename = "valid.csv", Contents = File.ReadAllBytes(FileUtility.GetContentFile(ContentDirectory, "LeafInput-valid.csv").FullName)
					}
				}
			};

		private readonly string _piscalConnectionString =
			ConfigurationManager.ConnectionStrings["PiscalServer"].ConnectionString;

		private PiscalSshClient GetTestClient()
		{
			return new PiscalSshClient(_piscalConnectionString);
		}

		[Test]
		public void Unconfigured()
        {
            Assert.Throws<ArgumentNullException>(() =>
            {
                var piscalSshClient = new PiscalSshClient(null);
            });
		}

		[Test, Explicit]
		public void SubmitLeafInputFile()
		{
			var client = GetTestClient();
			client.RunLeafInput(_testInput);
		}

		[Test, Explicit]
		public void GetLeafInputStatus()
		{
			var client = GetTestClient();
			var leafInputStatus = client.GetLeafInputStatus(_testInput);
			Console.WriteLine(leafInputStatus);
		}

		[Test, Explicit]
		public void RetrieveLeafOutput()
		{
			var client = GetTestClient();
			var result = client.RetrieveLeafOutput(_testInput).ToList();
			Console.WriteLine(string.Join(", ", result.Select(o => o.Filename)));
			//Console.WriteLine(result[0].Contents.GetString());
		}

		[Test, Explicit]
		public void CleanLeafOutput()
		{
			var client = GetTestClient();
			client.CleanupLeafProcess(_testInput);
		}

		[Test, Explicit]
		public void KillLeafProcess()
		{
			var client = GetTestClient();
			client.KillLeafProcess(_testInput);
		}
	}
}
