/******************************************************************************
 * Copyright AllSeen Alliance. All rights reserved.
 *
 *    Permission to use, copy, modify, and/or distribute this software for any
 *    purpose with or without fee is hereby granted, provided that the above
 *    copyright notice and this permission notice appear in all copies.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 ******************************************************************************/


#ifndef __TIMECLIENT__TIMERSIGNALHANDLER__
#define __TIMECLIENT__TIMERSIGNALHANDLER__

#include <alljoyn/InterfaceDescription.h>
#include <alljoyn/MessageReceiver.h>
#include <alljoyn/services_common/AsyncTaskQueue.h>
#include <alljoyn/time/TimeServiceClientTimer.h>
#include "../common/Mutex.h"

namespace ajn {
namespace services {

class TimerSignalTask;

class TimerSignalHandler : public MessageReceiver {

  public:
    /**
     * GetInstance
     * @return TimeServiceSignalHandler created only once.
     */
    static TimerSignalHandler* getInstance();

    /**
     * Adds TimeServiceClientTimer event listener
     * @param timer
     * @return status
     */
    QStatus registerTimer(TimeServiceClientTimer*Timer);

    /**
     * Remove TimeServiceClientTimer event listener
     * @param timer
     */
    void unRegisterTimer(TimeServiceClientTimer*Timer);

  private:

    /**
     * Constructor
     */
    TimerSignalHandler();
    /**
     * Desctructor
     */
    virtual ~TimerSignalHandler();

    /**
     *  pointer to TimerSignalHandler
     */
    static TimerSignalHandler* s_Instance;

    std::vector<TimeServiceClientTimer*> m_Timers;

    Mutex m_Protector;

    const ajn::InterfaceDescription::Member* m_TimerSignalMethod;
    const ajn::InterfaceDescription::Member* m_TimerRunStateChangedSignalMethod;

    static AsyncTaskQueue s_SignalQueue;

    static TimerSignalTask s_SignalTask;

    void handleTimerSignal(const ajn::InterfaceDescription::Member* member,
                           const char* srcPath, ajn::Message& msg);

    TimeServiceClientTimer* findTimerByObjectPathForDevice(qcc::String objPath, qcc::String sender);
};

} /* namespace services */
} /* namespace ajn */

#endif /* defined(__TIMECLIENT__TIMERSIGNALHANDLER__) */
