using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace Chipset_FSB_estimator
{
	public class frmChipset_FSB_estimator : System.Windows.Forms.Form
	{
		#region Messages
		private string message1;
		private string message2;
		private string message3;
		private string message4;
		private string message5;
		private string message6;
		private string message7;
		private string message8;
		private string message9;
		private string message10;
		private string message11;
		private string message12;
		private string message13;
		private string message14;
		#endregion
        #region External variables
        private bool translation=false;
        double[] Vdd=new double[6];
        double[] FSB=new double[6];
        int number;
        #endregion		
		#region Form's routines
		private void frmChipset_FSB_estimator_Load(object sender, System.EventArgs e)
		{
            Translation();
           	double Vdd=1.2;
			while (Vdd <= 2.0)
			{
				comboBox1.Items.Add(Vdd.ToString(System.Globalization.CultureInfo.InvariantCulture));
				comboBox2.Items.Add(Vdd.ToString(System.Globalization.CultureInfo.InvariantCulture));
				comboBox3.Items.Add(Vdd.ToString(System.Globalization.CultureInfo.InvariantCulture));
				comboBox4.Items.Add(Vdd.ToString(System.Globalization.CultureInfo.InvariantCulture));
				comboBox5.Items.Add(Vdd.ToString(System.Globalization.CultureInfo.InvariantCulture));
				Vdd+=0.001;
				Vdd=Math.Round(Vdd,3);
			}     
		}
		private void statusBar1_TextChanged(object sender, System.EventArgs e)
		{
			for (int i=1; i<=5; i++)
			{
				statusBar1.Visible=false;
				System.Threading.Thread.Sleep(1);
				statusBar1.Visible=true;
			}
		}
		#endregion
		
		#region Translation routine
		private void Translation()
		{
			FileInfo theSourceFile=new FileInfo(@"Language.txt");  // Το αρχείο εισόδου
			TextReader reader=theSourceFile.OpenText();
			string text="";
			bool crash=false;
			while (text != "[Chipset FSB estimator]")
			{
				text=reader.ReadLine();
				if (text == null)  // Βρήκε EOF άρα έχει γίνει @@ρια με το αρχείο μετάφρασης
				{
					this.TopMost=false;
					MessageBox.Show("A critical error found in language file\n"+
						"and program must be terminated.\n"+
						"You must check and replace the header.",
						"ERROR in language file!",MessageBoxButtons.OK,MessageBoxIcon.Error); 
					crash=true;
					break;
				}
			}
			if (crash)
                Application.Exit();
            translation=true;
			text=reader.ReadLine();
			statusBar1.Text=text;
			text=reader.ReadLine();
			group_Data.Text=text;
			text=reader.ReadLine();
			group_Button_panel.Text=text;
			text=reader.ReadLine();
			lbl_Measurements.Text=text;
			text=reader.ReadLine();
			group_Estimation_card.Text=text;
			text=reader.ReadLine();
			lbl_Desired_Vdd.Text=text;
			text=reader.ReadLine();
			group_Results.Text=text;
			text=reader.ReadLine();
			lbl_Estimated_area.Text=text;
			message1=reader.ReadLine();    
			message2=reader.ReadLine();
			message3=reader.ReadLine();
			message4=reader.ReadLine();
			message5=reader.ReadLine();
			message6=reader.ReadLine();
			message7=reader.ReadLine();
			message8=reader.ReadLine();
			message9=reader.ReadLine();
			message10=reader.ReadLine();
			message11=reader.ReadLine();
			message12=reader.ReadLine();
			message13=reader.ReadLine();
			message14=reader.ReadLine();
			reader.Close();
		}	
		#endregion					         
		
		#region Βutton Compute
		private void btn_Compute_Click(object sender, System.EventArgs e)
		{
			double maxvoltage=0, value=0.0;
            bool flag=true;  
            for (int i=0; i<=5; i++)
                Vdd[i]=FSB[i]=0.0;
			try
			{
				number=int.Parse(combo_Measurements.Text,System.Globalization.NumberFormatInfo.InvariantInfo);
			}
			catch
			{
				if (!translation)
				{
					MessageBox.Show("You must choose measurements number!\n"+"Retry and be careful!",
						"Empty combobox!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
					statusBar1.Text="You are not careful!";
				}
				else
				{
					MessageBox.Show(message1 + "\n" + message2,message3,MessageBoxButtons.OK,MessageBoxIcon.Error);
					statusBar1.Text=message4;
				}
				combo_Measurements.Text="??";
				combo_Measurements.Focus();
				flag=false;
			}
			try
			{
				Vdd[1]=double.Parse(comboBox1.Text,System.Globalization.NumberFormatInfo.InvariantInfo);
				FSB[1]=double.Parse(textBox1.Text,System.Globalization.NumberFormatInfo.InvariantInfo);
				Vdd[2]=double.Parse(comboBox2.Text,System.Globalization.NumberFormatInfo.InvariantInfo);
				FSB[2]=double.Parse(textBox2.Text,System.Globalization.NumberFormatInfo.InvariantInfo);
				if (number >= 3)
				{
					Vdd[3]=double.Parse(comboBox3.Text,System.Globalization.NumberFormatInfo.InvariantInfo);
					FSB[3]=double.Parse(textBox3.Text,System.Globalization.NumberFormatInfo.InvariantInfo);
				}
				if (number >= 4)
				{
					Vdd[4]=double.Parse(comboBox4.Text,System.Globalization.NumberFormatInfo.InvariantInfo);
					FSB[4]=double.Parse(textBox4.Text,System.Globalization.NumberFormatInfo.InvariantInfo);
				}
				if (number >= 5)   
				{
					Vdd[5]=double.Parse(comboBox5.Text,System.Globalization.NumberFormatInfo.InvariantInfo);
					FSB[5]=double.Parse(textBox5.Text,System.Globalization.NumberFormatInfo.InvariantInfo);
				}			    	    										  	   					
			}   // <--- END ΟF TRY
			catch
			{
				if (!translation)
				{
					MessageBox.Show("Enter proper values in all boxes!\n"+"Retry and be careful!",
						"Empty boxes or bad arguments!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
					statusBar1.Text="You are not careful!";
				}
				else
				{
					MessageBox.Show(message5 + "\n" + message3,message6,MessageBoxButtons.OK,MessageBoxIcon.Error);
					statusBar1.Text=message4;
				}
				flag=false;
			}	
			if (flag)
			{
				maxvoltage=Vdd[1];
				for (int i=1; i<=number; i++)
					if (Vdd[number] > maxvoltage)
						maxvoltage=Vdd[number];
				value=1.000;
				if (1.07*maxvoltage > 2.0)
				{
					while (value <= 2.0)
					{
						combo_Desired_Vdd.Items.Add(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
						value+=0.001;
						value=Math.Round(value,3);
					}   
				}
				else
				{
					while (value <= 1.07*maxvoltage)
					{
						combo_Desired_Vdd.Items.Add(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
						value+=0.001;
						value=Math.Round(value,3);
					}
				}
				if (!translation)
					statusBar1.Text="Choose the desired Vdd now:";
				else
					statusBar1.Text=message7;
				group_Estimation_card.Visible=true;
			}
		}
		private void btn_Compute_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			btn_Compute.Top--;
			btn_Compute.Left--;
		}
		private void btn_Compute_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			btn_Compute.Top++;
			btn_Compute.Left++;
		}
		#endregion
		#region Buttons help and clear
		private void btn_Help_Click(object sender, System.EventArgs e)
		{
			System.Threading.Thread.Sleep(100);
			try
			{
				ProcessStartInfo psi=new ProcessStartInfo("Manuals\\Chipset FSB estimator\\Chipset FSB estimator.htm");
				psi.UseShellExecute=true;
				Process.Start(psi);
			}
			catch
			{
				if (!translation)
					MessageBox.Show("Help file is missing!","No help!",MessageBoxButtons.OK,MessageBoxIcon.Error);
				else
					MessageBox.Show(message8,message9,MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}
		private void btn_Help_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			btn_Help.Top++;
			btn_Help.Left++;
		}
		private void btn_Help_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			btn_Help.Top--;
			btn_Help.Left--;
		}
		private void btn_Clear_Click(object sender, System.EventArgs e)
		{
			System.Threading.Thread.Sleep(100);
			for (int i=1; i<=50; i++)
			{
				this.Top+=10;
				this.Left+=10;
				this.Top-=10;
				this.Left-=10;
			}   
			textBox1.Text=textBox2.Text=textBox3.Text=textBox4.Text=textBox5.Text=""; 
			group_Estimation_card.Visible=group_Results.Visible=false;
            combo_Desired_Vdd.Text=combo_Measurements.Text=comboBox1.Text=comboBox2.Text=comboBox3.Text=comboBox4.Text=comboBox5.Text="";
            combo_Desired_Vdd.Items.Clear();  
            if (!translation)                     
				statusBar1.Text="All boxes cleared! Repeat or close the form!";
			else
				statusBar1.Text=message10;
		}
		private void btn_Clear_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			btn_Clear.Top++;
			btn_Clear.Left++;
		}
		private void btn_Clear_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			btn_Clear.Top--;
			btn_Clear.Left--;
		}
		#endregion
		
		#region Βutton Estimate
		private void btn_Estimate_Click(object sender, System.EventArgs e)
		{
			double edge1,edge2,voltage,value;
			double a1,b1,a2,b2,a3,b3,cor1,cor2,cor3;
			string text;
            try
            {
                voltage=double.Parse(combo_Desired_Vdd.Text,System.Globalization.NumberFormatInfo.InvariantInfo);
            }
            catch
            {
                if (!translation)
                {
                    MessageBox.Show("Critical error!\n"+"Retry and be careful!","Cannot continue!",
                        MessageBoxButtons.OK,MessageBoxIcon.Error);
                    statusBar1.Text="What's up?";
                    combo_Desired_Vdd.Focus();
                }
                else
                {
                    MessageBox.Show(message11+"\n"+message2,message12,MessageBoxButtons.OK,MessageBoxIcon.Error);
                    statusBar1.Text=message13;
                }
                goto Telos;
            }
            cor1=0.99; cor2=0.98; cor3=1.0;       // The part of complex calculations removed anyway assume that 
            a1=1; a2=2; a3=3; b1=4; b2=5; b3=6;   // after these calculations a1,a2.. have these values
            btn_Clear.Visible=group_Results.Visible=true;
            if (!translation)
                statusBar1.Text="All calculations performed! Repeat/clear or close the form!";
            else
                statusBar1.Text=message14;
            if (100.0*cor1 < 100.0)
            {
                cor1=Math.Round(100*cor1,6);
                text=cor1.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
                text+="%";
                textBox7.Text=text;
            }
            else
                textBox7.Text="100.0%";
            if (100.0*cor2 < 100.0)
            {
                cor2=Math.Round(100*cor2,6);
                text=cor2.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
                text+="%";
                textBox9.Text=text;
            }
            else
                textBox9.Text="100.0%";
            if (100.0*cor3 < 100.0)
            {
                cor3=Math.Round(100*cor3,6);
                text=cor3.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
                text+="%";
                textBox11.Text=text;
            }
            else
                textBox11.Text="100.0%";
            value=a1+b1*voltage;
            text="";
            edge1=Math.Round(value-2.0,0);
            text+=edge1.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
            text+=" - ";
            edge2=Math.Round(value,0);
            text+=edge2.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
            text+=" MHz.";
            textBox6.Text=text;
            value=a2+b2*Math.Log(voltage);
            text="";
            edge1=Math.Round(value-2.0,0);
            text+=edge1.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
            text+=" - ";
            edge2=Math.Round(value,0);
            text+=edge2.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
            text+=" MHz.";
            textBox8.Text=text;
            value=a3*Math.Pow(b3,voltage);
            text="";
            edge1=Math.Round(value-2.0,0);
            text+=edge1.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
            text+=" - ";
            edge2=Math.Round(value,0);
            text+=edge2.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
            text+=" MHz.";
            textBox10.Text=text;
            Telos:
            int dummy=69;
            dummy++;
		}
		private void btn_Estimate_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			btn_Estimate.Top++;
			btn_Estimate.Left++;
		}
		private void btn_Estimate_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			btn_Estimate.Top--;
			btn_Estimate.Left--;
		}
		#endregion
	}
}