using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using EMDServer.Common;
using EMDHardwareClientSimulation.Core.Devices;

namespace EMDHardwareClientSimulation.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.GET_POWER_VALUE:
                        if (this.mainController != null)
                        {
                            // mach es neus command mitm aktuelle powervalue vam device
                            PowerMeter d = (PowerMeter)this.mainController.GetDeviceById((int)cmd.Id);

                            if (d != null)
                            {
                                // create new power value array and set all fields to zero
                                byte[] powerValue = new byte[4];
                                powerValue[0] = 0;
                                powerValue[1] = 0;
                                powerValue[2] = 0;
                                powerValue[3] = 0;

                                float fPowerValue = (float)Math.Round(d.DeviceValue * 0.23529411764705882352941176470588, 2);
                                string sPowerValue = fPowerValue.ToString();

                                // split power value into part before and part after comma
                                string[] parts = sPowerValue.Split('.');

                                string partBeforeComma = parts[0];
                                string partAfterComma = string.Empty;
                                if (parts.Length > 1)
                                {
                                    partAfterComma = parts[1];
                                }

                                if (d.DeviceValue != 0)
                                {
                                    if (partBeforeComma.Length == 3)
                                    {
                                        powerValue[0] = 0;
                                        powerValue[1] = (byte)(int.Parse(partBeforeComma[0].ToString()));
                                        powerValue[2] = (byte)(int.Parse(partBeforeComma[1].ToString()));
                                        powerValue[3] = (byte)(int.Parse(partBeforeComma[2].ToString()));
                                    }
                                    else if (partBeforeComma.Length == 2)
                                    {
                                        powerValue[0] = 1;
                                        powerValue[1] = (byte)(int.Parse(partBeforeComma[0].ToString()));
                                        powerValue[2] = (byte)(int.Parse(partBeforeComma[1].ToString()));

                                        if (partAfterComma.Length >= 1)
                                        {
                                            powerValue[3] = (byte)(int.Parse(partAfterComma[0].ToString()));
                                        }
                                    }
                                    else if (partBeforeComma.Length == 1)
                                    {
                                        powerValue[0] = 2;
                                        powerValue[1] = (byte)(int.Parse(partBeforeComma[0].ToString()));

                                        if (partAfterComma.Length > 1)
                                        {
                                            powerValue[2] = (byte)(int.Parse(partAfterComma[0].ToString()));
                                            powerValue[3] = (byte)(int.Parse(partAfterComma[1].ToString()));
                                        }
                                        else if (partAfterComma.Length == 1)
                                        {
                                            powerValue[2] = (byte)(int.Parse(partAfterComma[0].ToString()));
                                            powerValue[3] = (byte)0;
                                        }
                                        else if (partAfterComma.Length == 0)
                                        {
                                            powerValue[2] = 0;
                                            powerValue[3] = 0;
                                        }
                                    }
                                }

                                Command c = new Command(cmd.Id, (byte)Constants.EMD_COMMAND.POWER_VALUE, powerValue);
                                this.mainController.SendCommand(c);
                            }
                        }
                        break;
                    case (int)Constants.EMD_COMMAND.GET_VALUE:
                        if (this.mainController != null)
                        {
                            // mach es neus command mitm aktuelle value vom device
                            Device d = this.mainController.GetDeviceById((int)cmd.Id);

                            if (d != null)
                            {
                                Command c = new Command(cmd.Id, (byte)Constants.EMD_COMMAND.VALUE, d.DeviceValue);
                                this.mainController.SendCommand(c);
                            }
                        }
                        break;
                    case (int)Constants.EMD_COMMAND.SET_VALUE:
                        if (this.mainController != null)
                        {
                            // setz der aktuell wärt vam device
                            Device d = this.mainController.GetDeviceById(cmd.Id);
                            Device pm;

                            if (d != null)
                            {
                                d.DeviceValue = cmd.getIntValue();                                
                                float powerValue = cmd.getIntValue() * 0.23529411764705882352941176470588f;

                                if (d.DeviceId == 10)
                                {                                    
                                    pm = this.mainController.GetDeviceById(11);
                                    pm.DeviceValue = cmd.getIntValue();

                                    this.mainController.Ui.setDimmerText(cmd.getIntValue().ToString());

                                    if (powerValue.ToString().Length > 3)
                                    {
                                        this.mainController.Ui.setPowerMeterText(powerValue.ToString().Substring(0, 4) + "W");
                                    }
                                    else
                                    {
                                        this.mainController.Ui.setPowerMeterText(powerValue.ToString() + "W");
                                    }

                                    if (d.DeviceValue > 0)
                                    {
                                        this.mainController.Ui.setLamp(true);
                                    }
                                    else
                                    {
                                        this.mainController.Ui.setLamp(false);
                                    }
                                }
                                else if (d.DeviceId == 20)
                                {
                                    pm = this.mainController.GetDeviceById(21);
                                    pm.DeviceValue = cmd.getIntValue();

                                    this.mainController.Ui.setDimmer2Text(cmd.getIntValue().ToString());

                                    if (powerValue.ToString().Length > 3)
                                    {
                                        this.mainController.Ui.setPowerMeter2Text(powerValue.ToString().Substring(0, 4) + "W");
                                    }
                                    else
                                    {
                                        this.mainController.Ui.setPowerMeter2Text(powerValue.ToString() + "W");
                                    }

                                    if (d.DeviceValue > 0)
                                    {
                                        this.mainController.Ui.setLamp2(true);
                                    }
                                    else
                                    {
                                        this.mainController.Ui.setLamp2(false);
                                    }
                                }
                            }
                            
                        }
                        break;
                }

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