#include "SystemTrayManager.h"
#include "MasterCtrl.h"
#include <QCursor>

CSystemTrayManager::CSystemTrayManager()
{

    mProgramHandle = 0;

    mTrayMenu = new QMenu();

    connect(&mSystemTrayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(TrayIconActivated(QSystemTrayIcon::ActivationReason)));
    connect(&mSystemTrayIcon,SIGNAL(messageClicked()),this,SLOT(TrayBaloonMessageClicked()));
    connect(mTrayMenu,SIGNAL(triggered(QAction*)),this,SLOT(TrayMenuClicked(QAction*)));

    mShowSettingsGUIAction = mTrayMenu->addAction("Settings");
    mQuitAction = mTrayMenu->addAction("Quit MasterController");
    mSystemTrayIcon.setIcon(QIcon("./Ico/icon.png"));
    mSystemTrayIcon.setToolTip("MasterController :)");
    mSystemTrayIcon.show();
    mSystemTrayIcon.setContextMenu(mTrayMenu);

}
CSystemTrayManager::~CSystemTrayManager()
{
    mSystemTrayIcon.hide();
    delete mTrayMenu;
}

void CSystemTrayManager::TrayBaloonMessageClicked()
{
    qDebug("Baloon message clicked");
}

void CSystemTrayManager::TrayIconActivated(QSystemTrayIcon::ActivationReason Reason)
{
    qDebug("Tray icon activated %d",Reason);
  //  mSystemTrayIcon.showMessage("Icon Clicked","The icon has\nbeen clicked.");
    switch(Reason)
    {
        case QSystemTrayIcon::Unknown:
        {
            break;
        }
        case QSystemTrayIcon::Context:
        {
            break;
        }
        case QSystemTrayIcon::DoubleClick:
        {
            break;
        }
        case QSystemTrayIcon::Trigger:
        {
            mTrayMenu->popup(QCursor::pos());
            break;
        }
        case QSystemTrayIcon::MiddleClick:
        {
            break;
        }
    }
}

void CSystemTrayManager::TrayMenuClicked(QAction *Menu)
{
    if(Menu == mQuitAction)
    {
        qDebug("Quit");
        mProgramHandle->QuitApplicationRequest();
    }
    else if(Menu == mShowSettingsGUIAction)
    {
        qDebug("Settings");
        mProgramHandle->ShowSettingsWindowRequest();
    }

}
