using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication1
{
    public partial class frmSettings : Form
    {
        private frmSettings()
        {
            InitializeComponent();

            
        }

        public frmSettings(Form f1)
        {
            InitializeComponent();
            this.callerForm = f1;
            panel1.Visible = false;
            panel2.Visible = true;
            panel3.Visible = false;
            
        }

        Form callerForm;

        



        public void showMe()
        {
            Show();
            if (callerForm != null)
            {
                callerForm.Hide();
            }

        }

        bool InvalidData = false;
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                try
                {
                    PublicVarClass.xOffset = Convert.ToInt32(txtXOffset.Text);
                    PublicVarClass.yOffset = Convert.ToInt32(txtYOffset.Text);
                    PublicVarClass.x0 = Convert.ToInt32(txtX0.Text);
                    PublicVarClass.y0 = Convert.ToInt32(txtY0.Text);
                    //PublicVarClass.ports = comboBox1.SelectedItem.ToString();
                    InvalidData = false;
                }
                catch
                {
                    MessageBox.Show("Invalid Data");
                    InvalidData=true;
                    throw new EvaluateException("Invalid Data");

                }


                PublicVarClass.scale = Convert.ToDouble(textBox4.Text);
                PublicVarClass.savesettings();
                MessageBox.Show("Settings Changed successfully.");
            
            }
            catch 
            {
                if (!InvalidData)
                MessageBox.Show("Cannot Save.");
            }
            //PublicVarClass.aOffset = txtXOffset.Text;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmSettings_Load(object sender, EventArgs e)
        {
             
            txtXOffset.Text = PublicVarClass.xOffset.ToString();
            txtYOffset.Text = PublicVarClass.yOffset.ToString();
            txtX0.Text = PublicVarClass.x0.ToString();
            txtY0.Text = PublicVarClass.y0.ToString();
            textBox4.Text = PublicVarClass.scale.ToString();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string val = PublicVarClass.CalculateMD5Hash(PublicVarClass.CalculateMD5Hash(txtPassLogIn.Text));
            if (val == PublicVarClass.pass)
            {
                panel1.Visible = true;
                panel3.Visible = true;
                panel2.Visible = false;
                txtXOffset.Focus();
            }
            else
            {
                MessageBox.Show("The password is incorrect." );
                txtPassLogIn.Clear();
            }
        }

        

        private void button3_Click(object sender, EventArgs e)
        {
            if (txtPass2.Text.Length == 0 && txtPass1.Text.Length == 0)
            {
                MessageBox.Show("Enter new password.");
            }
            else if (txtPass1.Text == txtPass2.Text )
            {
                PublicVarClass.pass = PublicVarClass.CalculateMD5Hash(PublicVarClass.CalculateMD5Hash(txtPass1.Text));
                PublicVarClass.savePassword();
                txtPass1.Text = txtPass2.Text = "";
                MessageBox.Show("Password Saved Successfully.");
            }
            else
            {
                MessageBox.Show("The passwords are not the same.");

            }
        }

        private void txtPassLogIn_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                btnLogIn.PerformClick();
        }

        private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                btnSave.PerformClick();
        }

        private void txtXOffset_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                btnSave.PerformClick();
        }

        private void txtYOffset_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                btnSave.PerformClick();
        }

        private void txtX0_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                btnSave.PerformClick();
        }

        private void txtY0_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                btnSave.PerformClick();
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                btnSave.PerformClick();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                btnSavePass.PerformClick();
        }

        private void txtPass2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                btnSavePass.PerformClick();
        }

        private void frmSettings_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (callerForm != null)
            {
                callerForm.Show();
            }
        }

        

        
    }
}
