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

namespace HwComWrapperNETGui
{
    public partial class Manipulation64Dialog : Form
    {
        #region Construction

        public Manipulation64Dialog()
        {
            InitializeComponent();

            this.cbCyclic.Checked = false;
            this.cbCyclic.Enabled = false;
            this.cbStartManipulations.Checked = false;
            this.cbStartManipulations.CheckedChanged += (sender, args) =>
            {
                this.cbCyclic.Enabled = this.cbStartManipulations.Checked;
            };

        }

        #endregion

        #region Public Properties

        public IList<Manipulation64> RbsManipulations64
        {
            get
            {
                var manipulations = new List<Manipulation64>(lvSequence.Items.Count);
                foreach (ListViewItem item in lvSequence.Items)
                {
                    manipulations.Add(item.Tag as Manipulation64);
                }

                return manipulations.AsReadOnly();
            }
        }

        public bool Cyclic => cbCyclic.Checked;

        public bool StartManipulations => cbStartManipulations.Checked;

        #endregion

        #region Private Methods

        private void UpdateLvItem(ListViewItem item)
        {
            item.SubItems.Clear();
            var m = item.Tag as Manipulation64;
            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
            var trigerTargetType = string.Empty;
            var trigerModeValue = string.Empty;
            var trigerHysterese = string.Empty;
            var trigerNr = 0;
            foreach (Trigger64 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)
        {
            var item = new ListViewItem();
            item.Tag = new Manipulation64();

            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)
            {
                var m = lvSequence.SelectedItems[0].Tag as Manipulation64;
                m.AddTrigger(new Trigger64());

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

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

                Trigger64 lastTrigger = null;

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

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

        #endregion
    }
}