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

namespace Arduino8x8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SerialPort comPort;

        private void Form1_Load(object sender, EventArgs e)
        {

            comPort = new SerialPort();
            comPort.PortName = "COM4";
            comPort.BaudRate = 115200;

            comPort.DataReceived += Incoming;
            comPort.Open();
            for (int i = 0; i < 8; i++)
                dataGridView1.Columns.Add(i.ToString(), i.ToString());

            dataGridView1.Rows.Insert(0, 8);

            for (int i = 0; i < 8; i++)
            {
                dataGridView1.Columns[i].Width = 64;
                dataGridView1.Rows[i].Height = 64;
            }

            for (int j = 0; j < 8; j++)
                for (int i = 0; i < 8; i++)
                    dataGridView1.Rows[j].Cells[i].Value = "1";

        }

        public static Bitmap MakeGrayscale(Bitmap original)
        {
            //make an empty bitmap the same size as original
            Bitmap newBitmap = new Bitmap(original.Width, original.Height);
            byte[] b = new byte[8];

            for (int i = 0; i < original.Width; i++)
            {
                for (int j = 0; j < original.Height; j++)
                {
                    //get the pixel from the original image
                    Color originalColor = original.GetPixel(i, j);

                    //create the grayscale version of the pixel
                    int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59)
                        + (originalColor.B * .11));

                    //create the color object
                    Color newColor = Color.FromArgb(grayScale, grayScale, grayScale);

                    //set the new image's pixel to the grayscale version
                    newBitmap.SetPixel(i, j, newColor);
                }
            }

            return newBitmap;
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewCell a = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];


            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                a.Style.BackColor = Color.Yellow;
                a.Value = "1";
            }
            else
            {
                a.Style.BackColor = Color.Black;
                a.Value = "0";
            }

            dataGridView1.Refresh();
            dataGridView1.CurrentCell = null;

            UpdateDisplay();
        }

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

        void UpdateDisplay()
        {
            textBox1.Text = "";
            byte[] b = new byte[8];

            for (int j = 0; j < 8; j++)
            {
                b[j] = 0;
                int a = 0;
                textBox1.Text += "Rad: " + j + "\r\n";
                for (byte i = 0; i < 8; i++)
                {
                    if (!dataGridView1.Rows[j].Cells[i].Value.ToString().Contains('0'))
                    {
                        int c = (int)Math.Pow(2, i);
                        textBox1.Text += " " + c + " ";
                        a += c;
                    }
                }
                b[j] = (byte)(a + 48);


                textBox1.Text += "\r\nVal: " + a + "\r\n";
            }

            //comPort.WriteLine(o);

            comPort.Write(b, 0, b.Length);

        }


        void Incoming(object sender,
                                   SerialDataReceivedEventArgs e)
        {
            displayResponse(comPort.ReadExisting());
        }

        protected void displayResponse(string resp)
        {
            //here cross thread exception occur if directly set to lblMsgResp.Text="Test";
            if (textBox1.InvokeRequired)
            {
                textBox1.Invoke(new MethodInvoker(delegate { textBox1.Text += resp; }));
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            comPort.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (int j = 0; j < 8; j++)
                for (int i = 0; i < 8; i++)
                {
                    dataGridView1.Rows[j].Cells[i].Value = "1";
                    dataGridView1.Rows[j].Cells[i].Style.BackColor = Color.Yellow;
                }
            UpdateDisplay();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            for (int j = 0; j < 8; j++)
                for (int i = 0; i < 8; i++)
                {
                    dataGridView1.Rows[j].Cells[i].Value = "0";
                    dataGridView1.Rows[j].Cells[i].Style.BackColor = Color.Black;
                }
            UpdateDisplay();
        }
    }
}
