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

CSystemTrayManager::CSystemTrayManager(CTrayVolumeCtrl *VolumeCtrlWidget)
{

    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*)));


    QWidgetAction *test = new QWidgetAction(0);
    test->setDefaultWidget(VolumeCtrlWidget);
//    mTrayVolumeAction = new CTrayVolumeMenuAction("Volume");
//    mShowSettingsGUIAction = mTrayMenu->addAction("Settings");
    mQuitAction = mTrayMenu->addAction("Quit SMS Client");
    mTrayMenu->addSection("Display");
    mCloneDisplaysAction = mTrayMenu->addAction("Clone");
    mExtendDisplaysAction = mTrayMenu->addAction("Extend");
    mTrayMenu->addSeparator();
     mTrayMenu->addAction(test);
    //mTrayMenu->setMinimumWidth(300);


    mSystemTrayIcon.setIcon(QIcon("./Ico/sms.png"));
    mSystemTrayIcon.setToolTip("SMS Client :)");
    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:
        {
  //          mTrayVolumeCtrl->show();
            qDebug("Context");
            break;
        }
        case QSystemTrayIcon::DoubleClick:
        {
            break;
        }
        case QSystemTrayIcon::Trigger:
        {
            //qDebug("Trigger");
            //mTrayMenu->popup(QCursor::pos());
            //mProgramHandle->RespawnMainWindowRequest();
 //           mTrayVolumeCtrl->show();
            mProgramHandle->TrayIconLeftClick();
            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();
    }
    else if(Menu == mCloneDisplaysAction)
    {
        system("C:\\Windows\\System32\\DisplaySwitch.exe /clone");
    }
    else if(Menu == mExtendDisplaysAction)
    {
        system("C:\\Windows\\System32\\DisplaySwitch.exe /extend");
    }

}

int CSystemTrayManager::NewSMSMessagesPendingCount(int count)
{
    if(count == 0)
    {
        mSystemTrayIcon.setIcon(QIcon("./Ico/sms.png"));
    }
    else
    {
        mSystemTrayIcon.setIcon(QIcon("./Ico/sms_new.png"));
    }

    return RET_OK;
}
