/******************************************************************************
 * BrowseForm.cs 
 * 
 * <Copyright and License> 
 * 
 * This file is part of GODS, a package for annotating medical images. 
 * 
 * Copyright (C) 2004 
 * Xuedong Chen 
 * Heri Mariwa 
 * Kimmy (Hsiang-ling) Lin 
 * Rui Yu 
 * 
 * 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 2 
 * 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, write to the Free Software 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 *  
 * For further questions about this file, please send email to 
 * gods@cs.umb.edu or visit us at http://gods.sourceforge.net.  
 ******************************************************************************/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace GODS
{
	using DBHandler;
	/// <summary>
	/// Summary description for BrowseForm.
	/// </summary>
	public class BrowseForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label lbMRN;
		private System.Windows.Forms.TextBox FdApptDT;
		private System.Windows.Forms.Label lbApptDT;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.TextBox textBox1;
		private ArrayList mrns;
		private ArrayList dates;
		private ImageData[] imageData;
		private int curMrnIndex,curAppDateIndex;
		private bool preview;
		private GODSMain mainApp;
		private System.Windows.Forms.Button btnOpen;
		private System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.CheckBox previewCheckBox;
		private System.Windows.Forms.Button imgsButton;
		private System.Windows.Forms.ComboBox cbBoxMRN;
		private System.Windows.Forms.ComboBox cbBoxDate;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public BrowseForm(GODSMain mainApp)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			preview = true;
			curMrnIndex = 0;
			curAppDateIndex = 0;
			this.mainApp = mainApp;
		}

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

		/// <summary>
		/// setter for main form
		/// </summary>
		/// <param name="mf">gods main form</param>
