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 frmManual : Form
    {
        private frmManual()
        {
            InitializeComponent();
           
        }

        Form callerForm;
        public frmManual(Form f1)
        {
            InitializeComponent();
            this.callerForm = f1;
            foreach (string s in SerialPort.GetPortNames())
            {
                comboBox1.Items.Add(s);
            }
            if (comboBox1.Items.Count > 0)
            {
                if (PublicVarClass.ports.Length > 1)
                {
                    comboBox1.SelectedItem = PublicVarClass.ports;
                }
                else
                {
                    comboBox1.SelectedItem = comboBox1.Items[0];
                }
            }
            comboBox1.SelectedIndexChanged += delegate
            {
                PublicVarClass.ports = comboBox1.SelectedText;
            };
        }

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

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                PublicVarClass.x = Convert.ToInt32(txtX.Text);
                PublicVarClass.y = Convert.ToInt32(txtY.Text);
            }
            catch (Exception exception)
            {
                MessageBox.Show("Please enter a number value");
            }

            try
            {
                PublicVarClass.loadSetting();
                Pair newCoordinates = interpolator.calculateMapping(PublicVarClass.x, PublicVarClass.y);
                PublicVarClass.x = (int) Math.Round(newCoordinates.x) + PublicVarClass.xOffset;
                PublicVarClass.y = (int) Math.Round(newCoordinates.y) + PublicVarClass.yOffset;
                
                
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Log", true))
                {
                    file.WriteLine(PublicVarClass.x + " " + PublicVarClass.y);
                }
                
                //Console.Write(PublicVarClass.x + " " + PublicVarClass.y);
                PublicVarClass.a = Convert.ToInt32(txtA.Text);
                PublicVarClass.send();
            }
            catch (Exception exception)
            {
                Console.Write(exception.Message);
                MessageBox.Show("Interpolation Exception");
            }
            


            
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtA.Clear();
            txtX.Clear();
            txtY.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            callerForm.Show();

            Close();
        }

        private void frmManual_Load(object sender, EventArgs e)
        {
            txtA.Text = PublicVarClass.a.ToString();
            txtX.Text = PublicVarClass.x.ToString();
            txtY.Text = PublicVarClass.y.ToString();
        }

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