using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Globalization;

namespace Support_pedagogique_numerique
{
	/// <summary>
	/// Description résumée de Form_params.
	/// </summary>
	public class Form_params : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox tb_titre_bas;
		private System.Windows.Forms.TextBox tb_url_avt;
		private System.Windows.Forms.TextBox tb_copyright;
		private System.Windows.Forms.TextBox tb_avertissement;
		private System.Windows.Forms.TextBox tb_titre3;
		private System.Windows.Forms.TextBox tb_titre2;
		private System.Windows.Forms.TextBox tb_date;
		private System.Windows.Forms.TextBox tb_titre1;
		private System.Windows.Forms.Button btn_suivant;
		private System.Windows.Forms.Button btn_annuler;
		
		// Le document XML à modifier
		XmlDocument xmlDoc;
		XmlNode noeud_titre;
		XmlNode noeud_date;
		XmlNode noeud_titre2;
		XmlNode noeud_titre3;
		XmlNode noeud_avertissement;
		XmlNode noeud_url_avertissement;
		XmlNode noeud_titre_bas;
		XmlNode noeud_copyright;
		XmlNode noeud_info_auteur;
		XmlNode noeud_url_info_auteur;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.TextBox tb_info_auteur;
		private System.Windows.Forms.TextBox tb_url_info_auteur;
		private System.Windows.Forms.Label label12;
		private System.Windows.Forms.TextBox tb_texte_url;


		private System.ComponentModel.Container components = null;

		public Form_params()
		{
			//
			// Requis pour la prise en charge du Concepteur Windows Forms
			//
			InitializeComponent();

			//
			// TODO : ajoutez le code du constructeur après l'appel à InitializeComponent
			//
		}

