///////////////////////////////////////////////////////////////////////////////
//
// CAmbientLightAwareSensorManagerEvents::RemoveSensor
//
// Description of function/method:
//        Helper function, clears the event sink for the sensor and then
//        releases the sensor.
//
// Parameters:
//        REFSENSOR_ID sensorID: Input sensor
//
// Return Values:
//        S_OK on success, else an error
//
///////////////////////////////////////////////////////////////////////////////
HRESULT CAmbientLightAwareSensorManagerEvents::RemoveSensor(REFSENSOR_ID sensorID)
{
    HRESULT hr = S_OK;

    if (m_Sensors.Lookup(sensorID))
    {
        ISensor* pSensor = m_Sensors[sensorID];
        m_Sensors.RemoveKey(sensorID);
        pSensor->Release();
    }
    else
    {
        hr = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
    }

    return hr;
}
///////////////////////////////////////////////////////////////////////////////
//
// CSensorManagerEvents::Initialize
//
// Description of function/method:
//        Initialize the sensor data.
//
// Parameters:
//        none
//
// Return Values:
//        HRESULT S_OK on success
//
///////////////////////////////////////////////////////////////////////////////
HRESULT CSensorManagerEvents::Initialize(int NumSensorTypes, ...)
{
    HRESULT hr;
	va_list vl;
	SENSOR_TYPE_ID TypeID;


	va_start( vl, NumSensorTypes );

	// Step through the list.
	  for ( int x = 0; x < NumSensorTypes; x++ )        // Loop until all numbers are added
	  {
		TypeID = va_arg( vl, SENSOR_TYPE_ID );
		AddRequestedSensor(TypeID);
	}
   va_end( vl );




	hr = ::CoCreateInstance(CLSID_SensorManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&m_spISensorManager));
	if (FAILED(hr))
	{
		OutputDebugString(L"Unable to CoCreateInstance() the SensorManager.");
		return E_FAIL;
	}

    if (SUCCEEDED(hr))
    {
        hr = m_spISensorManager->SetEventSink(this);
        if (SUCCEEDED(hr))
        {
            // Find all Ambient Light Sensors
            ISensorCollection* spSensors;
            hr = m_spISensorManager->GetSensorsByCategory(SENSOR_CATEGORY_ALL, &spSensors);
			if(FAILED(hr))
			{
				OutputDebugString(L"Unable to find any sensors on the computer.");
				return E_FAIL;
			}
            if (SUCCEEDED(hr) && NULL != spSensors)
            {
                ULONG ulCount = 0;
                hr = spSensors->GetCount(&ulCount);
                if (SUCCEEDED(hr))
                {


					// Make a SensorCollection with only the sensors we want to get permission to access.
					ISensorCollection *pSensorCollection = NULL;
					HRESULT hr = ::CoCreateInstance(CLSID_SensorCollection, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pSensorCollection));
					if (FAILED(hr))
					{
						OutputDebugString(L"Unable to CoCreateInstance() a SensorCollection.");
						return E_FAIL;
					}
                    for(ULONG i=0; i < ulCount; i++)
                    {
                        ISensor* spSensor;
                        hr = spSensors->GetAt(i, &spSensor);
						SENSOR_TYPE_ID idType = GUID_NULL;
						hr = spSensor->GetType(&idType);

						std::list<SensorRequest*> ::iterator it;
						for (it=RequestedSensors.begin(); it!=RequestedSensors.end() ; ++it)
						{
							if((*it)->m_Type == idType)
								break;
						}
						if(it==RequestedSensors.end())
						{
							// we have never requested this sensor;
							continue;
						}

						pSensorCollection->Clear();
						pSensorCollection->Add(spSensor);
						// Have the SensorManager prompt the end-user for permission.
						hr = m_spISensorManager->RequestPermissions(NULL, pSensorCollection, TRUE);
						if (FAILED(hr))
						{
							Setstatus(GUID_NULL, SENSOR_STATUS_DISABLED);
							OutputDebugString(L"No permission to access Requested Sensor.");
						}
						spSensor->Release();
					}
					pSensorCollection->Release();

                    for(ULONG i=0; i < ulCount; i++)
                    {
                        ISensor* spSensor;
                        hr = spSensors->GetAt(i, &spSensor);
                        if (SUCCEEDED(hr))
                        {
							SensorState state = SENSOR_STATE_ERROR;
							hr = spSensor->GetState(&state);
							OnSensorEnter(spSensor,state);
							spSensor->Release();
                        }
                    }
                }
            }
			spSensors->Release();
        }
    }
    return hr;
}