using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

using Chart;
using DataPerformer;
using DiagramUI;
using ToolBox;


namespace DataPerformerUI
{
	/// <summary>
	/// Summary description for FormGraphSave.
	/// </summary>
	public class FormGraphSave : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panelGraph;
		private System.Windows.Forms.MainMenu mainMenuPlot;
		private System.Windows.Forms.MenuItem menuItemFile;
		private System.Windows.Forms.MenuItem menuItemSaveGraph;
		private System.Windows.Forms.MenuItem menuItemFormat;
		private System.Windows.Forms.MenuItem menuItemFont;
		private System.Windows.Forms.SaveFileDialog saveFileDialogPlot;
		private System.Windows.Forms.SaveFileDialog saveFileDialogGraph;
		private ChartPerformer performer;
        private DataPerformer.Series series = new DataPerformer.Series();
        private MenuItem menuItemSaveXML;
        private IContainer components;

		public FormGraphSave()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

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

		public FormGraphSave(DataPerformer.Series series, ArrayList comments)
		{
			InitializeComponent();
			ResourceService.Resources.LoadControlResources(this);
            this.series.CopyFrom(series);
			//this.series.CopyFrom(series);
			PanelChart panel = new PanelChart(new int[,]{{80, 30}, {10, 40}});
			panel.Width = panelGraph.Width - 100;
			panel.Height = panelGraph.Height - 100;
			panel.Left = 50;
			panel.Top = 50;
			performer = panel.Performer;
			panelGraph.Controls.Add(panel);
			SimpleCoordinator coordinator = new SimpleCoordinator(5, 5, performer);
			performer.Coordinator = coordinator;
			EditorReceiver.AddEditorDrag(panelGraph);
			PictureReceiver.AddImageDrag(panelGraph);
			ControlPanel.LoadControls(panelGraph, comments);
            ISeries s = new SeriesGraph(series);
            performer.AddSeries(s, Color.Magenta);
            /*DataPerformer.Series s = new DataPerformer.Series();
			s.CopyFrom(this.series);
			s.PenColor = Color.Red;
			performer.AddSeries(s);
			performer.RefreshAll();*/
            performer.RefreshAll();
		}


		/// <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.components = new System.ComponentModel.Container();
            this.panelGraph = new System.Windows.Forms.Panel();
            this.mainMenuPlot = new System.Windows.Forms.MainMenu(this.components);
            this.menuItemFile = new System.Windows.Forms.MenuItem();
            this.menuItemSaveGraph = new System.Windows.Forms.MenuItem();
            this.menuItemSaveXML = new System.Windows.Forms.MenuItem();
            this.menuItemFormat = new System.Windows.Forms.MenuItem();
            this.menuItemFont = new System.Windows.Forms.MenuItem();
            this.saveFileDialogPlot = new System.Windows.Forms.SaveFileDialog();
            this.saveFileDialogGraph = new System.Windows.Forms.SaveFileDialog();
            this.SuspendLayout();
            // 
            // panelGraph
            // 
            this.panelGraph.BackColor = System.Drawing.Color.White;
            this.panelGraph.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panelGraph.Location = new System.Drawing.Point(24, 40);
            this.panelGraph.Name = "panelGraph";
            this.panelGraph.Size = new System.Drawing.Size(900, 500);
            this.panelGraph.TabIndex = 20;
            // 
            // mainMenuPlot
            // 
            this.mainMenuPlot.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItemFile,
            this.menuItemFormat});
            // 
            // menuItemFile
            // 
            this.menuItemFile.Index = 0;
            this.menuItemFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItemSaveGraph,
            this.menuItemSaveXML});
            this.menuItemFile.Text = "File";
            // 
            // menuItemSaveGraph
            // 
            this.menuItemSaveGraph.Index = 0;
            this.menuItemSaveGraph.Text = "Save graph as";
            this.menuItemSaveGraph.Click += new System.EventHandler(this.menuItemSaveGraph_Click);
            // 
            // menuItemSaveXML
            // 
            this.menuItemSaveXML.Index = 1;
            this.menuItemSaveXML.Text = "Save as XML";
            this.menuItemSaveXML.Click += new System.EventHandler(this.menuItemSaveXML_Click);
            // 
            // menuItemFormat
            // 
            this.menuItemFormat.Index = 1;
            this.menuItemFormat.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItemFont});
            this.menuItemFormat.Text = "Format";
            // 
            // menuItemFont
            // 
            this.menuItemFont.Index = 0;
            this.menuItemFont.Text = "Font";
            this.menuItemFont.Click += new System.EventHandler(this.menuItemFont_Click);
            // 
            // saveFileDialogPlot
            // 
            this.saveFileDialogPlot.Filter = "XML Function files |*.xml";
            // 
            // saveFileDialogGraph
            // 
            this.saveFileDialogGraph.Filter = "Graph files |*.gra";
            // 
            // FormGraphSave
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(976, 589);
            this.Controls.Add(this.panelGraph);
            this.Menu = this.mainMenuPlot;
            this.Name = "FormGraphSave";
            this.Text = "Save graph";
            this.ResumeLayout(false);

		}
		#endregion

		private void menuItemFont_Click(object sender, System.EventArgs e)
		{
			try
			{
				TextBox box = ControlPanel.GetActiveTextBox(panelGraph);
				if (box == null)
				{
					return;
				}
				FontDialog dlg = new FontDialog();
				dlg.ShowDialog(this);
				Font font = dlg.Font;
				box.Font = font;
			}
			catch (Exception ex)
			{
				DefaultForm.ShowError(this, ex);
			}

		}

		private void menuItemSaveGraph_Click(object sender, System.EventArgs e)
		{
			try
			{
				if (performer.Count == 0)
				{
					return;
				}
				saveFileDialogGraph.InitialDirectory = ResourceService.Resources.CurrentDirectory + "Series";
				if (saveFileDialogGraph.ShowDialog(this) != DialogResult.OK)
				{
					return;
				}
				Stream stream = File.Open(saveFileDialogGraph.FileName, FileMode.Create);
				BinaryFormatter bformatter = new BinaryFormatter();
				ArrayList comments = ControlPanel.GetControls(panelGraph);
				series.Comments = comments;
				bformatter.Serialize(stream, series);
				stream.Close();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
			}
			catch (Exception ex)
			{
				DiagramUI.DefaultForm.ShowError(this, ex);
			}
		}

        private void menuItemSaveXML_Click(object sender, EventArgs e)
        {
            try
            {
                if (performer.Count == 0)
                {
                    return;
                }
                if (saveFileDialogPlot.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }
                XmlDocument doc = new XmlDocument();
                doc.LoadXml("<Root/>");
                XmlElement el = series.CreateXml(doc);
                doc.DocumentElement.AppendChild(el);
                doc.Save(saveFileDialogPlot.FileName);
            }
            catch (Exception ex)
            {
                DiagramUI.DefaultForm.ShowError(this, ex);
            }

        }



	}


}
