package com.qiniu.qvs;

import com.qiniu.common.QiniuException;
import com.qiniu.qvs.model.Device;
import com.qiniu.qvs.model.PatchOperation;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.qiniu.util.UrlUtils;

public class DeviceManager {

	private final String apiServer;
	private final Client client;
	private final Auth auth;

	public DeviceManager(Auth auth) {
		this(auth, "http://qvs.qiniuapi.com");
	}

	public DeviceManager(Auth auth, String apiServer) {
		this(auth, apiServer, new Client());
	}

	public DeviceManager(Auth auth, String apiServer, Client client) {
		this.auth = auth;
		this.apiServer = apiServer;
		this.client = client;
	}

	public Response createDevice(String namespaceId, Device device) throws QiniuException {
		StringMap params = new StringMap();
		params.put("name", device.getName());
		params.put("gbId", device.getGbId());
		params.putNotNull("username", device.getUsername());
		params.putNotNull("password", device.getPassword());
		params.put("pullIfRegister", device.isPullIfRegister());
		params.put("desc", device.getDesc());

		String url = String.format("%s/v1/namespaces/%s/devices", apiServer, namespaceId);
		return QvsResponse.post(url, params, client, auth);
	}

	/*
	 * 删除设备
	 */
	public Response deleteDevice(String namespaceId, String gbId) throws QiniuException {
		String url = String.format("%s/v1/namespaces/%s/devices/%s", apiServer, namespaceId, gbId);
		return QvsResponse.delete(url, client, auth);
	}

	/*
	 * 更新设备
	 */
	public Response updateDevice(String namespaceId, String gbId, PatchOperation[] patchOperation)
			throws QiniuException {
		StringMap params = new StringMap().put("operations", patchOperation);
		String url = String.format("%s/v1/namespaces/%s/devices/%s", apiServer, namespaceId, gbId);
		return QvsResponse.patch(url, params, client, auth);
	}

	/*
	 * 查询设备
	 */
	public Response queryDevice(String namespaceId, String gbId) throws QiniuException {
		String url = String.format("%s/v1/namespaces/%s/devices/%s", apiServer, namespaceId, gbId);
		return QvsResponse.get(url, client, auth);
	}

	/*
	 * 获取设备列表
	 */
	public Response listDevice(String namespaceId, int offset, int line, String prefix, String state, int qtype)
			throws QiniuException {
		String requestUrl = String.format("%s/v1/namespaces/%s/devices", apiServer, namespaceId);
		StringMap map = new StringMap().put("offset", offset).put("line", line).put("qtype", qtype)
				.put("prefix", prefix).put("state", state);
		requestUrl = UrlUtils.composeUrlWithQueries(requestUrl, map);
		return QvsResponse.get(requestUrl, client, auth);
	}

	/*
	 * 启动设备
	 */
	public Response startDevice(String namespaceId, String gbId) throws QiniuException {
		String url = String.format("%s/v1/namespaces/%s/devices/%s/start", apiServer, namespaceId, gbId);
		return QvsResponse.post(url, new StringMap(), client, auth);
	}

	/*
	 * 停止设备
	 */
	public Response stopDevice(String namespaceId, String gbId) throws QiniuException {
		String url = String.format("%s/v1/namespaces/%s/devices/%s/stop", apiServer, namespaceId, gbId);
		return QvsResponse.post(url, new StringMap(), client, auth);
	}

}

@@ -2680,16 +2680,18 @@
 %09 * %E5%90%AF%E5%8A%A8%E8%AE%BE%E5%A4%87
+%E6%8B%89%E6%B5%81
 %0A%09 */%0A%09p
@@ -2740,32 +2740,49 @@
 eId, String gbId
+, String channels
 ) throws QiniuEx
@@ -2885,32 +2885,103 @@
 aceId, gbId);%0A%09%09
+StringMap params = new StringMap().put(%22channels%22, channels);%0A        %09
 return QvsRespon
@@ -2989,39 +2989,30 @@
 e.post(url, 
-new StringMap()
+params
 , client, au
@@ -3032,16 +3032,18 @@
 %09 * %E5%81%9C%E6%AD%A2%E8%AE%BE%E5%A4%87
+%E6%8B%89%E6%B5%81
 %0A%09 */%0A%09p
@@ -3091,32 +3091,49 @@
 eId, String gbId
+, String channels
 ) throws QiniuEx
@@ -3235,32 +3235,103 @@
 aceId, gbId);%0A%09%09
+StringMap params = new StringMap().put(%22channels%22, channels);%0A        %09
 return QvsRespon
@@ -3343,31 +3343,22 @@
 st(url, 
-new StringMap()
+params
 , client

update start and stop device params