/**
 * Get task info from Task scheduler.
 * Returns 0 if task found and no errors, -1 if task not found.
 * If task found: frequency, dayNum, minNum and statusCode are set.
 *
 * @param active  : [OUT] true if task is active
 * @param dayNum  : [OUT] repeating every 'dayNum' days
 * @param minNum  : [OUT] repeating every 'minNum' minutes
 * @return          0 = task found, status correct.
 *                  1 = task found, status incorrect.
 *                  2 = task found but manually changed.
 *                 -1 = task not found.
 *                 -2 = errors occurred.
 */
int getScheduleTask(bool* active, int* dayNum, int* minNum) {

    int ret = 0;
    HRESULT hr = S_OK;
    ITaskScheduler* pITS = NULL;
    ITask*          pITask;
    wstring taskName;

    // Init COM library & create instance for Task scheduler.
    if ((pITS = initScheduleInstance()) == NULL) {
        goto error;
    }

    // Task is associated with current user
    if (getScheduledTaskName(taskName)) {
        goto error;
    }

    //
    // Open the task (fails if task not existing).
    //
    hr = pITS->Activate(taskName.c_str(),
                        IID_ITask,
                        (IUnknown**) &pITask);
    pITS->Release();
    if (FAILED(hr)) {
        goto notExisting;
    }


    //
    // Check task status
    //
    DWORD pdwExitCode;
    hr = pITask->GetExitCode(&pdwExitCode);
    if ((hr & SCHED_S_TASK_HAS_NOT_RUN) || (hr != S_OK)) {
        setErrorF(getLastErrorCode(), DBG_SCHED_LAST_EXECUTION);
        LOG.debug(getLastErrorMsg());
        ret = 1;
    }


    //
    // Check the flags -> active/disabled
    //
    DWORD flag = -1, triggerType = -1;
    pITask->GetFlags(&flag);
    if (flag & TASK_FLAG_DISABLED) {
        *active = false;
    } else {
        *active = true;
    }


    //
    // Get the trigger interface.
    //
    ITaskTrigger *pITaskTrigger;
    hr = pITask->GetTrigger(0, &pITaskTrigger);
    if (FAILED(hr)) {
        char* msg = readSystemErrorMsg(hr);
        setErrorF(getLastErrorCode(), ERR_SCHED_GET_TRIGGER, hr, msg);
        delete [] msg;
        goto error;
    }

    TASK_TRIGGER pTrigger;
    ZeroMemory(&pTrigger, sizeof(TASK_TRIGGER));
    hr = pITaskTrigger->GetTrigger(&pTrigger);
    if (FAILED(hr)) {
        char* msg = readSystemErrorMsg(hr);
        setErrorF(getLastErrorCode(), ERR_SCHED_GET_TRIGGER2, hr, msg);
        delete [] msg;
        goto error;
    }

    //
    // Get values
    //
    int minToSet = pTrigger.MinutesInterval;
    if (minToSet == 1439) {
        minToSet = 1440;    // the value must normalized to get 1 day
    }

    *minNum = minToSet; // pTrigger.MinutesInterval;
    *dayNum = pTrigger.Type.Daily.DaysInterval;

    // Check if trigger correct
    triggerType = pTrigger.TriggerType;
    if (triggerType != TASK_TIME_TRIGGER_DAILY ||
        (pTrigger.MinutesDuration != 0
         && pTrigger.MinutesInterval != 0
         && pTrigger.MinutesDuration != 1440) ) {
        ret = 2;
    }

    // Release the ITask interface.
    pITaskTrigger->Release();
    pITask->Release();

    CoUninitialize();
    return ret;

notExisting:
    LOG.debug(DBG_SCHED_TASK_NOT_FOUND);
    *active = false;
    *minNum = 0;
    *dayNum = 0;
    CoUninitialize();
    return -1;

error:
    LOG.error(getLastErrorMsg());
    CoUninitialize();
    return -2;
}
//---------------------------------------------------------------------------
int TTaskScheduler::GetTaskInfo(wstring taskName, TTaskParams& taskParams){
    ITask *pITask = Activate(taskName);

    if(pITask == NULL){
        return 0;
    }

    LPWSTR ppwszAccountName;
    LPWSTR ppwszComment;
    LPWSTR ppwszCreator;
    LPWSTR lpwszApplicationName;
    LPWSTR lpwszParameters;
    LPWSTR lpwszWorkDir;

    DWORD pdwExitCode;
    DWORD pdwRunTime;
    DWORD pdwPriority;

    WORD pwIdleMinutes;
    WORD pwDeadlineMinutes;
    SYSTEMTIME pstLastRun;
    SYSTEMTIME pstNextRun;
    HRESULT phrStatus;


  
    HRESULT hr = pITask->GetAccountInformation(&ppwszAccountName);
    if (FAILED(hr)){
        sprintf(lastError, "Failed to retreive task info, error = 0x%x\n",hr);
        taskParams.accountName = L"";
    }else{
        taskParams.accountName = ppwszAccountName;
        CoTaskMemFree(ppwszAccountName);
    }

    hr = pITask->GetApplicationName(&lpwszApplicationName);
    if (FAILED(hr)){
        sprintf(lastError, "Failed to retreive task info, error = 0x%x\n",hr);
    }else{
        taskParams.appName = lpwszApplicationName;
        CoTaskMemFree(lpwszApplicationName);
    }

    hr = pITask->GetComment(&ppwszComment);
    if (FAILED(hr)){
        sprintf(lastError, "Failed to retreive task info, error = 0x%x\n",hr);
    }else{
        taskParams.comments = ppwszComment;
        CoTaskMemFree(ppwszComment);
    }

    hr = pITask->GetCreator(&ppwszCreator);
    if (FAILED(hr)){
        sprintf(lastError, "Failed to retreive task info, error = 0x%x\n",hr);
    }else{
        taskParams.creator = ppwszCreator;
        CoTaskMemFree(ppwszCreator);
    }

    hr = pITask->GetExitCode(&pdwExitCode);
    if (FAILED(hr)){
        sprintf(lastError, "Failed to retreive task info, error = 0x%x\n",hr);
    }else{
        taskParams.exitCode = pdwExitCode;
    }

    hr = pITask->GetIdleWait(&pwIdleMinutes, &pwDeadlineMinutes);
    if (FAILED(hr)){
        sprintf(lastError, "Failed to retreive task info, error = 0x%x\n",hr);
    }else{
        taskParams.deadlineMinutes = pwDeadlineMinutes;
        taskParams.idleMinutes = pwIdleMinutes;
    }

    hr = pITask->GetMaxRunTime(&pdwRunTime);
    if (FAILED(hr)){
        sprintf(lastError, "Failed to retreive task info, error = 0x%x\n",hr);
    }else{
        taskParams.maxRuntime = pdwRunTime;
    }

    hr = pITask->GetMostRecentRunTime(&pstLastRun);
    if (FAILED(hr)){
        sprintf(lastError, "Failed to retreive task info, error = 0x%x\n",hr);
    }else{
        taskParams.lastRun = pstLastRun;
    }

    hr = pITask->GetNextRunTime(&pstNextRun);
    if (FAILED(hr)){
        sprintf(lastError, "Failed to retreive task info, error = 0x%x\n",hr);
    }else{
        taskParams.nextRun = pstNextRun;
    }

    hr = pITask->GetParameters(&lpwszParameters);
    if (FAILED(hr)){
        sprintf(lastError, "Failed to retreive task info, error = 0x%x\n",hr);
    }else{
        taskParams.appParameters = lpwszParameters;
        CoTaskMemFree(lpwszParameters);
    }

    hr = pITask->GetPriority(&pdwPriority);
    if (FAILED(hr)){
        sprintf(lastError, "Failed to retreive task info, error = 0x%x\n",hr);
    }else{
        taskParams.priority = pdwPriority;
    }

    hr = pITask->GetStatus(&phrStatus);
    if (FAILED(hr)){
        sprintf(lastError, "Failed to retreive task info, error = 0x%x\n",hr);
    }else{
        taskParams.status = phrStatus;
    }

    hr = pITask->GetWorkingDirectory(&lpwszWorkDir);
    if (FAILED(hr)){
        sprintf(lastError, "Failed to retreive task info, error = 0x%x\n",hr);
    }else{
        taskParams.workingDirectory = lpwszWorkDir;
        CoTaskMemFree(lpwszWorkDir);
    }

    pITask->Release();
    return 1;

}