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

import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.iot.common.DeviceManagement;
import org.wso2.carbon.device.mgt.iot.common.controlqueue.mqtt.MqttConfig;
import org.wso2.carbon.device.mgt.iot.common.controlqueue.xmpp.XmppConfig;
import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.device.mgt.iot.common.config.server.DeviceCloudConfigManager;

import java.io.File;
import java.util.HashMap;
import java.util.Map;


public class ZipUtil {

	public ZipArchive downloadSketch(String owner, String deviceType, String deviceId, String
			token, String refreshToken) throws DeviceManagementException {

		if (owner == null || deviceType == null) {
			throw new DeviceManagementException("Invalid parameters for `owner` or `deviceType`");
		}

		String sep = File.separator;
		String sketchFolder = "repository" + sep + "resources" + sep + "sketches";
		String archivesPath = CarbonUtils.getCarbonHome() + sep + sketchFolder + sep + "archives"
				+ sep + deviceId;
		String templateSketchPath = sketchFolder + sep + deviceType;

		Map<String, String> contextParams = new HashMap<String, String>();
		contextParams.put("DEVICE_OWNER", owner);
		contextParams.put("DEVICE_ID", deviceId);


		//TODO: Changes needs to be made to the device-config.xml
//		String serverIP =
//				DeviceCloudConfigManager.getInstance().getDeviceCloudMgtConfig().getApiManager()
//						.getServerURL();
//
//		int indexOfChar = serverIP.lastIndexOf(File.separator);
//		if (indexOfChar != -1) {
//			serverIP = serverIP.substring((indexOfChar + 1), serverIP.length());
//		}
//
//		String serverPort =
//				DeviceCloudConfigManager.getInstance().getDeviceCloudMgtConfig().getApiManager()
//						.getGatewayPort();
//
//		String serverEndPoint = serverIP + ":" + serverPort;
//		contextParams.put("SERVER_EP", serverEndPoint);

		String endpoint = MqttConfig.getInstance().getMqttQueueEndpoint();
		int indexOfChar = endpoint.lastIndexOf(File.separator);
		if (indexOfChar != -1) {
			endpoint = endpoint.substring((indexOfChar + 1), endpoint.length());
		}
		contextParams.put("MQTT_EP", endpoint);

		endpoint = XmppConfig.getInstance().getXmppEndpoint();
		indexOfChar = endpoint.lastIndexOf(File.separator);
		if (indexOfChar != -1) {
			endpoint = endpoint.substring((indexOfChar + 1), endpoint.length());
		}
		contextParams.put("XMPP_EP", endpoint);

		contextParams.put("DEVICE_TOKEN", token);
		contextParams.put("DEVICE_REFRESH_TOKEN", refreshToken);

		DeviceManagement deviceManagement = new DeviceManagement();
		ZipArchive zipFile = deviceManagement.getSketchArchive(archivesPath, templateSketchPath,
															   contextParams);

		return zipFile;
	}
}

@@ -1297,70 +1297,9 @@
 ;%0A%0A%0A
-%09%09//TODO: Changes needs to be made to the device-config.xml%0A//
+%0A
 %09%09St
@@ -1314,18 +1314,16 @@
 verIP =%0A
-//
 %09%09%09%09Devi
@@ -1391,34 +1391,32 @@
 getApiManager()%0A
-//
 %09%09%09%09%09%09.getServer
@@ -1422,21 +1422,17 @@
 rURL();%0A
-//%0A//
+%0A
 %09%09int in
@@ -1481,18 +1481,16 @@
 rator);%0A
-//
 %09%09if (in
@@ -1508,18 +1508,16 @@
 = -1) %7B%0A
-//
 %09%09%09serve
@@ -1584,19 +1584,13 @@
 ));%0A
-//
 %09%09%7D%0A
-//%0A//
+%0A
 %09%09St
@@ -1607,18 +1607,16 @@
 rPort =%0A
-//
 %09%09%09%09Devi
@@ -1692,18 +1692,16 @@
 nager()%0A
-//
 %09%09%09%09%09%09.g
@@ -1721,13 +1721,9 @@
 ();%0A
-//%0A//
+%0A
 %09%09St
@@ -1777,10 +1777,8 @@
 rt;%0A
-//
 %09%09co
@@ -1893,22 +1893,18 @@
 oint();%0A
+
 %09%09
-int 
 indexOfC

update the server Ip config read