/* (non-Javadoc)
   * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
   */
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    if (req.getPathInfo().equals("/location")) {
      resp.setContentType("application/neptus+xml");
      PrintWriter page = resp.getWriter();
      LocationType start = MyState.getLocation();
      page.write(start.asXML(ImcMsgManager.getManager().getLocalId() + " Location"));
      page.close();
      return;
    } else if (req.getPathInfo().equals("/console") || req.getPathInfo().equals("/console.png")) {
      if (System.currentTimeMillis() - consoleLastRTime > 10000) {
        File logImages = new File("log/images");
        File[] fileList =
            logImages.listFiles(
                new FileFilter() {
                  @Override
                  public boolean accept(File pathname) {
                    if (pathname.isFile()
                        && "png".equalsIgnoreCase(FileUtil.getFileExtension(pathname))) return true;
                    else return false;
                  }
                });
        Vector<File> fileVec = new Vector<File>();
        for (File f : fileList) {
          fileVec.add(f);
        }
        Collections.sort(fileVec);
        if (fileVec.size() > 0) console = fileVec.lastElement();
        else console = null;
        consoleLastRTime = System.currentTimeMillis();
      }

      if (req.getPathInfo().equals("/console.png")) {
        if (console != null && console.exists() && allowConsoleExposure) {
          resp.setContentType("image/png");
          FileInputStream fxStream = new FileInputStream(console);
          StreamUtil.copyStreamToStream(fxStream, resp.getOutputStream());
          fxStream.close();
        }
        resp.getOutputStream().close();
      } else {
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("<html><head><meta http-equiv='refresh' content='30'></head><body>");
        if (console != null && console.exists() && allowConsoleExposure)
          resp.getWriter()
              .write(
                  "<img src=\"/localstate/console.png\" alt=\""
                      + console.getName()
                      + "\" "
                      + "id=\""
                      + console.getName()
                      + " border=\"0\">");
        else resp.getWriter().write("No consoles active.");
        resp.getWriter().write("</body></html>");
        resp.getWriter().close();
        // <img src="guides.ign.com4_files/header220001101.gif" alt="IGNguides.com" usemap="#HEADER"
        // width="590" border="0" height="90">
      }
    } else if (req.getPathInfo().equals("/")) {
      resp.setContentType("text/html;charset=UTF-8");
      if (!ImcMsgManager.getManager().isRunning()) {
        resp.getWriter()
            .write(
                "<html><head><meta http-equiv='refresh' content='30'>"
                    + "<body>Comms are not running.</body></html>");
        resp.getWriter().close();
        return;
      }

      ImcId16 idImc = ImcMsgManager.getManager().getLocalId();

      String ret = "<html>";

      ret += "<h2>Location</h2><blockquote>";
      LocationType home = MyState.getLocation();
      String homeLoc = home.getLatitudeAsPrettyString() + ", " + home.getLongitudeAsPrettyString();
      // ret += homeLoc+"<br/>";
      ret += "<a href='/localstate/location'>" + homeLoc + "</a><br/>";
      ret += "</blockquote><br>";

      ret += "<h2>State</h2>";
      ret += "<b>ID:</b> " + idImc + "<br>";
      ret +=
          "<b>Services:</b> "
              + ImcMsgManager.getManager().getAllServicesString().replaceAll(";", ";<br>")
              + "<br><br>";

      ret += "<b>Comms info:</b><br>" + ImcMsgManager.getManager().getCommStatusAsHtmlFragment();

      if (System.currentTimeMillis() - ImcMsgManager.getManager().getAnnounceLastArriveTime()
          > DateTimeUtil.MINUTE * 5) {
        ret += "<b color='red'>Announce not arriving for some time</b><br>";
      }

      ret += "</html>";

      resp.getWriter().write(ret);
      resp.getWriter().close();
    }
  }