using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Xml.Serialization;



using edu.jhu.Cas.Util;

using edu.jhu.Cas.Jobs;



namespace edu.jhu.Cas.Web

{

	/// <summary>

	/// Summary description for Images.

	/// </summary>

	public class Images : System.Web.UI.Page

	{

		protected string plotURL,lmessage,tableName;

		protected bool loggedin;

		protected long wsid;

		protected ImgCutoutParams args;

		protected TextBox WidthB, HeightB,ScaleB;

		protected CheckBox GB,LB,PB,SB,TB,OB,FB,BB,MB,PlB,IB;

		protected Hashtable opts;



		private void Page_Load(object sender, System.EventArgs e)

		{

			// Put user code to initialize the page here

		}



		

		override protected void OnInit(EventArgs e)

		{

			InitializeComponent();

			base.OnInit(e);

			WebUtils.CheckLoggedIn(Session,Response,Request,true);
			lmessage = Request.Params["lmessage"];
			loggedin = Session["USER"] != null;  
			if (loggedin) 
				wsid = (long)Session["USER"];
			tableName = Request.Params["tableName"];

			args = new ImgCutoutParams();

			WidthB.Text = args.Width.ToString();

			HeightB.Text = args.Height.ToString();

			ScaleB.Text = args.Scale.ToString();

			opts = new Hashtable();

			opts.Add(GB,"G");

			opts.Add(LB,"L");

			opts.Add(PB,"P");

			opts.Add(SB,"S");

			opts.Add(TB,"T");

			opts.Add(OB,"O");

			opts.Add(BB,"B");

			opts.Add(FB,"F");

			opts.Add(MB,"M");

			opts.Add(PlB,"Q");

			opts.Add(IB,"I");

		}

				

		private void InitializeComponent()

		{    

			this.Load += new System.EventHandler(this.Page_Load);

		}

		

		protected void Fetch(object sender, System.EventArgs e){

			/**

			args.Width = int.Parse(WidthB.Text);

			args.Height = int.Parse(HeightB.Text);

			*/

			args.Height = int.Parse(WidthB.Text);

			args.Width = int.Parse(HeightB.Text);

			args.Scale = double.Parse(ScaleB.Text);

			foreach(object o in opts.Keys){

				CheckBox cb = (CheckBox)o;

				if(cb.Checked)

					args.Opt+=opts[o].ToString();

			}  

			XmlSerializer serializer = new XmlSerializer(typeof(ImgCutoutParams));

			TextWriter tw = new StringWriter();

			serializer.Serialize(tw,args);

			tw.Close(); 
			long jobid = (new TableJobs()).QueryFileOutput(wsid,"select * from "+tableName,OUTPUT.IMG_COLLECTION,
				"MYDB",tw.ToString(),tableName+"_Images");
			if (jobid <=0) throw new Exception ("No job was created - has this been implemented ?");
			// lets wait so it might be finished 
			System.Threading.Thread.Sleep(3000);
			Response.Redirect("jobdetails.aspx?id="+jobid+"&message=Details%20of%20Extraction%20"+jobid);

		}

	}

}

