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

namespace EMDWebserver
{
    public class MobileManager
    {
        #region Class variables
        // ----------------------------------------
        public List<string> iPhones {get;set;}
        private SortedDictionary<int, Command> pendingCommands;
        private Dictionary<string, Command[]> latestDeviceCmdList;
        private static Constants.EMD_COMMAND[] CallbackValues = new Constants.EMD_COMMAND[] { Constants.EMD_COMMAND.GET_POWER_VALUE, Constants.EMD_COMMAND.GET_VALUE};
        // ----------------------------------------
        #endregion

        #region Public methods
        // ----------------------------------------
        public MobileManager()
        {
            this.iPhones = new List<string>();
            this.pendingCommands = new SortedDictionary<int, Command>();
            this.latestDeviceCmdList = new Dictionary<string, Command[]>();

            Command cmd1 = new Command(114, 10, new byte[] { 2, 3, 5, 9 });
            Command cmd2 = new Command(115, 10, new byte[] { 1, 8, 9, 1 });

            //this.pendingCommands.Add(114, cmd1);
            //this.pendingCommands.Add(115, cmd2);
        }

        public bool isAvailable(string ipAddress)
        {
            return this.iPhones.Contains(ipAddress);
        }

        public bool isCallbackCommand(Command cmd)
        {
            bool isCallbackValue = false;

            for (int i = 0; i < CallbackValues.Length; i++)
            {
                if ((Constants.EMD_COMMAND) cmd.Cmd == CallbackValues[i])
                {
                    isCallbackValue = true;
                }
            }

            return isCallbackValue;
        }

        public void addDevice(string ipAddress)
        {
            this.iPhones.Add(ipAddress);
            this.latestDeviceCmdList.Add(ipAddress, new Command[2]);
        }

        public SortedDictionary<int, Command> getActualCommands(string ipAddress)
        {
            lock (this)
            {
                return this.pendingCommands;
            }
        }

        public void addCommand(int id, Command cmd)
        {
            lock (this)
            {
                if (this.pendingCommands.ContainsKey(id))
                    this.pendingCommands[id] = cmd;
                else
                    this.pendingCommands.Add(id, cmd);
            }
        }
        // ----------------------------------------
        #endregion
    }
}
