package com.etherios.gae.connector.tank;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.digi.connector.client.CloudConnection;
import com.digi.connector.client.models.CloudDevice;
import com.etherios.gae.connector.tank.datastore.UserData;
import com.etherios.gae.connector.tank.models.Device;
import com.google.gson.Gson;

@SuppressWarnings("serial")
public class GetDevicesServlet extends BaseServlet {
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		clearErrorMessage();
		
		ArrayList<Device> devices = new ArrayList<Device>();
		
		UserData user = getUserFromSession(req);
		if (user == null) {
			HashMap<String, String> errorResponse = new HashMap<String, String>();
			errorResponse.put("error", errorMessage);
			errorResponse.put("login", "true");
			
			Gson gson = new Gson();
			resp.getWriter().write(gson.toJson(errorResponse));
			return;
		}
		
		// Create the CloudConnection object with the provided user and password.
		CloudConnection cloudConnection = new CloudConnection(user.getUser(), user.getPassword(), user.getServer());
		// Verify if the account is valid.
		ArrayList<CloudDevice> devicesList = cloudConnection.getDevices(true);
		if (devicesList == null) {
			errorMessage = ERROR_INVALID_USER;
			
			req.setAttribute("error", ERROR_INVALID_USER);
			loginDispatcher.forward(req, resp);
			
			//resp.sendRedirect("/login.jsp?error=" + errorMessage);
			return;
		}
		
		for (CloudDevice device:devicesList) {
			Device myDevice = new Device(device.getID());
			myDevice.setName(device.getDeviceType());
			myDevice.setVendorID(device.getVendorID().getVendorID());
			if (myDevice.getLatitude() == null)
				myDevice.setLatitude("");
			else
				myDevice.setLatitude(device.getLatitude());
			if (myDevice.getLongitude() == null)
				myDevice.setLongitude("");
			else
				myDevice.setLongitude(device.getLongitude());
			
			if (device.isConnected())
				myDevice.setStatus(Device.STATUS_ONLINE);
			else
				myDevice.setStatus(Device.STATUS_OFFLINE);
			
			if (user.getSelectedDevice() != null && 
					user.getSelectedDevice().equals(device.getID()))
				myDevice.setSelected(Device.SELECTED_TRUE);
			
			if (!devices.contains(myDevice))
				devices.add(myDevice);
			else if (devices.contains(myDevice)) {
				devices.remove(myDevice);
				devices.add(myDevice);
			}
		}
		
		Gson gson = new Gson();
		resp.getWriter().write(gson.toJson(devices));
	}
	
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
}
