using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using EMDServer.Common;

namespace EMDHardwareClientSimulation.Core.Devices
{
    /// <summary>
    /// The Device class absracts the end devices such 
    /// as ZigBee Dimmer, Lamps, PowerMeters or Displays
    /// </summary>
    public class Device
    {
        #region Class variables
        // ----------------------------------------
        protected int deviceId;
        protected Constants.EMD_DEVICE_TYPE deviceType;
        protected int deviceValue;

        protected iEMDConnection gateway;
        protected MainController mainController;
        // ----------------------------------------
        #endregion

        #region Public methods
        // ----------------------------------------
        /// <summary>
        /// Creates a new instance of the Device class
        /// </summary>
        public Device()
        {
            Debug.WriteLine("DEVICE - Instantiated!");
        }

        /// <summary>
        /// Creates a new instance of the Device class
        /// </summary>
        /// <param name="gateway">iEMDConnection interface to the Device</param>
        public Device(iEMDConnection gateway)
        {
            this.gateway = gateway;

            Debug.WriteLine("DEVICE - Instantiated with params EMDINTERFACE!");
        }


        /// <summary>
        /// Creates a new instance of the Device class
        /// </summary>
        /// <param name="id">int id of the Device</param>
        /// <param name="type">type of the Device</param>
        /// <param name="gateway">iEMDConnection interface to the Device</param>
        public Device(int id, Constants.EMD_DEVICE_TYPE type, iEMDConnection gateway)
        {
            this.deviceId = id;
            this.deviceType = type;
            this.deviceValue = 0;
            this.gateway = gateway;

            Debug.WriteLine("DEVICE - Instantiated with params ID=" + id + ", TYPE=" + type + ", EMDINTERFACE!");
        }

        /// <summary>
        /// Sets a reference to the main controller
        /// </summary>
        /// <param name="mc">MainController object</param>
        public void setMainController(MainController mc)
        {
            this.mainController = mc;

            Debug.WriteLine("DEVICE - MainController set!");
        }

        /// <summary>
        /// Sets the actual value of the Device
        /// </summary>
        /// <param name="value">int value of the Device</param>
        public void sendValue()
        {
            // mc.send
            if (this.mainController != null)
            {
                //float sendValue = (float)(Math.Pow(5d, deviceValue / 255.0) - 1) / 4.0f * 60.0f;

                Command c = new Command((byte)deviceId, (byte)Constants.EMD_COMMAND.SET_VALUE, deviceValue);
                this.mainController.SendCommand(c);

                Debug.WriteLine("DEVICE - Value changed: VALUE=" + deviceValue);
            }
        }
        // ----------------------------------------
        #endregion

        #region Properties
        // ----------------------------------------
        /// <summary>
        /// Gets or sets the int id of the device
        /// </summary>
        public int DeviceId
        {
            get { return deviceId; }
            set { deviceId = value; }
        }
        
        /// <summary>
        /// Gets or sets the int type of the device
        /// </summary>
        public Constants.EMD_DEVICE_TYPE DeviceType
        {
            get { return deviceType; }
            set { deviceType = value; }
        }

        /// <summary>
        /// Gets or sets the int value of the device
        /// </summary>
        public int DeviceValue
        {
            get { return deviceValue; }
            set { deviceValue = value; }
        }

        public iEMDConnection Gateway
        {
            get { return gateway; }
            set { gateway = value; }
        }
        // ----------------------------------------
        #endregion
    }
}
