using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Xml.Serialization;

namespace EMDServer.Common
{
    [Serializable]
    public class Command:MarshalByRefObject
    {
        #region Class variables
        // ----------------------------------------
        private byte id;
        private byte cmd;
        private byte[] val;
        // ----------------------------------------
        #endregion

        #region Public methods
        // ----------------------------------------
        /// <summary>
        /// Creates a new instance of the class which 
        /// contains a Device Command
        /// </summary>
        public Command()
        {
            id = 0;
            cmd = 0;
            val = new byte[4];
        }

        #region Unused
        ///// <summary>
        ///// Creates a new instance of the class which
        ///// containts a Device Command
        ///// </summary>
        ///// <param name="id">byte Device id</param>
        ///// <param name="val">byte[4] array value</param>
        //public Command(byte id, byte[] val)
        //{
        //    this.id = id;
        //    this.cmd = 0;
        //    this.val = val;
        //}

        ///// <summary>
        ///// Creates a new instance of the class which 
        ///// contains a Device Command
        ///// </summary>
        ///// <param name="id">byte Device id</param>
        ///// <param name="val">float value</param>
        //public Command(byte id, float val)
        //{
        //    this.id = id;
        //    this.cmd = 0;
        //    this.val = BitConverter.GetBytes(val);
        //}
        #endregion

        /// <summary>
        /// Crates a new instance of the class which
        /// contains a Device Command
        /// </summary>
        /// <param name="id">byte Device id</param>
        /// <param name="cmd">byte cmd</param>
        /// <param name="val">byte[4] array value</param>
        public Command(byte id, byte cmd, byte[] val)
        {
            this.id = id;
            this.cmd = cmd;
            this.val = val;
        }

        /// <summary>
        /// Creates a new instance of the class which 
        /// contains a Device Command
        /// </summary>
        /// <param name="id">byte Device id</param>
        /// <param name="cmd">byte cmd</param>
        /// <param name="val">int value</param>
        public Command(byte id, byte cmd, Int32 val)
        {
            this.id = id;
            this.cmd = cmd;
            this.val = BitConverter.GetBytes(val);
        }

        /// <summary>
        /// Creates a new instance of the class which 
        /// contains a Device Command
        /// </summary>
        /// <param name="id">byte Device id</param>
        /// <param name="cmd">byte cmd</param>
        /// <param name="val">float value</param>
        public Command(byte id, byte cmd, float val)
        {
            this.id = id;
            this.cmd = cmd;
            this.val = BitConverter.GetBytes(val);
        }

        /// <summary>
        /// Gets the command as an output Command in a
        /// byte[] array with 6 fields (id -> 1 Byte, cmd -> 1 Byte, val -> 4 Bytes)
        /// </summary>
        /// <returns>byte[6] array with the Command</returns>
        public byte[] getOutputCommand()
        {
            byte[] result = new byte[6];
            result[0] = id;
            result[1] = cmd;
            result[2] = val[0];
            result[3] = val[1];
            result[4] = val[2];
            result[5] = val[3];

            return result;
        }        
        
        /// <summary>
        /// Returns the value byte array as a float
        /// </summary>
        /// <returns>float value of the value byte array</returns>
        public float getFloatValue()
        {
            float result = 0;

            try
            {
                result = BitConverter.ToSingle(val, 0);
            }
            catch (Exception e)
            {                
                Debug.WriteLine("COMMAND - Exception!!!!\t" + e.Message);
            }

            return result;
        }

        /// <summary>
        /// Returns the value byte array as an int
        /// </summary>
        /// <returns>int value of the value byte array</returns>
        public int getIntValue()
        {
            int result = 0;

            try
            {
                result = BitConverter.ToInt32(val, 0);
            }
            catch (Exception e)
            {
                Debug.WriteLine("COMMAND - Exception!!!!\t" + e.Message);
            }

            return result;
        }

        public int getDeviceType()
        {
            int result = 0;

            result = (int)this.val[0];

            return result;
        }
        // ----------------------------------------
        #endregion

        #region Properties
        // ----------------------------------------
        [XmlElement("ID")]
        public byte Id
        {
            get { return id; }
            set { id = value; }
        }

        [XmlElement("CMD")]
        public byte Cmd
        {
            get { return cmd; }
            set { cmd = value; }
        }

        [XmlElement("VALUE")]
        public byte[] Val
        {
            get { return val; }
            set { val = value; }
        }
        // ----------------------------------------
        #endregion

        #region Override
        // ----------------------------------------
        public override string ToString()
        {
            return "Gerät vom Typ " + ((int)this.val[0]).ToString() + " mit values: " + this.val[1];
        }
        // ----------------------------------------
        #endregion
    }
}
