/// <summary>
        /// Check if the droped files are products and load them if they are
        /// </summary>
        void CommandForm_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                foreach (string s in files)
                {
                    try
                    {
                        filesystem.FileFormat_t file = new filesystem.FileFormat_t();
                        file = filesystem.getDataFromFile(s);

                        if (cbxOutletSide.SelectedIndex != 0 && cbxOutletSide.SelectedIndex != 1)
                        {
                            cbxOutletSide.SelectedIndex = 0;
                        }
                        file.packet.message.parameter = (ushort)cbxOutletSide.SelectedIndex;
                        if (!file.id.Contains("product_"))
                        {
                            file.id = "product_" + file.id;
                        }
                        filesystem.saveToFile(file.packet, file.name, file.id);
                        nmbrOfCustomButtons++;
                        ParamButton dpb = new ParamButton(new ProductParameter_t(file.packet), OnButtonClick, file.name, file.id);
                        dpb.parameter = file.packet.message.parameter;
                        flowProductButtons.Controls.Add(dpb);
                    }
                    catch { }
                }
            }
        }
        /// <summary>
        /// Load parameter of a product when its button is clicked
        /// </summary>
        private void OnButtonClick(object sender, EventArgs e)
        {
            ParamButton button = sender as ParamButton;

            txtSaveName.Text = button.ProductParams.prodName;

            foreach (ParamButton btn in flowProductButtons.Controls)
            {
                btn.UseVisualStyleBackColor = true;
            }
            button.UseVisualStyleBackColor = false;

            try
            {
                loadParamOnIndexChange = false;
                this.ProductParams     = button.ProductParams;
                paramsToControls();
                loadParamOnIndexChange      = true;
                cbxOutletSide.SelectedIndex = button.parameter;
            }
            catch
            {
                filesystem.deleteFile(button.id);
                flowProductButtons.Controls.Remove(button);
            }
        }
        public CommandForm(ref ProductParameter_t[] defaultProducts, SerialComm.Packet_t p)
        {
            InitializeComponent();
            packet = p;
            defaultProductParams = defaultProducts;

            // load saved products
            files = filesystem.loadFromFiles();
            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].id != null)
                {
                    files[i] = CompleteDataOfFile(files[i]);
                    nmbrOfCustomButtons++;
                    ParamButton dpb = new ParamButton(new ProductParameter_t(files[i].packet), OnButtonClick, files[i].name, files[i].id);
                    dpb.parameter = files[i].packet.message.parameter;
                    flowProductButtons.Controls.Add(dpb);
                }
            }

            // if there are no saved products load the default ones
            if (flowProductButtons.Controls.Count == 0)
            {
                ParamButton Espresso    = new ParamButton(defaultProductParams[(byte)ProductType_t.Espresso_e], OnButtonClick, "Espresso", "defEsp");
                ParamButton Coffee      = new ParamButton(defaultProductParams[(byte)ProductType_t.Coffee_e], OnButtonClick, "Coffee", "defCof");
                ParamButton Cappuccino  = new ParamButton(defaultProductParams[(byte)ProductType_t.Cappuccino_e], OnButtonClick, "Cappuccino", "defCap");
                ParamButton Americano   = new ParamButton(defaultProductParams[(byte)ProductType_t.Americano_e], OnButtonClick, "Americano", "defAmericano");
                ParamButton AutoSteam   = new ParamButton(defaultProductParams[(byte)ProductType_t.AutoSteam_e], OnButtonClick, "Auto steam", "defAutoSteam");
                ParamButton HotWater    = new ParamButton(defaultProductParams[(byte)ProductType_t.HotWater_e], OnButtonClick, "Hot water", "defHotWater");
                ParamButton Everfoam    = new ParamButton(defaultProductParams[(byte)ProductType_t.Everfoam_e], OnButtonClick, "Everfoam", "defEFoam");
                ParamButton Milk        = new ParamButton(defaultProductParams[(byte)ProductType_t.Milk_e], OnButtonClick, "Milk", "defMilk");
                ParamButton MilkFoam    = new ParamButton(defaultProductParams[(byte)ProductType_t.MilkFoam_e], OnButtonClick, "Milk foam", "defMilkFoam");
                ParamButton ManualSteam = new ParamButton(defaultProductParams[(byte)ProductType_t.ManualSteam_e], OnButtonClick, "Manual steam", "defManSteam");

                flowProductButtons.Controls.Add(Espresso);
                flowProductButtons.Controls.Add(Coffee);
                flowProductButtons.Controls.Add(Cappuccino);
                flowProductButtons.Controls.Add(Americano);
                flowProductButtons.Controls.Add(AutoSteam);
                flowProductButtons.Controls.Add(Everfoam);
                flowProductButtons.Controls.Add(Milk);
                flowProductButtons.Controls.Add(MilkFoam);
                flowProductButtons.Controls.Add(ManualSteam);
            }

            // select Espresso as default
            cbxOutletSide.SelectedIndex = 0;
            this.ProductParams          = defaultProductParams[(byte)ProductType_t.Espresso_e];
            paramsToControls();
        }
        /// <summary>
        /// Create a new product button and saves it
        /// </summary>
        private void btnCreate_Click(object sender, EventArgs e)
        {
            ParamButton button = new ParamButton(new ProductParameter_t(), OnButtonClick, "", "");

            filesystem.FileFormat_t file = new filesystem.FileFormat_t();

            foreach (ParamButton pb in flowProductButtons.Controls)
            {
                // when the save name is the same as the name of the selected button overwrite it
                // usevisualstylebackcolor is used as an indicator of the clicked state
                if (pb.ProductParams.prodName == txtSaveName.Text && pb.UseVisualStyleBackColor == false)
                {
                    button = pb;
                    flowProductButtons.Controls.Remove(pb);
                }
            }

            controlsToParams();

            if (button.id == "")
            {
                if (txtSaveName.Text != "")
                {
                    file.name = txtSaveName.Text;
                }
                else
                {
                    file.name = "Custom " + nmbrOfCustomButtons;
                }

                file.id = "custom" + nmbrOfCustomButtons;
                controlsToParams();

                file.packet            = new SerialComm.Packet_t();
                file.packet.data       = ProductParams.ToArrayApi();
                file.packet.dataLength = (ushort)file.packet.data.Length;

                nmbrOfCustomButtons++;
            }
            else
            {
                file.id                = button.id;
                file.name              = txtSaveName.Text;
                file.packet.data       = ProductParams.ToArrayApi();
                file.packet.dataLength = (ushort)file.packet.data.Length;
            }

            if (cbxOutletSide.SelectedIndex != 0 && cbxOutletSide.SelectedIndex != 1)
            {
                cbxOutletSide.SelectedIndex = 0;
            }
            file.packet.message.parameter = (ushort)cbxOutletSide.SelectedIndex;
            if (!file.id.Contains("product_"))
            {
                file.id = "product_" + file.id;
            }
            filesystem.saveToFile(file.packet, file.name, file.id);
            ParamButton dpb = new ParamButton(new ProductParameter_t(file.packet), OnButtonClick, file.name, file.id);

            flowProductButtons.Controls.Add(dpb);
        }