using System;
using AutoMapper;
using LeafWeb.Core.Entities;

namespace LeafWeb.Core.Remote
{
	public class PiscalLeafOutputFile
	{
		public string Filename { get; set; }
		public byte[] Contents { get; set; }
		public string PiscalDirectoryName { get; set; }
		public int LeafInputId => PiscalUtility.GetIdFromDirectoryName(PiscalDirectoryName);
		// valid values: "touser", "nottouser", "clninput"
		public string OutputFileType { get; set; }

		static PiscalLeafOutputFile()
		{
			Mapper.CreateMap<PiscalLeafOutputFile, LeafOutputFile>()
				.ForMember(dest => dest.FileContents, opt => 
					opt.ResolveUsing(src => new LeafOutputFileContents {Contents = src.Contents}))
				.ForMember(dest => dest.FileType, opt =>
					opt.ResolveUsing(src =>
					{
						switch (src.OutputFileType)
						{
							case "touser":
								return LeafOutputFileType.ToUser;
							case "nottouser":
								return LeafOutputFileType.NotToUser;
							case "clninput":
								return LeafOutputFileType.CleanedInput;
							default:
								throw new ArgumentException();
						}
								
					}));
		}

		public PiscalLeafOutputFile() { }

		public LeafOutputFile GetLeafOutputFile()
		{
			var leafOutputFile = new LeafOutputFile();
			Mapper.Map(this, leafOutputFile);
			return leafOutputFile;
		}
	}
}