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

namespace HearingAidLab
{
	/// <summary>
	/// Summary description for CatLoudUser.
	/// </summary>
    /// 


    public enum ButtonLayout
    {
        Default,
        ISO
    }

	public class CatLoudUser : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button btnListen;
		private System.Windows.Forms.Button btnRespond;
		private ArrayList listStrings = new ArrayList();
		public delegate void ButtonEventHandler(object sender, EventArgs e);
		public event ButtonEventHandler ButtonEvt;

		public CatLoudUser(int iButtonLayout)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			this.btnListen.Hide();
			if(iButtonLayout == 0)//Default layout 
			{
				listStrings.Add("TOO Loud");
				listStrings.Add("Very Loud");
				listStrings.Add("Loud");
				listStrings.Add("Medium");
				listStrings.Add("Soft");
				listStrings.Add("Very Soft");
				listStrings.Add("Can't Hear");
			}
			else if (iButtonLayout == 1)//ISO layout
			{
				listStrings.Add("Extremely Loud");
				listStrings.Add("Very Loud");
				listStrings.Add("-------");
				listStrings.Add("Loud");
				listStrings.Add("-------");
				listStrings.Add("Medium");
				listStrings.Add("-------");
				listStrings.Add("Soft");
				listStrings.Add("-------");
				listStrings.Add("Very Soft");
				listStrings.Add("Not Heard");
			}

		}

		/// <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.btnListen = new System.Windows.Forms.Button();
			this.btnRespond = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// btnListen
			// 
			this.btnListen.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.btnListen.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnListen.Location = new System.Drawing.Point(360, 152);
			this.btnListen.Name = "btnListen";
			this.btnListen.Size = new System.Drawing.Size(112, 32);
			this.btnListen.TabIndex = 0;
			this.btnListen.Text = "Listen";
			this.btnListen.Visible = false;
			// 
			// btnRespond
			// 
			this.btnRespond.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.btnRespond.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnRespond.Location = new System.Drawing.Point(368, 184);
			this.btnRespond.Name = "btnRespond";
			this.btnRespond.Size = new System.Drawing.Size(120, 72);
			this.btnRespond.TabIndex = 1;
			this.btnRespond.Text = "Respond";
			this.btnRespond.Click += new System.EventHandler(this.btnRespond_Click);
			// 
			// CatLoudUser
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(492, 266);
			this.Controls.Add(this.btnRespond);
			this.Controls.Add(this.btnListen);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "CatLoudUser";
			this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
			this.Text = "CatLoudUser";
			this.ResumeLayout(false);

		}
		#endregion

		public void CreateButtons(int n , int Layout)
		{
			if(n<7)
				n=7;
			int x = 0;
			//int y = 8+8+32;
			int y = 8+8;
			int yspace = 700/n;
			if(yspace>60)
				yspace = 60;
			int ysize = yspace-5;
			int width = 300-10;
			this.Size = new Size(500,8+yspace*(n+1));
			//btnRespond.Location = new Point(x,btnRespond.Location.Y);
			//this.btnRespond.Width = width;
			this.Invalidate();
			this.Update();
			ArrayList lColors = GetColors(n);

			int label = (n-2)/4;
			Font f = new Font("Arial",12.25f,FontStyle.Bold);
			
			double fl = 50.0/(n-1);
			for(int i=0;i<n;i++)
			{
				Button b = new Button();
				b.Font = f;



                if (Layout == Convert.ToInt32(ButtonLayout.Default))
                {
                    if (i == 0)
                        b.Text = (string)listStrings[0];
                    else if (i == n - 1)
                        b.Text = (string)listStrings[6];
                    else
                    {
                        if (i % label == 0)
                        {
                            b.Text = (string)listStrings[i / label];
                        }
                    }
                }
                else
                {
                    b.Text = (string)listStrings[i];
                }
				b.Tag = i;
				b.Click += new EventHandler(Button_Click);
				b.Location = new Point(x,y);
				b.Size = new Size(width,ysize);
				width-=10;
				b.BackColor = (Color) lColors[i];
				b.ForeColor = Color.White;
				this.Controls.Add(b);
				y+=yspace;
				x+=5;
				Label lbl = new Label();
				lbl.Location = new Point(5,5);
				lbl.Size = new Size(50,lbl.Height);
				lbl.Click += new EventHandler(Button_Click);
				lbl.ForeColor = Color.White;
				lbl.Font = new Font("Arial",10.25f,FontStyle.Bold);
				//b.Controls.Add(lbl);
				int z = (int) ((n-i-1)*fl);
				lbl.Text += z.ToString();
				
			}
		}
		private ArrayList GetColors(int n)
		{
			//start at RGB of 255,0,0
			//end at RGB of 0,0,255
			int space = 255/n;
			int red = 255;
			int green = 0;
			int blue = 0;
			ArrayList l = new ArrayList();
			for(int i=0;i<n;i++)
			{
				Color c = Color.FromArgb(red,green,blue);
				l.Add(c);
				red-=space;
				blue+=space;
			}
			return l;
		}

		private void Button_Click(object sender, EventArgs e)
		{
			if(sender is Button)
			{
				Button b = (Button) sender;
               
				ButtonEvt(sender, e);
			}
			else if(sender is Label)
			{
				Button b = (Button) ((Label)sender).Parent;
				ButtonEvt(b, e);
			}
		}
		public void LightListen()
		{
			this.btnRespond.BackColor = this.BackColor;
			//btnListen.BackColor = Color.Red;
		}
		public void LightRespond()
		{
			//btnListen.BackColor = this.BackColor;
			btnRespond.BackColor = Color.Green;
		}
		public void LightNone()
		{
			//btnListen.BackColor = this.BackColor;
			btnRespond.BackColor = this.BackColor;
		}

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