// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2012 Thomas Klose & Torsten Schache, Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.


using System;
using System.IO;  
using iTextSharp.text.pdf;
using System.Reflection;  

using Hiperscan.I18n;
using Hiperscan.Spectroscopy.IdentModule;


namespace Hiperscan.UpdateCertificate  
{  
	public class UpdateCertificate
	{  				
		public static void CreateCertificate(UpdateCertificateInfo update_info, string path)
		{
			try
			{
				if (Directory.Exists(path) == false)
					Directory.CreateDirectory(path);
                string out_file = Path.Combine(path, Extensions.ApoIdentCommon.DefaultNames.IdentServer.UPDATE_CERTIFICATE_FILE);

                string resource = Locale.ResourceFileRelativeToAssembly(Assembly.GetExecutingAssembly(), "Templates", 
                                                                        "update_certificate.pdf", false);

				string destination = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()+ ".pdf");
				File.Copy(resource, destination);

				Stream stream = File.Open(destination, FileMode.Open);
				PdfReader reader = new PdfReader(stream);

				using (FileStream outstream = new FileStream(out_file, FileMode.Create))  
				{
					PdfStamper stamper = new PdfStamper(reader, outstream);
						
					AcroFields form = stamper.AcroFields;
					if (!form.Fields.Keys.Contains("version") ||
						   !form.Fields.Keys.Contains("date"))
						throw new Exception("Invalid template: Missing fields.");

					form.SetField("version", update_info.Version);
                    form.SetField("date", Locale.LocalizedDate( update_info.Date));
					
					var info = reader.Info;
					info["Author"]       = "HiperScan GmbH";
                    info["CreationDate"] = Locale.LocalizedDate(DateTime.Now);
					stamper.MoreInfo = info;
					stamper.FormFlattening = true;
					stamper.Close();  
					reader.Close();  
				}

				try
				{
					File.Delete(destination);
				}
				catch (Exception)
				{
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex);
				/*
				string file = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
				file = Path.Combine(file, "uc_log.txt");
				using (StreamWriter sw = new StreamWriter(file, true))
				{
					sw.WriteLine(ex.ToString());
				}
				*/
			}
		}
	} 
}