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


using CategoryTheory;
using DiagramUI;
using FormulaEditor;
using ToolBox;
using Chart;
using DataTableSelection;
using DataSetService;
using DataPerformer;



namespace DataPerformerUI
{
	/// <summary>
	/// Summary description for FormSeries.
	/// </summary>
	public class FormSeriesTable : System.Windows.Forms.Form, IUpdatableForm
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
        private IObjectLabel label;
        private DataTableSeries series;
		private System.Windows.Forms.OpenFileDialog openFileDialogGraph;
        private System.Windows.Forms.SaveFileDialog saveFileDialogGraph;
        private MenuStrip menuStripMain;
        private ToolStrip toolStripMain;
        private ToolStripMenuItem fileToolStripMenuItem;
        private ToolStripMenuItem openToolStripMenuItem;
        private ToolStripMenuItem loadfromdatabaseToolStripMenuItem;
        private ToolStripMenuItem saveasToolStripMenuItem;
        private Panel panelLeft;
        private Panel panelBottom;
        private Label labelConstStep;
        private Panel panelTop;
		private ChartPerformer performer;
        private ToolStripButton toolStripButtonOpen;
        private ToolStripButton toolStripButtonSave;

        private OfficePickers.ColorPicker.ToolStripColorPicker pic = new OfficePickers.ColorPicker.ToolStripColorPicker();
        private ToolStripComboBox toolStripButtonType;
        private ToolStripComboBox toolStripButtonX;
        private ToolStripComboBox toolStripButtonY;
        private ToolStripButton toolStripButtonRefresh;
        private Panel panelCenterLeft;
        private Panel panelCenterRight;
        private Panel panelGraph;

        private readonly string[] modes = new string[] { "Lines", "Crosses" };


		private FormSeriesTable()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

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

        public FormSeriesTable(IObjectLabel label)
		{
			InitializeComponent();
			ResourceService.Resources.LoadControlResources(this);
			this.label = label;
            series = label.Object as DataTableSeries;

			//ser.PenColor = Color.Magenta;
			PanelChart panel = new PanelChart(new int[,]{{80, 20}, {30, 40}});
			panel.Width = panelGraph.Width - 100;
			panel.Height = panelGraph.Height - 100;
			panel.Top = 50;
			panel.Left = 50;
            panel.Dock = DockStyle.Fill;
			performer = panel.Performer;
			performer.Resize();
			panelGraph.Controls.Add(panel);
			ArrayList comments = series.Comments;
			ControlPanel.LoadControls(panelGraph, comments);
			SimpleCoordinator coordinator = new SimpleCoordinator(5, 5, performer);
			performer.Coordinator = coordinator;
			UpdateFormUI();
			//performer.AddSeries(ser, Color.Magenta);
			//performer.RefreshAll();
            fill();
			if (series != null)
			{
				labelConstStep.Text = series.HasEqualSteps;
			}
			//if (EMDatabaseInterface.DatabaseClient.Service == null)
		//	{
				//menuItemLoadFromDatabase.Visible = false;
		//	}

            foreach (string mode in modes)
            {
                string m = ResourceService.Resources.GetControlResource(mode);
                toolStripButtonType.Items.Add(m);
            }
            pic.Text = ResourceService.Resources.GetControlResource("Color");
            pic.Width = 70;
            toolStripMain.Items.Add(pic);
		}

        private void fill()
        {
          //IDataSetConsumer cons = series;
          //DataSet dataSet = cons.
            List<string> names = series.Names;
            DefaultForm.FillCombo(toolStripButtonX, names);
            DefaultForm.FillCombo(toolStripButtonY, names);
            DefaultForm.SelectCombo(toolStripButtonX, series.X);
            DefaultForm.SelectCombo(toolStripButtonY, series.Y);
        }



        private void show(string mode, Color color)
        {
            if (performer.Count > 0)
            {
                return;
            }
            SeriesGraph ser = new SeriesGraph(series);
            if (mode.Equals(ResourceService.Resources.GetControlResource("Lines")))
            {
                performer.AddSeries(ser, color);
            }
            else
            {
                ISeriesPainter p = null;
                if (mode.Equals(ResourceService.Resources.GetControlResource("Crosses")))
                {
                    p = new CrossSeriesPainter(performer, color);
                }
                if (p != null)
                {
                    performer.AddSeries(ser, p);
                }
            }
            performer.RefreshAll();

        }


        private void clear()
        {
            performer.RemoveAll();
            Refresh();
        }


