using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.Office.Core;
using ppt = Microsoft.Office.Interop.PowerPoint;


namespace vclass.presentation
{
	/// <summary>
	/// Summary description for PresentationController.
	/// </summary>
	public class PresentationController : System.Windows.Forms.Form
	{
		private ppt.Application powerpoint = new ppt.Application();
		private ppt.Presentation currentPresentation;
		private System.Windows.Forms.Button OpenButton;
		private System.Windows.Forms.Button previousButton;
		private System.Windows.Forms.Button nextButton;
		private System.Windows.Forms.TextBox slideNoTextBox;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private PresentationForm presentationForm;
		private System.Resources.ResourceManager resources;
		private String currentFileName;
		private String directory = "";
		//private ppt._Slide currentSlide;


		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public PresentationController(PresentationForm form)
		{
			//
			// Required for Windows Form Designer support
			//
			presentationForm = form;
			resources = new System.Resources.ResourceManager(typeof(PresentationController));
			
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.OpenButton = new System.Windows.Forms.Button();
			this.previousButton = new System.Windows.Forms.Button();
			this.nextButton = new System.Windows.Forms.Button();
			this.slideNoTextBox = new System.Windows.Forms.TextBox();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.SuspendLayout();
			// 
			// OpenButton
			// 
			this.OpenButton.Location = new System.Drawing.Point(8, 8);
			this.OpenButton.Name = "OpenButton";
			this.OpenButton.Size = new System.Drawing.Size(40, 24);
			this.OpenButton.TabIndex = 0;
			this.OpenButton.Text = "Open";
			this.OpenButton.Click += new System.EventHandler(this.OpenButton_Click);
			// 
			// previousButton
			// 
			this.previousButton.Location = new System.Drawing.Point(56, 8);
			this.previousButton.Name = "previousButton";
			this.previousButton.Size = new System.Drawing.Size(24, 24);
			this.previousButton.TabIndex = 1;
			this.previousButton.Text = "<";
			this.previousButton.Click += new System.EventHandler(this.previousButton_Click);
			// 
			// nextButton
			// 
			this.nextButton.Location = new System.Drawing.Point(120, 8);
			this.nextButton.Name = "nextButton";
			this.nextButton.Size = new System.Drawing.Size(24, 24);
			this.nextButton.TabIndex = 2;
			this.nextButton.Text = ">";
			this.nextButton.Click += new System.EventHandler(this.nextButton_Click);
			// 
			// slideNoTextBox
			// 
			this.slideNoTextBox.Location = new System.Drawing.Point(88, 8);
			this.slideNoTextBox.Multiline = true;
			this.slideNoTextBox.Name = "slideNoTextBox";
			this.slideNoTextBox.Size = new System.Drawing.Size(24, 24);
			this.slideNoTextBox.TabIndex = 3;
			this.slideNoTextBox.Text = "0";
			this.slideNoTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			this.slideNoTextBox.TextChanged += new System.EventHandler(this.slideNoTextBox_TextChanged);
			// 
			// PresentationController
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(200, 38);
			this.Controls.Add(this.slideNoTextBox);
			this.Controls.Add(this.nextButton);
			this.Controls.Add(this.previousButton);
			this.Controls.Add(this.OpenButton);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "PresentationController";
			this.Text = "Presentation Controller";
			this.ResumeLayout(false);

		}
		#endregion

		private void OpenButton_Click(object sender, System.EventArgs e)
		{
			try
			{
				openFileDialog1.InitialDirectory = "c:\\yavuz";
				openFileDialog1.ShowDialog();
				Console.WriteLine("file name is " + openFileDialog1.FileName);
				currentFileName = openFileDialog1.FileName;
				if(powerpoint.Active == MsoTriState.msoFalse)
				{
					powerpoint.Activate();
					powerpoint.WindowState = ppt.PpWindowState.ppWindowMinimized;
				}
				currentPresentation = powerpoint.Presentations.Open(currentFileName, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoTrue);
				currentPresentation.SaveAs(currentFileName.Replace("ppt", "gif") , ppt.PpSaveAsFileType.ppSaveAsGIF, MsoTriState.msoTrue);
				directory = currentFileName.Replace(".ppt", "");
				presentationForm.setPicture( System.Drawing.Image.FromFile(directory + "\\Slide1.GIF"));
				slideNoTextBox.Text = "1";
			}
			catch(Exception exc)
			{
				Console.WriteLine("the exception is : " + exc.Message);
			}
		}

		private void nextButton_Click(object sender, System.EventArgs e)
		{
			int tmpno = Convert.ToInt32(slideNoTextBox.Text) + 1;
			if(tmpno > currentPresentation.Slides.Count)
				tmpno--;
			slideNoTextBox.Text = Convert.ToString(tmpno); 
			loadSlide();
		}

		private void previousButton_Click(object sender, System.EventArgs e)
		{
			int tmpno = Convert.ToInt32(slideNoTextBox.Text) - 1;
			if(tmpno <= 0)
				tmpno = 1;
			slideNoTextBox.Text = Convert.ToString(tmpno);
			loadSlide();
		}

		private void slideNoTextBox_TextChanged(object sender, System.EventArgs e)
		{
			int tmpno = 0;
			try
			{
				tmpno = Convert.ToInt32(slideNoTextBox.Text);
			}
			catch(Exception ex){
				Console.WriteLine(ex.StackTrace);
				slideNoTextBox.Text = "1";
				tmpno = Convert.ToInt32(slideNoTextBox.Text);
			}
			if(tmpno <= 0)
				slideNoTextBox.Text = "1";
			else if(tmpno > currentPresentation.Slides.Count)
				slideNoTextBox.Text = Convert.ToString(currentPresentation.Slides.Count);
			loadSlide();
		}

		public void loadSlide(){
			if(slideNoTextBox.Text == "" || Convert.ToInt32( slideNoTextBox.Text) == 0)
				return;
			int tmpno = Convert.ToInt32(slideNoTextBox.Text);
			System.Drawing.Image slide = System.Drawing.Image.FromFile(directory + "\\Slide" + tmpno + ".GIF");
			presentationForm.setPicture(slide);
		}

	}
}
