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 USB_Comunication;


namespace USB_interface
{
    public partial class MainWindow : Form
    {
        public Loging log = new Loging();

        public MainWindow()
        {
            log.StartLoging();

            log.WriteLog("Program start.");
            
            // vytvoreni panelu pro podokna zarizeni
            subPanel = new Panel();
            subPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            subPanel.Name = "Panel0";
            subPanel.TabIndex = 0;
				
            InitializeComponent();

            this.IsMdiContainer = true;

            // bocni menu panel viditelny
            this.panelJednaToolStripMenuItem.Checked = true;

            this.tableLayoutPanel1.Controls.Clear();
            this.tableLayoutPanel1.Controls.Add(subPanel);

            MyGenForm oneForm = new MyGenForm(/*"No device", "Generator", 0, this*/);
            oneForm.TopLevel = false;
            oneForm.MdiParent = this;
            subPanel.Controls.Add(oneForm);

            this.ShapeChanged += oneForm.radioButton_CheckedChanged;
            this.ViewChanged += oneForm.ShowCheckedChanged;
            this.ModeChanged += oneForm.ModeCheckedChanged;
            this.UserCountChanged += oneForm.CreatePoints;
            this.LoadReq += oneForm.LoadUserSig;
            this.SaveReq += oneForm.SaveUserSig;

            oneForm.Show();

            this.listBox1.DoubleClick += listBox1_Select;
            this.listBox1.KeyPress += listBox1_Select;

            this.checkBox1.Checked = true;

            for (int i = 4; i <= 40; i += 2)
            {
                comboBox1.Items.Add(i.ToString());
            }
            comboBox1.SelectedIndex = 0;

            button1.Enabled = false;
            button3.Enabled = false;
            comboBox1.Enabled = false;
            checkBox3.Enabled = false;
            checkBox4.Enabled = false;

            // nastaveni pocatecniho modu zobrazeni
            initializeUSB();

            this.radioButton1.Select();
        }

        private USB_Comm USB;

		private void initializeUSB()
		{
			USB = new USB_Comm();

			USB.USBdeviceChanged += RefreshUSB;

			USB.InitUsbDevices();
		}

		// aktualizace seznamu zarizeni
		private void RefreshUSB(object sender, EventArgs e)
		{
			listBox1.Items.Clear();
			foreach(USB_Device dev in USB.Devices)
			{
				this.listBox1.Items.Add(dev);
			}

			listBox2.Items.Clear();
			foreach(string dev in USB.OtherDevices.Values)
			{
				this.listBox2.Items.Add(dev);
			}

			log.WriteLog("USB devices refreshed, it found " + USB.Devices.Count.ToString() + " devices."); 
		}


        // Menu panel
        private void panelJednaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.panelJednaToolStripMenuItem.Checked)
            {
                this.splitContainer1.Panel1Collapsed = true;
                this.panelJednaToolStripMenuItem.Checked = false;
            }
            else
            {
                this.splitContainer1.Panel1Collapsed = false;
                this.panelJednaToolStripMenuItem.Checked = true;
            }
            OnResize(null);
        }


        // prekresleni pri posunuti splitteru
        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {
            OnResize(null);
        }

		// prekresleni oken zarizeni
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            if (subPanel.Controls.Count > 0)
            {
                (subPanel.Controls[0] as MyGenForm).SetSize(this.tableLayoutPanel1.Size);
            }
        }


        // List subPanelu
        private Panel subPanel = null;


        // Vybrani zarizeni v listboxu
        private void listBox1_Select(object sender, EventArgs e)
        {
            device_selected(this.listBox1.SelectedItem);
        }
        private void listBox1_Select(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == 13)
                device_selected(this.listBox1.SelectedItem);
        }

        // zpracovani vyberu zarizeni
        private void device_selected(object SelectedItem)
        {
			if (SelectedItem == null) return;

			try
			{
				USB_Device device = SelectedItem as USB_Device;
                device.DeviceOpened += (subPanel.Controls[0] as MyGenForm).DeviceOpened;
                USB.USBdeviceClosed += (subPanel.Controls[0] as MyGenForm).DeviceClosed;
				device.OpenDevice();
			
			}
			catch(Exception e)
			{
				MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
        }

        private void Device_DeviceOpened(object sender, string e)
        {
            throw new NotImplementedException();
        }

        // zobraz log soubor
        private void button2_Click(object sender, EventArgs e)
        {
			MessageBox.Show(log.GetMessages(), "Log file content");
        }

        protected override void OnClosed(EventArgs e)
        {
            log.WriteLog("Program end.");
            log.Close();
            base.OnClosed(e);
        }

        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            MyGenForm.SelectedForm sf = MyGenForm.SelectedForm.sine;
            if (radioButton6.Checked)
            {
                sf = MyGenForm.SelectedForm.user;
                button1.Enabled = true;
                button3.Enabled = true;
                comboBox1.Enabled = true;
                checkBox3.Enabled = true;
                checkBox4.Enabled = true;
            }
            else
            {
                if (radioButton1.Checked) sf = MyGenForm.SelectedForm.sine;
                if (radioButton2.Checked) sf = MyGenForm.SelectedForm.triangle;
                if (radioButton3.Checked) sf = MyGenForm.SelectedForm.sawDown;
                if (radioButton4.Checked) sf = MyGenForm.SelectedForm.sawUp;
                if (radioButton5.Checked) sf = MyGenForm.SelectedForm.rectangle;

                button1.Enabled = false;
                button3.Enabled = false;
                comboBox1.Enabled = false;
                checkBox3.Enabled = false;
                checkBox4.Enabled = false;
            }

            if(ShapeChanged != null)
            {
                ShapeChanged(this, sf);
            }
        }

        private void checkBox_CheckedChanged(object sender, EventArgs e)
        {
            if(ViewChanged != null)
            {
                bool[] arr = new bool[2];
                arr[0] = checkBox1.Checked;
                arr[1] = checkBox2.Checked;

                ViewChanged(this, arr);
            }
        }

        private void ModeCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (sender == checkBox3)
            {
                if(checkBox3.Checked)
                {
                    checkBox4.Checked = false;
                }
            }

            if (sender == checkBox4)
            {
                if (checkBox4.Checked)
                {
                    checkBox3.Checked = false;
                }
            }

            if (ModeChanged != null)
            {
                bool[] arr = new bool[2];
                arr[0] = checkBox3.Checked;
                arr[1] = checkBox4.Checked;

                ModeChanged(this, arr);
            }

        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(UserCountChanged != null)
            {
                UserCountChanged(this, int.Parse(comboBox1.SelectedItem.ToString()));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(LoadReq != null)
            {
                LoadReq(this, null);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(SaveReq != null)
            {
                SaveReq(this, null);
            }
        }


        public event EventHandler<MyGenForm.SelectedForm> ShapeChanged;
        public event EventHandler<bool[]> ViewChanged;
        public event EventHandler<bool[]> ModeChanged;
        public event EventHandler<int> UserCountChanged;
        public event EventHandler LoadReq;
        public event EventHandler SaveReq;

    }
}