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

namespace EMDServer.Core
{
    class CommandProcessor
    {
        #region Class variables
        // ----------------------------------------
        private MainController mainController;
        // ----------------------------------------
        #endregion

        #region Public methods
        // ----------------------------------------
        /// <summary>
        /// Creates a new instance of the class which is
        /// responsible for interpreting the incoming 
        /// messages from input devices
        /// </summary>
        public CommandProcessor()
        {
            //Debug.WriteLine("COMMANDPROCESSOR - Instantiated!");
        }

        /// <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("COMMANDPROCESSOR - MainController set!");
        }

        /// <summary>
        /// Processes the received messages, decodes the 
        /// commands and executes the appropriate function calls
        /// </summary>
        /// <param name="cmd">received Command</param>
        /// <param name="emdConnection">connection where command came from</param>
        public void processCommand(Command cmd, iEMDConnection emdConnection)
        {
             // create new Command with the received values
            try
            {

                switch ((int)cmd.Cmd)
                {
                    case (int)Constants.EMD_COMMAND.ADD_DEVICE:
                        if (this.mainController != null)
                        {
                            this.mainController.addDevice((int)cmd.Id, (Constants.EMD_DEVICE_TYPE)cmd.getDeviceType(), emdConnection);
                        }
                        break;
                    case (int)Constants.EMD_COMMAND.REMOVE_DEVICE:
                        if (this.mainController != null)
                        {
                            this.mainController.removeDevice((int)cmd.Id);
                        }
                        break;
                    case (int)Constants.EMD_COMMAND.BUTTON_RELEASED:
                        if (this.mainController != null)
                        {
                            this.mainController.buttonReleased((int)cmd.Id);
                        }
                        break;
                    case (int)Constants.EMD_COMMAND.BUTTON_PRESSED:
                        if (this.mainController != null)
                        {
                            this.mainController.buttonPressed((int)cmd.Id);
                        }
                        break;
                    case (int)Constants.EMD_COMMAND.TOGGLE_VALUE:
                        if (this.mainController != null)
                        {
                            this.mainController.toggleSwitch((int)cmd.Id);
                        }
                        break;
                    // received commands will be redirected to all devices which are 
                    // connected to the sender of the command
                    default:
                        if (this.mainController != null)
                        {
                            this.mainController.sendCommand(cmd);
                        }
                        break;
                }

                //Debug.WriteLine("COMMANDPROCESSOR - Command processed, ID=" + cmd.Id + " CMD=" + cmd.Cmd + " VAL=" + Util.Utils.getInstance().ConvertByteToDecimal( cmd.Val) );
            }
            catch (Exception e)
            {
                Debug.WriteLine("COMMANDPROCESSOR - Error: Invalid Command received! Exception: " + e.Message);
            }
        }
        // ----------------------------------------
        #endregion
    }
}
