using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Ebel.Hardware.Communications.Rbs;

namespace HwComWrapperNETGui
{
    public partial class RbsSequenceDialog : Form
    {
        public RbsSequenceDialog()
        {
            InitializeComponent();
        }

        public IList<Manipulation> RbsManipulationSequence
        {
            get
            {
                List<Manipulation> sequence = new List<Manipulation>(lvSequence.Items.Count);
                foreach (ListViewItem item in lvSequence.Items)
                {
                    sequence.Add(item.Tag as Manipulation);
                }
                return sequence.AsReadOnly();
            }
        }

        public bool Cyclic
        {
            get { return cbCyclic.Checked; }
        }

        public void SetCyclic(bool useCyclic)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => this.cbCyclic.Checked = useCyclic));
                return;
            }

            this.cbCyclic.Checked = useCyclic;
        }

        private void UpdateLvItem(ListViewItem item)
        {
            item.SubItems.Clear();
            Manipulation m = item.Tag as Manipulation;
            if (m == null) return;

            // type
            item.Text = m.ManipulationType.ToString();
            // Value 1
            item.SubItems.Add(m.ManipulationValue1.ToString());
            // Value 2
            item.SubItems.Add(m.ManipulationValue2.ToString());
            // Steps
            item.SubItems.Add(m.ManipulationValue3.ToString());
            // Mode and Target
            item.SubItems.Add(String.Format("{0}: {1}", m.ManipulationMode, m.ManipulationTarget.ToString()));
            // Delay Type and Value
            item.SubItems.Add(String.Format("{0}: {1}", m.DelayType, m.DelayValue));
            // Duration Type and Value
            item.SubItems.Add(String.Format("{0}: {1}", m.ManipulationDurationType, m.ManipulationDuration));

            // Format the trigger items
            string trigerTargetType = string.Empty;
            string trigerModeValue = string.Empty;
            string trigerHysterese = string.Empty;
            int trigerNr = 0;
            foreach (Trigger trig in m.Triggers)
            {
                trigerNr++;
                trigerTargetType += string.Format("Trigger {0}: {1}; ", trigerNr, trig.TargetType);
                trigerModeValue += string.Format("Trigger {0}: {1}: {2} - {3}; ", trigerNr, trig.Mode, trig.Source, trig.Value);
                trigerHysterese += string.Format("Trigger {0}: {1}; ", trigerNr, trig.Hysterese);
            }

            // Trigger Type
            item.SubItems.Add(trigerTargetType);
            // Trigger Mode and Value
            item.SubItems.Add(trigerModeValue);
            // Trigger Hysterese
            item.SubItems.Add(trigerHysterese);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ListViewItem item = new ListViewItem();
            item.Tag = new Manipulation();

            UpdateLvItem(item);

            lvSequence.Items.Add(item);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in lvSequence.SelectedItems)
            {
                lvSequence.Items.Remove(item);
            }
        }

        private void lvSequence_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvSequence.SelectedItems.Count != 1)
            {
                pgMaipulation.SelectedObject = null;
                btnAddTrigger.Enabled = false;
                btnRemoveTrigger.Enabled = false;
            }
            else
            {
                pgMaipulation.SelectedObject = lvSequence.SelectedItems[0].Tag;
                btnAddTrigger.Enabled = true;
                btnRemoveTrigger.Enabled = true;
            }
        }

        private void pgMaipulation_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            if (lvSequence.SelectedItems.Count == 1)
            {
                UpdateLvItem(lvSequence.SelectedItems[0]);
            }
        }

        private void btnAddTrigger_Click(object sender, EventArgs e)
        {
            if (lvSequence.SelectedItems.Count > 0)
            {
                Manipulation m = lvSequence.SelectedItems[0].Tag as Manipulation;
                m.AddTrigger(new Trigger());

                UpdateLvItem(lvSequence.SelectedItems[0]);
                pgMaipulation.SelectedObject = m;
            }
        }

        private void btnRemoveTrigger_Click(object sender, EventArgs e)
        {
            if (lvSequence.SelectedItems.Count > 0)
            {
                Manipulation m = lvSequence.SelectedItems[0].Tag as Manipulation;
                Trigger lastTrigger = null;

                foreach (Trigger t in m.Triggers) lastTrigger = t;

                if (lastTrigger != null)
                {
                    m.RemoveTrigger(lastTrigger);
                    UpdateLvItem(lvSequence.SelectedItems[0]);
                    pgMaipulation.SelectedObject = m;
                }
            }
        }
    }
}