<html>
<%@page import="java.io.File"%>
<%@page import="java.io.IOException"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="javax.xml.bind.DatatypeConverter"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page import="com.eg.EgInstallInfo"%>
<%@page import="com.egurkha.UserRegistry"%>
<%@page import="com.egurkha.User"%>
<%@page import="com.eg.supermanager.*"%>
<%@page import="java.util.*"%>
<%@page import="com.eg.*"%>
<%@page import="com.egurkha.*"%>
<%@page import="com.egurkha.util.*"%>

<head>
</head>
<body>
<%
	try
	{
		String imagePath=EgInstallInfo.getInstallDir()+"//manager//SEUM//screenshots//";
		String imageID = request.getParameter("imageID");
		if(imageID != null && imageID.trim().length()>0 && !imageID.equalsIgnoreCase("NULL"))
		{
			if(!EgInstallInfo.isSuperManager)
			{
				imagePath = imagePath+"//"+imageID;	
				BufferedImage bImage = ImageIO.read(new File(imagePath));
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				ImageIO.write( bImage, "jpg", baos );
				baos.flush();
				byte[] imageInByteArray = baos.toByteArray();
				baos.close();                                   
				String b64 = DatatypeConverter.printBase64Binary(imageInByteArray);
%>
				<img  class="img-responsive" src="data:image/jpg;base64, <%=b64%>"/>
<%
			}
			else
			{
				String mgrId = request.getParameter("managerId");
				byte imageInByteArray[]=null;
				Hashtable mgrThreadTable = new Hashtable();
				Hashtable reportingManagers = (Hashtable) EgManagersInfo.getInstance().getReportingManagers();
				if(reportingManagers != null && reportingManagers.containsKey(mgrId))
				{
					long timeout = EgSmSuperInfo.getInstance().getSmTimeout();
					Hashtable dataToMgr = new Hashtable();
					dataToMgr.put("operation","getScreenShot");
					dataToMgr.put("imageID",imageID);
					String targetFile = "/final/servlet/com.eg.supermanager.CommonMonitorServlet?operation=getScreenShot&compressedData=true";
					EgManager egManager=(EgManager)reportingManagers.get(mgrId);
					if(egManager != null)
					{
						ConnectionHelperThread connHelp=new ConnectionHelperThread(egManager,targetFile,2,null,true,dataToMgr,timeout);
						mgrThreadTable.put(mgrId,connHelp);
					}
				}
				Enumeration enmMgr = mgrThreadTable.keys();
				while(enmMgr.hasMoreElements())
				{
					String s_mgrId = (String)enmMgr.nextElement();
					ConnectionHelperThread connHelp = (ConnectionHelperThread) mgrThreadTable.get(s_mgrId);
					if(connHelp != null)
					{
						ConnectionUtils.waitUntilTimeout(connHelp);
						SmConnectionThread sct =(SmConnectionThread) connHelp.ct;
						if(sct != null && sct.done)
						{
							if(sct.data != null && sct.data.size() > 0)
							{
								Object obj = sct.data.get(0);
								if(obj instanceof Hashtable)
								{
									Hashtable tmpht = (Hashtable)obj;
									if(tmpht != null && tmpht.containsKey("imageInByteArray"))
									{
										imageInByteArray=(byte[])tmpht.get("imageInByteArray");
									}
								}
							}
						}
					}
				}
				if(imageInByteArray != null)
				{
					String b64 = DatatypeConverter.printBase64Binary(imageInByteArray);
%>
					<img  class="img-responsive" src="data:image/jpg;base64, <%=b64%>"/>                            
<%
				}
			}
		}
	}
	catch(IOException e)
	{
		try
		{
			String imagePath=EgInstallInfo.getInstallDir()+"//manager//tomcat//webapps//final//images//Light//noscreenshot.png";
			UserRegistry registry = UserRegistry.getInstance();
			User user = registry.getUser(session.getId());
			String mon_theme = "";
			if(user != null)	
				mon_theme = user.getMonitorSkin();

			if(mon_theme.equalsIgnoreCase("Dark"))
				imagePath=EgInstallInfo.getInstallDir()+"//manager//tomcat//webapps//final//images//Dark//noscreenshot.png";

			BufferedImage bImage = ImageIO.read(new File(imagePath));
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ImageIO.write( bImage, "png", baos );
			baos.flush();
			byte[] imageInByteArray = baos.toByteArray();
			baos.close();                                   
			String b64 = DatatypeConverter.printBase64Binary(imageInByteArray);
			%>
			<div style="display:flex;width:100%;height:100%;">
				<div style="margin:auto auto auto auto;width:210px;height:105px;">
					<img class="img-responsive" src="data:image/png;base64, <%=b64%>"/>
				</div>
			</div>
		<%
		}
		catch (Exception er)
		{
			er.printStackTrace();
		}
	}
%>

</body>
</html>