		public void UpdateFormUI()
		{
			Text = label.RootName;//NamedComponent.GetText(label);
		}
		
		
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			//NamedComponent.RemoveForm(label);
			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()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormSeriesTable));
            this.openFileDialogGraph = new System.Windows.Forms.OpenFileDialog();
            this.saveFileDialogGraph = new System.Windows.Forms.SaveFileDialog();
            this.menuStripMain = new System.Windows.Forms.MenuStrip();
            this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.loadfromdatabaseToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.saveasToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripMain = new System.Windows.Forms.ToolStrip();
            this.toolStripButtonOpen = new System.Windows.Forms.ToolStripButton();
            this.toolStripButtonSave = new System.Windows.Forms.ToolStripButton();
            this.toolStripButtonRefresh = new System.Windows.Forms.ToolStripButton();
            this.toolStripButtonType = new System.Windows.Forms.ToolStripComboBox();
            this.toolStripButtonX = new System.Windows.Forms.ToolStripComboBox();
            this.toolStripButtonY = new System.Windows.Forms.ToolStripComboBox();
            this.panelLeft = new System.Windows.Forms.Panel();
            this.panelBottom = new System.Windows.Forms.Panel();
            this.labelConstStep = new System.Windows.Forms.Label();
            this.panelTop = new System.Windows.Forms.Panel();
            this.panelCenterLeft = new System.Windows.Forms.Panel();
            this.panelCenterRight = new System.Windows.Forms.Panel();
            this.panelGraph = new System.Windows.Forms.Panel();
            this.menuStripMain.SuspendLayout();
            this.toolStripMain.SuspendLayout();
            this.panelBottom.SuspendLayout();
            this.SuspendLayout();
            // 
            // saveFileDialogGraph
            // 
            this.saveFileDialogGraph.Filter = "Graph files |*.gra";
            // 
            // menuStripMain
            // 
            this.menuStripMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem});
            this.menuStripMain.Location = new System.Drawing.Point(0, 0);
            this.menuStripMain.Name = "menuStripMain";
            this.menuStripMain.Size = new System.Drawing.Size(1040, 24);
            this.menuStripMain.TabIndex = 2;
            this.menuStripMain.Text = "menuStripMain";
            // 
            // fileToolStripMenuItem
            // 
            this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.openToolStripMenuItem,
            this.loadfromdatabaseToolStripMenuItem,
            this.saveasToolStripMenuItem});
            this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            this.fileToolStripMenuItem.Size = new System.Drawing.Size(35, 20);
            this.fileToolStripMenuItem.Text = "File";
            // 
            // openToolStripMenuItem
            // 
            this.openToolStripMenuItem.Name = "openToolStripMenuItem";
            this.openToolStripMenuItem.Size = new System.Drawing.Size(181, 22);
            this.openToolStripMenuItem.Text = "Open";
            this.openToolStripMenuItem.Click += new System.EventHandler(this.openToolStripMenuItem_Click);
            // 
            // loadfromdatabaseToolStripMenuItem
            // 
            this.loadfromdatabaseToolStripMenuItem.Name = "loadfromdatabaseToolStripMenuItem";
            this.loadfromdatabaseToolStripMenuItem.Size = new System.Drawing.Size(181, 22);
            this.loadfromdatabaseToolStripMenuItem.Text = "Load from database";
            // 
            // saveasToolStripMenuItem
            // 
            this.saveasToolStripMenuItem.Name = "saveasToolStripMenuItem";
            this.saveasToolStripMenuItem.Size = new System.Drawing.Size(181, 22);
            this.saveasToolStripMenuItem.Text = "Save as";
            this.saveasToolStripMenuItem.Click += new System.EventHandler(this.saveasToolStripMenuItem_Click);
            // 
            // toolStripMain
            // 
            this.toolStripMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripButtonOpen,
            this.toolStripButtonSave,
            this.toolStripButtonRefresh,
            this.toolStripButtonType,
            this.toolStripButtonX,
            this.toolStripButtonY});
            this.toolStripMain.Location = new System.Drawing.Point(0, 24);
            this.toolStripMain.Name = "toolStripMain";
            this.toolStripMain.Size = new System.Drawing.Size(1040, 25);
            this.toolStripMain.TabIndex = 3;
            this.toolStripMain.Text = "toolStrip1";
            // 
            // toolStripButtonOpen
            // 
            this.toolStripButtonOpen.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripButtonOpen.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButtonOpen.Image")));
            this.toolStripButtonOpen.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButtonOpen.Name = "toolStripButtonOpen";
            this.toolStripButtonOpen.Size = new System.Drawing.Size(23, 22);
            this.toolStripButtonOpen.Text = "toolStripButton1";
            this.toolStripButtonOpen.Click += new System.EventHandler(this.toolStripButtonOpen_Click);
            // 
            // toolStripButtonSave
            // 
            this.toolStripButtonSave.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripButtonSave.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButtonSave.Image")));
            this.toolStripButtonSave.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButtonSave.Name = "toolStripButtonSave";
            this.toolStripButtonSave.Size = new System.Drawing.Size(23, 22);
            this.toolStripButtonSave.Text = "toolStripButton1";
            this.toolStripButtonSave.Click += new System.EventHandler(this.toolStripButtonSave_Click);
            // 
            // toolStripButtonRefresh
            // 
            this.toolStripButtonRefresh.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripButtonRefresh.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButtonRefresh.Image")));
            this.toolStripButtonRefresh.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButtonRefresh.Name = "toolStripButtonRefresh";
            this.toolStripButtonRefresh.Size = new System.Drawing.Size(23, 22);
            this.toolStripButtonRefresh.Text = "toolStripButton1";
            this.toolStripButtonRefresh.Click += new System.EventHandler(this.toolStripButtonRefresh_Click);
            // 
            // toolStripButtonType
            // 
            this.toolStripButtonType.Name = "toolStripButtonType";
            this.toolStripButtonType.Size = new System.Drawing.Size(121, 25);
            this.toolStripButtonType.Text = "<Series type>";
            // 
            // toolStripButtonX
            // 
            this.toolStripButtonX.Name = "toolStripButtonX";
            this.toolStripButtonX.Size = new System.Drawing.Size(121, 25);
            this.toolStripButtonX.Text = "<X>";
            // 
            // toolStripButtonY
            // 
            this.toolStripButtonY.Name = "toolStripButtonY";
            this.toolStripButtonY.Size = new System.Drawing.Size(121, 25);
            this.toolStripButtonY.Text = "<Y>";
            // 
            // panelLeft
            // 
            this.panelLeft.Dock = System.Windows.Forms.DockStyle.Left;
            this.panelLeft.Location = new System.Drawing.Point(0, 49);
            this.panelLeft.Name = "panelLeft";
            this.panelLeft.Size = new System.Drawing.Size(16, 568);
            this.panelLeft.TabIndex = 4;
            // 
            // panelBottom
            // 
            this.panelBottom.Controls.Add(this.labelConstStep);
            this.panelBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panelBottom.Location = new System.Drawing.Point(16, 571);
            this.panelBottom.Name = "panelBottom";
            this.panelBottom.Size = new System.Drawing.Size(1024, 46);
            this.panelBottom.TabIndex = 5;
            // 
            // labelConstStep
            // 
            this.labelConstStep.Location = new System.Drawing.Point(98, 19);
            this.labelConstStep.Name = "labelConstStep";
            this.labelConstStep.Size = new System.Drawing.Size(664, 23);
            this.labelConstStep.TabIndex = 2;
            this.labelConstStep.Text = "label1";
            // 
            // panelTop
            // 
            this.panelTop.Dock = System.Windows.Forms.DockStyle.Top;
            this.panelTop.Location = new System.Drawing.Point(16, 49);
            this.panelTop.Name = "panelTop";
            this.panelTop.Size = new System.Drawing.Size(1024, 29);
            this.panelTop.TabIndex = 6;
            // 
            // panelCenterLeft
            // 
            this.panelCenterLeft.Dock = System.Windows.Forms.DockStyle.Left;
            this.panelCenterLeft.Location = new System.Drawing.Point(16, 78);
            this.panelCenterLeft.Name = "panelCenterLeft";
            this.panelCenterLeft.Size = new System.Drawing.Size(11, 493);
            this.panelCenterLeft.TabIndex = 7;
            // 
            // panelCenterRight
            // 
            this.panelCenterRight.Dock = System.Windows.Forms.DockStyle.Right;
            this.panelCenterRight.Location = new System.Drawing.Point(1030, 78);
            this.panelCenterRight.Name = "panelCenterRight";
            this.panelCenterRight.Size = new System.Drawing.Size(10, 493);
            this.panelCenterRight.TabIndex = 8;
            // 
            // panelGraph
            // 
            this.panelGraph.BackColor = System.Drawing.SystemColors.Window;
            this.panelGraph.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panelGraph.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panelGraph.Location = new System.Drawing.Point(27, 78);
            this.panelGraph.Name = "panelGraph";
            this.panelGraph.Size = new System.Drawing.Size(1003, 493);
            this.panelGraph.TabIndex = 9;
            // 
            // FormSeriesTable
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(1040, 617);
            this.Controls.Add(this.panelGraph);
            this.Controls.Add(this.panelCenterRight);
            this.Controls.Add(this.panelCenterLeft);
            this.Controls.Add(this.panelTop);
            this.Controls.Add(this.panelBottom);
            this.Controls.Add(this.panelLeft);
            this.Controls.Add(this.toolStripMain);
            this.Controls.Add(this.menuStripMain);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MainMenuStrip = this.menuStripMain;
            this.Name = "FormSeriesTable";
            this.Text = "Series";
            this.Activated += new System.EventHandler(this.FormSeries_Activated);
            this.menuStripMain.ResumeLayout(false);
            this.menuStripMain.PerformLayout();
            this.toolStripMain.ResumeLayout(false);
            this.toolStripMain.PerformLayout();
            this.panelBottom.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void draw()
		{
			performer.RefreshAll();
		}

		private void menuItemOpen_Click(object sender, System.EventArgs e)
		{
			openFileDialogGraph.InitialDirectory = ResourceService.Resources.CurrentDirectory + "\\Series";
			if (openFileDialogGraph.ShowDialog(this) != DialogResult.OK)
			{
				return;
			}
            BinaryFormatter form = new BinaryFormatter();
            Stream stream = File.OpenRead(openFileDialogGraph.FileName);
            Series s = form.Deserialize(stream) as Series;
            stream.Close();
            series.CopyFrom(s);
            series.Comments = s.Comments;
            ControlPanel.ClearComments(panelGraph);
            ControlPanel.LoadControls(panelGraph, series.Comments); 
/*			Stream stream = File.OpenRead(openFileDialogGraph.FileName);
			DataPerformer.Series s = Loaders.GraphLoader.Serialize(stream, true);
			if (s == null)
			{
				stream.Close();
				stream = File.OpenRead(openFileDialogGraph.FileName);
				s = Loaders.GraphLoader.Serialize(stream, false);
			}
			stream.Close();
			series.CopyFrom(s);
			performer[0].CopyFrom(s);
			ControlPanel.ClearComments(panelGraph);
			ArrayList comments = s.Comments;
			series.Comments = comments;
			ControlPanel.LoadControls(panelGraph, comments);
			performer.RefreshAll();
			if (series != null)
			{
				labelConstStep.Text = series.HasEqualSteps;
			}*/
		}

		private void menuItemSave_Click(object sender, System.EventArgs e)
		{
			if (saveFileDialogGraph.ShowDialog(this) != DialogResult.OK)
			{
				return;
			}
			DataPerformer.Series s = new DataPerformer.Series();
			s.CopyFrom(series);
			BinaryFormatter f = new BinaryFormatter();
			Stream stream = File.OpenWrite(saveFileDialogGraph.FileName);
			f.Serialize(stream, s);
			stream.Close();
		}

		private void menuItemLoadFromDatabase_Click(object sender, System.EventArgs e)
		{
			/*EMDatabaseInterface.FormDatabaseGraph form = new EMDatabaseInterface.FormDatabaseGraph(null);
			form.ShowDialog(this);
			if (form.Accepted)
			{
				Series s = form.Series;
				series.CopyFrom(s);
				performer[0].CopyFrom(s);
				series.Comments = s.Comments;
				ControlPanel.ClearComments(panelGraph);
				ControlPanel.LoadControls(panelGraph, s.Comments);
				performer.RefreshAll();
				if (series != null)
				{
					labelConstStep.Text = series.HasEqualSteps;
				}
			}*/
		}

		private void FormSeries_Activated(object sender, System.EventArgs e)
		{
			ControlPanel.DeactivateAll(panelGraph);
		}

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            menuItemOpen_Click(sender, e);
            
        }

        private void toolStripButtonOpen_Click(object sender, EventArgs e)
        {
            menuItemOpen_Click(sender, e);
        }

        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            menuItemSave_Click(sender, e);
        }

        private void saveasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            menuItemSave_Click(sender, e);
        }

 
 
 
        private void toolStripButtonRefresh_Click(object sender, EventArgs e)
        {
            string x = toolStripButtonX.SelectedItem + "";
            string y = toolStripButtonY.SelectedItem + "";
            series.Set(x, y);
            string mode = toolStripButtonType.SelectedItem + "";
            show(mode, pic.Color);

        }

	}
}
