using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GlobalPaymentsTestApp {
    public static class Invoker {

        #region ListViewItem
        public static void SetListViewItem(ListView listView, ListViewItem item) {
            if (listView.InvokeRequired) {
                listView.Invoke((MethodInvoker)(() => listView.Items.Add(item)));
            } else {
                listView.Items.Add(item);
            }
        }

        public static void SetListViewItem(ListView listView, bool isEnabled) {
            if (listView.InvokeRequired) {
                listView.Invoke((MethodInvoker)(() => listView.Enabled = isEnabled));
            } else {
                listView.Enabled = isEnabled;
            }
        }

        public static void RemoveListViewItemAt(ListView listView, int index) {
            if (listView.InvokeRequired) {
                listView.Invoke((MethodInvoker)(() => listView.Items.RemoveAt(index)));
            } else {
                listView.Items.RemoveAt(index);
            }
        }

        public static void ReplaceListViewItemAt(ListView listView, ListViewItem item, int index) {
            if (listView.InvokeRequired) {
                listView.Invoke((MethodInvoker)(() => listView.Items[index] = item));
            } else {
                listView.Items[index] = item;
            }
        }

        public static void ClearListViewItem(ListView listView) {
            if (listView.InvokeRequired) {
                listView.Invoke((MethodInvoker)(() => listView.Items.Clear()));
            } else {
                listView.Items.Clear();
            }
        }

        public static void SelectListViewItem(ListView listView, int row) {
            if (listView.InvokeRequired) {
                listView.Invoke((MethodInvoker)(() => listView.Select()));
                listView.Invoke((MethodInvoker)(() => listView.Items[row].Selected = true));
                listView.Invoke((MethodInvoker)(() => listView.HideSelection = false));
            } else {
                listView.Select();
                listView.Items[row].Selected = true;
                listView.HideSelection = false;
            }
        }

        public static ListViewItem GetListViewItemAt(ListView listView, int index) {
            if (listView.InvokeRequired) {
                ListViewItem x = new ListViewItem();
                listView.Invoke((MethodInvoker)(() => x = listView.Items[index]));
                return x;
            } else {
                return listView.Items[index];
            }
        }

        public static bool IsListViewItemSelectedAt(ListView listView, int index) {
            if (listView.InvokeRequired) {
                bool x = false;
                listView.Invoke((MethodInvoker)(() => x = listView.Items[index].Selected));
                return x;
            } else {
                return listView.Items[index].Selected;
            }
        }

        #endregion

        #region Label
        public static void SetLabel(Label label, object value) {
            if (value != null) {
                if (label.InvokeRequired) {
                    label.Invoke((MethodInvoker)(() => label.Text = value.ToString()));
                } else {
                    label.Text = value.ToString();
                }
            }
        }

        public static void SetLabel(Label label, bool isEnabled) {
            if (label.InvokeRequired) {
                label.Invoke((MethodInvoker)(() => label.Enabled = isEnabled));
            } else {
                label.Enabled = isEnabled;
            }
        }

        public static void SetLabel(Label label, Color color) {
            if (label.InvokeRequired) {
                label.Invoke((MethodInvoker)(() => label.ForeColor = color));
            } else {
                label.ForeColor = color;
            }
        }
        #endregion

        #region TextBox
        public static void SetTextBox(TextBox textBox, object value) {
            if (value != null) {
                if (textBox.InvokeRequired) {
                    textBox.Invoke((MethodInvoker)(() => textBox.Text = value.ToString()));
                } else {
                    textBox.Text = value.ToString();
                }
            }
        }

        public static void SetTextBox(TextBox textBox, bool isEnabled) {
            if (textBox.InvokeRequired) {
                textBox.Invoke((MethodInvoker)(() => textBox.Enabled = isEnabled));
            } else {
                textBox.Enabled = isEnabled;
            }
        }
        #endregion

        #region RichTextBox
        public static void SetRichTextBox(RichTextBox richTextBox, object value) {
            if (value != null) {
                if (richTextBox.InvokeRequired) {
                    richTextBox.Invoke((MethodInvoker)(() => richTextBox.Text = value.ToString()));
                } else {
                    richTextBox.Text = value.ToString();
                }
            }
        }

        public static void SetRichTextBox(RichTextBox richTextBox, bool isEnabled) {
            if (richTextBox.InvokeRequired) {
                richTextBox.Invoke((MethodInvoker)(() => richTextBox.Enabled = isEnabled));
            } else {
                richTextBox.Enabled = isEnabled;
            }
        }
        #endregion

        #region GroupBox
        public static void SetGroupBox(GroupBox groupBox, object value) {
            if (value != null) {
                if (groupBox.InvokeRequired) {
                    groupBox.Invoke((MethodInvoker)(() => groupBox.Text = value.ToString()));
                } else {
                    groupBox.Text = value.ToString();
                }
            }
        }

        public static void SetGroupBox(GroupBox groupBox, bool isEnabled) {
            if (groupBox.InvokeRequired) {
                groupBox.Invoke((MethodInvoker)(() => groupBox.Enabled = isEnabled));
            } else {
                groupBox.Enabled = isEnabled;
            }
        }

        public static void SetGroupBoxVisibility(GroupBox groupBox, bool isVisible) {
            if (groupBox.InvokeRequired) {
                groupBox.Invoke((MethodInvoker)(() => groupBox.Visible = isVisible));
            } else {
                groupBox.Visible = isVisible;
            }
        }

        public static void SetGroupBoxControl(GroupBox groupBox, UserControl control) {
            if(control != null) {
                if (groupBox.InvokeRequired) {
                    groupBox.Invoke((MethodInvoker)(() => groupBox.Controls.Add(control)));
                } else {
                    groupBox.Controls.Add(control);
                }
            } else {
                if (groupBox.InvokeRequired) {
                    groupBox.Invoke((MethodInvoker)(() => groupBox.Controls.Clear()));
                } else {
                    groupBox.Controls.Clear();
                }
            }
        }
        #endregion

        #region Button
        public static void SetButton(Button button, object value) {
            if (value != null) {
                if (button.InvokeRequired) {
                    button.Invoke((MethodInvoker)(() => button.Text = value.ToString()));
                } else {
                    button.Text = value.ToString();
                }
            }
        }

        public static void SetButton(Button button, bool isEnabled) {
            if (button.InvokeRequired) {
                button.Invoke((MethodInvoker)(() => button.Enabled = isEnabled));
            } else {
                button.Enabled = isEnabled;
            }
        }
        #endregion

        #region CheckBox
        public static void SetCheckBox(CheckBox checkBox, object value) {
            if (value != null) {
                if (checkBox.InvokeRequired) {
                    checkBox.Invoke((MethodInvoker)(() => checkBox.Text = value.ToString()));
                } else {
                    checkBox.Text = value.ToString();
                }
            }
        }

        public static void SetCheckBox(CheckBox checkBox, bool isEnabled) {
            if (checkBox.InvokeRequired) {
                checkBox.Invoke((MethodInvoker)(() => checkBox.Enabled = isEnabled));
            } else {
                checkBox.Enabled = isEnabled;
            }
        }

        public static void SetCheckBoxChecked(CheckBox checkBox, bool isEnabled) {
            if (checkBox.InvokeRequired) {
                checkBox.Invoke((MethodInvoker)(() => checkBox.Checked = isEnabled));
            } else {
                checkBox.Checked = isEnabled;
            }
        }

        public static void SetCheckBoxVisibility(CheckBox checkBox, bool isVisible) {
            if (checkBox.InvokeRequired) {
                checkBox.Invoke((MethodInvoker)(() => checkBox.Visible = isVisible));
            } else {
                checkBox.Visible = isVisible;
            }
        }
        #endregion

        #region ComboBox
        public static void SetComboBoxItem(ComboBox comboBox, object value) {
            if (value != null) {
                if (comboBox.InvokeRequired) {
                    comboBox.Invoke((MethodInvoker)(() => comboBox.Items.Add(value)));
                } else {
                    comboBox.Items.Add(value);
                }
            }
        }

        public static void SetComboBoxSelectedIndex(ComboBox comboBox, int index) {
            if (comboBox.InvokeRequired) {
                comboBox.Invoke((MethodInvoker)(() => comboBox.SelectedIndex = index));
            } else {
                comboBox.SelectedIndex = index;
            }
        }

        public static void SetComboBox(ComboBox comboBox, bool isEnabled) {
            if (comboBox.InvokeRequired) {
                comboBox.Invoke((MethodInvoker)(() => comboBox.Enabled = isEnabled));
            } else {
                comboBox.Enabled = isEnabled;
            }
        }
        #endregion

        #region NumericUpDown
        public static void SetNumericUpDown(NumericUpDown numericUpDown, object value) {
            if (value != null) {
                if (numericUpDown.InvokeRequired) {
                    numericUpDown.Invoke((MethodInvoker)(() => numericUpDown.Value = decimal.Parse(value.ToString())));
                } else {
                    numericUpDown.Value = decimal.Parse(value.ToString());
                }
            }
        }
        #endregion
    }
}
