package org.wso2.carbon.device.mgt.iot.common.api;

import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;

import org.wso2.carbon.device.mgt.iot.common.DeviceManagement;
import org.wso2.carbon.device.mgt.iot.common.util.DeviceTypes;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import java.util.ArrayList;
import java.util.List;

public class DevicesManagerService {

	@Path("/devices/username/{username}")
	@GET
	@Consumes("application/json")
	@Produces("application/json")
	public Device[] getDevices(@PathParam("username") String username)
			throws DeviceManagementException {

		DeviceManagement deviceManagement = new DeviceManagement();

		List<Device> devices = deviceManagement.getDeviceManagementService().getDevicesOfUser(
				username);
		List<Device> activeDevices = new ArrayList<>();
		if (devices != null) {
			for (Device device : devices) {
				if (device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.ACTIVE)) {
					activeDevices.add(device);
				}
			}
		}
		return activeDevices.toArray(new Device[]{});
	}

	@Path("/devices/count/{username}")
	@GET
	@Consumes("application/json")
	@Produces("application/json")
	public int getDeviceCount(@PathParam("username") String username)
			throws DeviceManagementException {

		DeviceManagement deviceManagement = new DeviceManagement();

		List<Device> devices = deviceManagement.getDeviceManagementService().getDevicesOfUser(
				username);


		if (devices != null) {
			List<Device> activeDevices = new ArrayList<>();
			for (Device device : devices) {
				if (device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.ACTIVE)) {
					activeDevices.add(device);
				}
			}
			return activeDevices.size();
		}
		return 0;
	}


//	@Path("/devices/types/{type}")
//	@GET
//	@Consumes("application/json")
//	@Produces("application/json")
//	public Device[] getDevicesByType(@PathParam("type") String deviceType)
//			throws DeviceManagementException {
//
//		DeviceManagement deviceManagement = new DeviceManagement();
//
//		List<Device> devices = deviceManagement.getDeviceManagementService().get(deviceType);
//
//		return devices.toArray(new Device[]{});
//	}

	@Path("/devices/types/")
	@GET
	@Consumes("application/json")
	@Produces("application/json")
	public DeviceTypes[] getDeviceTypes()
			throws DeviceManagementDAOException {

		DeviceManagement deviceManagement = new DeviceManagement();

		return deviceManagement.getDeviceTypes();
	}

}

@@ -1,8 +1,695 @@
+/*%0A *  Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.%0A *%0A *  WSO2 Inc. licenses this file to you under the Apache License,%0A *  Version 2.0 (the %22License%22); you may not use this file except%0A *  in compliance with the License.%0A *  You may obtain a copy of the License at%0A *%0A *  http://www.apache.org/licenses/LICENSE-2.0%0A *%0A *  Unless required by applicable law or agreed to in writing,%0A *  software distributed under the License is distributed on an%0A *  %22AS IS%22 BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY%0A *  KIND, either express or implied.  See the License for the%0A *  specific language governing permissions and limitations%0A *  under the License.%0A *%0A */%0A%0A
 package 
@@ -2004,24 +2004,727 @@
 e%5B%5D%7B%7D);%0A%09%7D%0A%0A
+%09@Path(%22/devices/ungrouped/username/%7Busername%7D%22)%0A%09@GET%0A%09@Consumes(%22application/json%22)%0A%09@Produces(%22application/json%22)%0A%09public Device%5B%5D getUnGroupedDevices(@PathParam(%22username%22) String username)%0A%09%09%09throws DeviceManagementException %7B%0A%0A%09%09DeviceManagement deviceManagement = new DeviceManagement();%0A%0A%09%09List%3CDevice%3E devices = deviceManagement.getDeviceManagementService().getUnGroupedDevicesOfUser(%0A%09%09%09%09username);%0A%09%09List%3CDevice%3E activeDevices = new ArrayList%3C%3E();%0A%09%09if (devices != null) %7B%0A%09%09%09for (Device device : devices) %7B%0A%09%09%09%09if (device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.ACTIVE)) %7B%0A%09%09%09%09%09activeDevices.add(device);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return activeDevices.toArray(new Device%5B%5D%7B%7D);%0A%09%7D%0A%0A
 %09@Path(%22/dev

Add method to retrieve un grouped devices