		/// <summary>
		/// Nettoyage des ressources utilisées.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Code généré par le Concepteur Windows Form
		/// <summary>
		/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
		/// le contenu de cette méthode avec l'éditeur de code.
		/// </summary>
		private void InitializeComponent()
		{
			this.panel1 = new System.Windows.Forms.Panel();
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label9 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.tb_titre_bas = new System.Windows.Forms.TextBox();
			this.tb_url_avt = new System.Windows.Forms.TextBox();
			this.tb_copyright = new System.Windows.Forms.TextBox();
			this.tb_avertissement = new System.Windows.Forms.TextBox();
			this.tb_titre3 = new System.Windows.Forms.TextBox();
			this.tb_titre2 = new System.Windows.Forms.TextBox();
			this.tb_date = new System.Windows.Forms.TextBox();
			this.tb_titre1 = new System.Windows.Forms.TextBox();
			this.btn_suivant = new System.Windows.Forms.Button();
			this.btn_annuler = new System.Windows.Forms.Button();
			this.label10 = new System.Windows.Forms.Label();
			this.tb_info_auteur = new System.Windows.Forms.TextBox();
			this.label11 = new System.Windows.Forms.Label();
			this.tb_url_info_auteur = new System.Windows.Forms.TextBox();
			this.label12 = new System.Windows.Forms.Label();
			this.tb_texte_url = new System.Windows.Forms.TextBox();
			this.panel1.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.BackColor = System.Drawing.Color.White;
			this.panel1.Controls.Add(this.label1);
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(744, 48);
			this.panel1.TabIndex = 1;
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(312, 24);
			this.label1.TabIndex = 0;
			this.label1.Text = "General settings";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.label12);
			this.groupBox1.Controls.Add(this.tb_texte_url);
			this.groupBox1.Controls.Add(this.label11);
			this.groupBox1.Controls.Add(this.tb_url_info_auteur);
			this.groupBox1.Controls.Add(this.label10);
			this.groupBox1.Controls.Add(this.tb_info_auteur);
			this.groupBox1.Controls.Add(this.label9);
			this.groupBox1.Controls.Add(this.label8);
			this.groupBox1.Controls.Add(this.label7);
			this.groupBox1.Controls.Add(this.label6);
			this.groupBox1.Controls.Add(this.label4);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.label5);
			this.groupBox1.Controls.Add(this.tb_titre_bas);
			this.groupBox1.Controls.Add(this.tb_url_avt);
			this.groupBox1.Controls.Add(this.tb_copyright);
			this.groupBox1.Controls.Add(this.tb_avertissement);
			this.groupBox1.Controls.Add(this.tb_titre3);
			this.groupBox1.Controls.Add(this.tb_titre2);
			this.groupBox1.Controls.Add(this.tb_date);
			this.groupBox1.Controls.Add(this.tb_titre1);
			this.groupBox1.Location = new System.Drawing.Point(16, 56);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(712, 376);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Enter settings for the index page";
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(64, 280);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(96, 16);
			this.label9.TabIndex = 19;
			this.label9.Text = "Copyright";
			this.label9.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(64, 248);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(96, 16);
			this.label8.TabIndex = 18;
			this.label8.Text = "Bottom title";
			this.label8.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(56, 216);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(104, 16);
			this.label7.TabIndex = 17;
			this.label7.Text = "Warning URL";
			this.label7.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(72, 152);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(88, 16);
			this.label6.TabIndex = 16;
			this.label6.Text = "Warning";
			this.label6.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(64, 120);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(96, 16);
			this.label4.TabIndex = 14;
			this.label4.Text = "Subtitle 2";
			this.label4.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(64, 88);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(96, 16);
			this.label3.TabIndex = 13;
			this.label3.Text = "Subtitle 1";
			this.label3.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(64, 56);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(96, 16);
			this.label2.TabIndex = 12;
			this.label2.Text = "Date";
			this.label2.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(64, 24);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(96, 16);
			this.label5.TabIndex = 11;
			this.label5.Text = "Title";
			this.label5.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// tb_titre_bas
			// 
			this.tb_titre_bas.Location = new System.Drawing.Point(176, 248);
			this.tb_titre_bas.Name = "tb_titre_bas";
			this.tb_titre_bas.Size = new System.Drawing.Size(480, 20);
			this.tb_titre_bas.TabIndex = 6;
			this.tb_titre_bas.Text = "textBox11";
			// 
			// tb_url_avt
			// 
			this.tb_url_avt.Location = new System.Drawing.Point(176, 216);
			this.tb_url_avt.Name = "tb_url_avt";
			this.tb_url_avt.Size = new System.Drawing.Size(480, 20);
			this.tb_url_avt.TabIndex = 5;
			this.tb_url_avt.Text = "textBox8";
			// 
			// tb_copyright
			// 
			this.tb_copyright.Location = new System.Drawing.Point(176, 280);
			this.tb_copyright.Name = "tb_copyright";
			this.tb_copyright.Size = new System.Drawing.Size(480, 20);
			this.tb_copyright.TabIndex = 7;
			this.tb_copyright.Text = "textBox7";
			// 
			// tb_avertissement
			// 
			this.tb_avertissement.Location = new System.Drawing.Point(176, 152);
			this.tb_avertissement.Name = "tb_avertissement";
			this.tb_avertissement.Size = new System.Drawing.Size(480, 20);
			this.tb_avertissement.TabIndex = 4;
			this.tb_avertissement.Text = "textBox6";
			// 
			// tb_titre3
			// 
			this.tb_titre3.Location = new System.Drawing.Point(176, 120);
			this.tb_titre3.Name = "tb_titre3";
			this.tb_titre3.Size = new System.Drawing.Size(480, 20);
			this.tb_titre3.TabIndex = 3;
			this.tb_titre3.Text = "textBox4";
			// 
			// tb_titre2
			// 
			this.tb_titre2.Location = new System.Drawing.Point(176, 88);
			this.tb_titre2.Name = "tb_titre2";
			this.tb_titre2.Size = new System.Drawing.Size(480, 20);
			this.tb_titre2.TabIndex = 2;
			this.tb_titre2.Text = "textBox3";
			// 
			// tb_date
			// 
			this.tb_date.Location = new System.Drawing.Point(176, 56);
			this.tb_date.Name = "tb_date";
			this.tb_date.Size = new System.Drawing.Size(208, 20);
			this.tb_date.TabIndex = 1;
			this.tb_date.Text = "textBox2";
			// 
			// tb_titre1
			// 
			this.tb_titre1.Location = new System.Drawing.Point(176, 24);
			this.tb_titre1.Name = "tb_titre1";
			this.tb_titre1.Size = new System.Drawing.Size(480, 20);
			this.tb_titre1.TabIndex = 0;
			this.tb_titre1.Text = "textBox1";
			// 
			// btn_suivant
			// 
			this.btn_suivant.Location = new System.Drawing.Point(528, 440);
			this.btn_suivant.Name = "btn_suivant";
			this.btn_suivant.Size = new System.Drawing.Size(80, 24);
			this.btn_suivant.TabIndex = 8;
			this.btn_suivant.Text = "Next >";
			this.btn_suivant.Click += new System.EventHandler(this.btn_suivant_Click);
			// 
			// btn_annuler
			// 
			this.btn_annuler.Location = new System.Drawing.Point(624, 440);
			this.btn_annuler.Name = "btn_annuler";
			this.btn_annuler.Size = new System.Drawing.Size(80, 24);
			this.btn_annuler.TabIndex = 9;
			this.btn_annuler.Text = "Cancel";
			this.btn_annuler.Click += new System.EventHandler(this.btn_annuler_Click);
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(64, 312);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(96, 16);
			this.label10.TabIndex = 21;
			this.label10.Text = "Author info";
			this.label10.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// tb_info_auteur
			// 
			this.tb_info_auteur.Location = new System.Drawing.Point(176, 312);
			this.tb_info_auteur.Name = "tb_info_auteur";
			this.tb_info_auteur.Size = new System.Drawing.Size(480, 20);
			this.tb_info_auteur.TabIndex = 20;
			this.tb_info_auteur.Text = "textBox1";
			// 
			// label11
			// 
			this.label11.Location = new System.Drawing.Point(64, 344);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(96, 16);
			this.label11.TabIndex = 23;
			this.label11.Text = "Author info URL";
			this.label11.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// tb_url_info_auteur
			// 
			this.tb_url_info_auteur.Location = new System.Drawing.Point(176, 344);
			this.tb_url_info_auteur.Name = "tb_url_info_auteur";
			this.tb_url_info_auteur.Size = new System.Drawing.Size(480, 20);
			this.tb_url_info_auteur.TabIndex = 22;
			this.tb_url_info_auteur.Text = "textBox2";
			// 
			// label12
			// 
			this.label12.Location = new System.Drawing.Point(56, 184);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(104, 16);
			this.label12.TabIndex = 25;
			this.label12.Text = "Warning link text";
			this.label12.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// tb_texte_url
			// 
			this.tb_texte_url.Location = new System.Drawing.Point(176, 184);
			this.tb_texte_url.Name = "tb_texte_url";
			this.tb_texte_url.Size = new System.Drawing.Size(480, 20);
			this.tb_texte_url.TabIndex = 24;
			this.tb_texte_url.Text = "textBox8";
			// 
			// Form_params
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(742, 478);
			this.Controls.Add(this.btn_annuler);
			this.Controls.Add(this.btn_suivant);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.panel1);
			this.Name = "Form_params";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Documentation index generation tool";
			this.Load += new System.EventHandler(this.Form_params_Load);
			this.Closed += new System.EventHandler(this.Form_params_Closed);
			this.panel1.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void Form_params_Closed(object sender, System.EventArgs e)
		{
			Application.Exit();
		}

		private void btn_annuler_Click(object sender, System.EventArgs e)
		{
			Application.Exit();
		}

		private void Form_params_Load(object sender, System.EventArgs e)
		{
			// Chargement du fichier XML et peuplement des champs
			xmlDoc = new  XmlDocument();
			try 
			{
				xmlDoc.Load(Config.index_docbook);
			}
			catch(Exception ex) 
			{
				MessageBox.Show("Impossible de trouver le fichier"+ Config.index_docbook +". Celui-ci doit se trouver dans le même répertoire que cet outil.");
				this.Close();
				Application.Exit();
			}
			noeud_titre=xmlDoc.SelectSingleNode("article/title");
			noeud_date=xmlDoc.SelectSingleNode("article/articleinfo/date");
			noeud_titre2=xmlDoc.SelectSingleNode("article/articleinfo/abstract/title");
			noeud_titre3=xmlDoc.SelectSingleNode("article/articleinfo/abstract/para");
			noeud_avertissement=xmlDoc.SelectSingleNode("article/warning/title");
			noeud_url_avertissement=xmlDoc.SelectSingleNode("article/warning/para/ulink");
			noeud_titre_bas=xmlDoc.SelectSingleNode("/article/articleinfo/publisher/publishername");
			noeud_copyright=xmlDoc.SelectSingleNode("/article/articleinfo/corpauthor");
			noeud_info_auteur=xmlDoc.SelectSingleNode("/article/articleinfo/releaseinfo/ulink");
			noeud_url_info_auteur=xmlDoc.SelectSingleNode("/article/articleinfo/releaseinfo/ulink/@url");
			
			tb_titre1.Text=noeud_titre.InnerText;
			tb_date.Text=noeud_date.InnerText;
			tb_titre2.Text=noeud_titre2.InnerText;
			tb_titre3.Text=noeud_titre3.InnerText;
			tb_avertissement.Text=noeud_avertissement.InnerText;
			tb_texte_url.Text=noeud_url_avertissement.InnerText;
			if (!Config.docbook_existant) 
			{
				// Le docbookindex.xsl a rajouté automatiquement documents\ devant l'url de l'avertissement
				// on contourne ça en tronquant l'URL
				tb_url_avt.Text=noeud_url_avertissement.Attributes.Item(0).InnerText.Substring(10);			
			}
			else tb_url_avt.Text=noeud_url_avertissement.Attributes.Item(0).InnerText;
			tb_titre_bas.Text=noeud_titre_bas.InnerText;
			tb_copyright.Text=noeud_copyright.InnerText;
			tb_info_auteur.Text=noeud_info_auteur.InnerText;
			if (!Config.docbook_existant) 
			{
				// Le docbookindex.xsl a rajouté automatiquement documents\ devant l'url de l'info
				// on contourne ça en tronquant l'URL
				tb_url_info_auteur.Text=noeud_url_info_auteur.InnerText.Substring(10);
			}
			else tb_url_info_auteur.Text=noeud_url_info_auteur.InnerText;
		}

		private void btn_suivant_Click(object sender, System.EventArgs e)
		{
			// écriture des paramètres dans le fichier XML
			noeud_titre.InnerText=tb_titre1.Text;
			noeud_date.InnerText=tb_date.Text;
			noeud_titre2.InnerText=tb_titre2.Text;
			noeud_titre3.InnerText=tb_titre3.Text;
			noeud_url_avertissement.InnerText=tb_texte_url.Text;
			noeud_avertissement.InnerText=tb_avertissement.Text;
			noeud_url_avertissement.Attributes.Item(0).InnerText=tb_url_avt.Text;
			noeud_titre_bas.InnerText=tb_titre_bas.Text;
			noeud_copyright.InnerText=tb_copyright.Text;
			noeud_info_auteur.InnerText=tb_info_auteur.Text;
			noeud_url_info_auteur.InnerText=tb_url_info_auteur.Text;
			XmlTextWriter writer = new XmlTextWriter(Config.index_docbook, null);
			writer.Formatting = Formatting.Indented;
			xmlDoc.WriteContentTo(writer);
			writer.Close();
	
			Form_intro.frm_arbo = new Form_arbo();
			Form_intro.frm_arbo.Show();
			Form_intro.frm_arbo.Activate();
			this.Hide();
		}


	}
}