//		public void SetMainForm(GODS.MainForm mf)
//		{
//			this.application = mf;
//		}
		#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.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(BrowseForm));
			this.lbMRN = new System.Windows.Forms.Label();
			this.FdApptDT = new System.Windows.Forms.TextBox();
			this.lbApptDT = new System.Windows.Forms.Label();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.cbBoxMRN = new System.Windows.Forms.ComboBox();
			this.cbBoxDate = new System.Windows.Forms.ComboBox();
			this.btnOpen = new System.Windows.Forms.Button();
			this.btnExit = new System.Windows.Forms.Button();
			this.previewCheckBox = new System.Windows.Forms.CheckBox();
			this.imgsButton = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// lbMRN
			// 
			this.lbMRN.Location = new System.Drawing.Point(16, 16);
			this.lbMRN.Name = "lbMRN";
			this.lbMRN.Size = new System.Drawing.Size(40, 13);
			this.lbMRN.TabIndex = 11;
			this.lbMRN.Text = "MRN:";
			this.lbMRN.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// FdApptDT
			// 
			this.FdApptDT.Location = new System.Drawing.Point(80, 48);
			this.FdApptDT.Name = "FdApptDT";
			this.FdApptDT.ReadOnly = true;
			this.FdApptDT.Size = new System.Drawing.Size(147, 20);
			this.FdApptDT.TabIndex = 9;
			this.FdApptDT.Text = "";
			// 
			// lbApptDT
			// 
			this.lbApptDT.Location = new System.Drawing.Point(16, 48);
			this.lbApptDT.Name = "lbApptDT";
			this.lbApptDT.Size = new System.Drawing.Size(91, 14);
			this.lbApptDT.TabIndex = 12;
			this.lbApptDT.Text = "Appt. Date:";
			this.lbApptDT.Click += new System.EventHandler(this.lbApptDT_Click);
			// 
			// pictureBox1
			// 
			this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.pictureBox1.Location = new System.Drawing.Point(16, 88);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(208, 176);
			this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureBox1.TabIndex = 77;
			this.pictureBox1.TabStop = false;
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(56, 16);
			this.textBox1.Name = "textBox1";
			this.textBox1.ReadOnly = true;
			this.textBox1.Size = new System.Drawing.Size(88, 20);
			this.textBox1.TabIndex = 82;
			this.textBox1.Text = "";
			// 
			// cbBoxMRN
			// 
			this.cbBoxMRN.Location = new System.Drawing.Point(56, 16);
			this.cbBoxMRN.Name = "cbBoxMRN";
			this.cbBoxMRN.Size = new System.Drawing.Size(121, 21);
			this.cbBoxMRN.TabIndex = 83;
			this.cbBoxMRN.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.cbBoxMRN_KeyPress);
			this.cbBoxMRN.SelectedIndexChanged += new System.EventHandler(this.cbBoxMRN_SelectedIndexChanged);
			this.cbBoxMRN.Leave += new System.EventHandler(this.cbBoxMRN_Leave);
			// 
			// cbBoxDate
			// 
			this.cbBoxDate.Location = new System.Drawing.Point(80, 48);
			this.cbBoxDate.Name = "cbBoxDate";
			this.cbBoxDate.Size = new System.Drawing.Size(152, 21);
			this.cbBoxDate.TabIndex = 84;
			this.cbBoxDate.SelectedIndexChanged += new System.EventHandler(this.cbBoxDate_SelectedIndexChanged);
			// 
			// btnOpen
			// 
			this.btnOpen.Location = new System.Drawing.Point(264, 232);
			this.btnOpen.Name = "btnOpen";
			this.btnOpen.Size = new System.Drawing.Size(75, 20);
			this.btnOpen.TabIndex = 73;
			this.btnOpen.Text = "Open";
			this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
			// 
			// btnExit
			// 
			this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnExit.Location = new System.Drawing.Point(264, 192);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(75, 20);
			this.btnExit.TabIndex = 74;
			this.btnExit.Text = "Cancel";
			// 
			// previewCheckBox
			// 
			this.previewCheckBox.Location = new System.Drawing.Point(264, 144);
			this.previewCheckBox.Name = "previewCheckBox";
			this.previewCheckBox.Size = new System.Drawing.Size(80, 24);
			this.previewCheckBox.TabIndex = 76;
			this.previewCheckBox.Text = "Preview";
			this.previewCheckBox.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
			// 
			// imgsButton
			// 
			this.imgsButton.Location = new System.Drawing.Point(264, 16);
			this.imgsButton.Name = "imgsButton";
			this.imgsButton.Size = new System.Drawing.Size(75, 20);
			this.imgsButton.TabIndex = 85;
			this.imgsButton.Text = "View All";
			this.imgsButton.Click += new System.EventHandler(this.imgsButton_Click);
			// 
			// BrowseForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(352, 278);
			this.Controls.Add(this.imgsButton);
			this.Controls.Add(this.cbBoxDate);
			this.Controls.Add(this.cbBoxMRN);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.FdApptDT);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.previewCheckBox);
			this.Controls.Add(this.btnExit);
			this.Controls.Add(this.btnOpen);
			this.Controls.Add(this.lbMRN);
			this.Controls.Add(this.lbApptDT);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "BrowseForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "BrowseForm";
			this.Load += new System.EventHandler(this.BrowseForm_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void lbApptDT_Click(object sender, System.EventArgs e)
		{
		
		}

		private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
		{
			putPreview(previewCheckBox.Checked);		
		}

		private void putPreview(bool check)
		{
			preview = check;
			if(check & imageData != null && imageData.Length > 0)
			{
				pictureBox1.Image = ImageUtil.getBitmapFromFile(imageData[curAppDateIndex].AnnotImgPath);
			}
			else
				pictureBox1.Image = null;
		}

		private ArrayList getDatesFromImgData(ImageData[] imgData)
		{
            ArrayList dates = new ArrayList();
			for(int i =0; i<imgData.Length;i++)
			{
				dates.Add(imgData[i].AppDate);
			}
			dates.Sort();
			return dates;
		}

		private void BrowseForm_Load(object sender, System.EventArgs e)
		{
			mrns = GODS.GODSMain.dbManager.getAllMrn();
			mrns.Sort();
			mrns.Reverse();
            this.cbBoxMRN.DataSource = mrns;
			if(mrns.Count>0)
			{
				this.enableBrowsing();
				curMrnIndex = 0;				
				loadMrn();				
			}
			else
			{
				this.cbBoxMRN.Enabled = false;
				this.cbBoxDate.Enabled = false;
				this.btnOpen.Enabled = false;
				this.imgsButton.Enabled = false;
			}
		}

		private void enableBrowsing()
		{
			this.cbBoxMRN.Enabled = true;
			this.cbBoxDate.Enabled = true;
			this.btnOpen.Enabled = true;
			this.imgsButton.Enabled = true;
		}

		private void loadMrn()
		{
			this.cbBoxMRN.SelectedIndex = curMrnIndex;
			imageData = GODS.GODSMain.dbManager.getImgData((string)mrns[curMrnIndex]);
			//image data should already be sorted
			dates = getDatesFromImgData(imageData);
			dates.Reverse();
			this.cbBoxDate.DataSource = dates;
			if(imageData!=null&&imageData.Length>0)
			{
				curAppDateIndex = 0;
				loadAppDate();
			}
			else
				Console.WriteLine("Error--an mrn should has image data in the image table");
		}

		private void loadAppDate()
		{			
			//get the latest mrn
			this.cbBoxDate.SelectedIndex = curAppDateIndex;
			if (preview)
			{
				previewCheckBox.Checked = true;
				putPreview(previewCheckBox.Checked);
			}
			/*if(curAppDateIndex==imageData.Length-1&&curMrnIndex==mrns.Count-1)
				btnNext.Enabled=false;
			else
				btnNext.Enabled=true;
			if(curAppDateIndex==0&&curMrnIndex==0)
				btnPrevious.Enabled=false;
			else
				btnPrevious.Enabled=true;*/
		}

		private void btnPrevious_Click(object sender, System.EventArgs e)
		{
			curMrnIndex--;
			if (curMrnIndex<0)
			{
				curMrnIndex=0;
				//btnPrevious.Enabled=false;
			}
			else
			{
				loadMrn();
			}
		}

		private void btnNext_Click(object sender, System.EventArgs e)
		{
			curMrnIndex++;
			if(mrns.Count==0||curMrnIndex>mrns.Count-1)
			{
				//btnNext.Enabled=false;
				curMrnIndex--;
				return;
			}
			loadMrn();
		}

		private void btnOpen_Click(object sender, System.EventArgs e)
		{
			string mrn = (string) this.cbBoxMRN.SelectedItem;
			DateTime appDate = (DateTime) this.cbBoxDate.SelectedItem;
			ImageData iData = GODS.GODSMain.dbManager.getImgDataByDate(mrn, appDate);
			if (iData != null)
			{
				if(!this.mainApp.activateAnnoForm(iData.ImageId ))
				{
					this.mainApp.LoadFromDB(iData);
				}
			}
			Close();
		}

		private void cbBoxMRN_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			curMrnIndex = this.cbBoxMRN.SelectedIndex;
			imageData = GODS.GODSMain.dbManager.getImgData((string)mrns[curMrnIndex]);
			//image data should already be sorted
			dates = getDatesFromImgData(imageData);
			this.cbBoxDate.DataSource = dates;
			if(imageData!=null&&imageData.Length>0)
			{
				curAppDateIndex = imageData.Length-1;
				loadAppDate();
			}
			else
				Console.WriteLine("Error--an mrn should has image data in the image table");
		}

		private void cbBoxDate_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			curAppDateIndex = this.cbBoxDate.SelectedIndex;
			loadAppDate();
		}

		private void imgsButton_Click(object sender, System.EventArgs e)
		{
			ThumbnailsForm imgsForm = new ThumbnailsForm(imageData,(string)mrns[curMrnIndex],this);
			imgsForm.ShowDialog(null);	
	        if (imgsForm.SelectedIndex!=-1)
			cbBoxDate.SelectedIndex = imgsForm.SelectedIndex;
		}

		private void cbBoxMRN_Leave(object sender, System.EventArgs e)
		{
			if (mrns.Contains(cbBoxMRN.Text))
			{   int index = mrns.IndexOf(cbBoxMRN.Text);			    
                cbBoxMRN.SelectedIndex = index;
			}
			else
				cbBoxMRN.SelectedIndex = curMrnIndex;
			;
		}

		private void cbBoxMRN_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if(e.KeyChar == (char)13)
			{
				if (mrns.Contains(cbBoxMRN.Text))
				{
					int index = mrns.IndexOf(cbBoxMRN.Text);			    
					cbBoxMRN.SelectedIndex = index;
				}
				else
					cbBoxMRN.SelectedIndex = curMrnIndex;
			}		
		}
	}
}
