package kz.cafe.client.rest;

import java.io.*;
import java.util.zip.*;
import javax.servlet.http.*;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import kz.cafe.client.*;
import kz.cafe.constants.*;
import kz.cafe.db.logs.*;
import kz.cafe.db.logs.model.*;

@Path("/admin")
public class FEAdmin extends FEAbstractService{
   //private static Log log = LogFactory.getLog(FEAdmin.class);

   private static final String LOG = "[ADMIN] ";





   public FEAdmin(@Context HttpServletRequest hsr){
      super(ServiceRequestModel.buildUpponServlet(hsr), LOG);
   }








   @GET
   @Path("/log/body/{UID}/{date}")
   @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
   public String getUserLog(@PathParam("UID")  long UID,
                            @PathParam("date") long date) throws Exception{
      StringBuilder sb = new StringBuilder();

      sb.append("<response>");

      for(MLogHeader mLogRecord3 : MongoLogService.getUserLogHeader(UID, date)){
         try{
            ByteArrayInputStream bais =
                 new ByteArrayInputStream(MongoLogService.getUserRecord(mLogRecord3._id).response);
            GZIPInputStream gzipIn = new GZIPInputStream(bais);
            ObjectInputStream objectIn = new ObjectInputStream(gzipIn);
            sb.append("" + objectIn.readObject());
            objectIn.close();
         }
         catch(Throwable e){
            throw new Error("Can not UNZIP binary data", e);
         }
      }

      sb.append("</response>");


      return sb.toString();
   }







   @GET
   @Path("/log/headers/{UID}")
   @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
   public String getUserLogHeaders(@PathParam("UID") long UID) throws Exception{
      StringBuilder sb = new StringBuilder();

      sb.append("<log>");

      for(MLogHeader mLogRecord3 : MongoLogService.getUserLogHeaders(UID)){
         sb.append("<request>");
         sb.append("<date>" + LogConstants.formatDate(mLogRecord3.date) + "</date>");
         sb.append("<date>" + mLogRecord3.date + "</date>");
         sb.append("<time>" + mLogRecord3.performance + " ms</time>");
         sb.append("<ip>" + mLogRecord3.IP + "</ip>");
         sb.append("<url>" + mLogRecord3.request_url + "</url>");

         if(mLogRecord3.request_post == null){
            // Well, it was GET
         }
         else{
            sb.append("<post>" + mLogRecord3.request_post + "</post>");
         }

         sb.append("</request>");
      }

      sb.append("</log>");


      return sb.toString();
   }
   
   
   
   
   
   
   
   
   

   
   
   @GET
   @Path("/log/headers")
   @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
   public String getLogHeaders() throws Exception{
      StringBuilder sb = new StringBuilder();

      sb.append("<log>");

      for(MLogHeader mLogRecord3 : MongoLogService.getLogHeaders(1000)){
         sb.append("<request>");
         sb.append("<date>" + LogConstants.formatDate(mLogRecord3.date) + "</date>");
         sb.append("<date>" + mLogRecord3.date + "</date>");
         sb.append("<time>" + mLogRecord3.performance + " ms</time>");
         sb.append("<ip>" + mLogRecord3.IP + "</ip>");
         sb.append("<url>" + mLogRecord3.request_url + "</url>");

         if(mLogRecord3.request_post == null){
            // Well, it was GET
         }
         else{
            sb.append("<post>" + mLogRecord3.request_post + "</post>");
         }

         sb.append("</request>");
      }

      sb.append("</log>");


      return sb.toString();
   }
   
   
   
   
   
   
   
   
   



//   try{
//      ByteArrayInputStream bais = new ByteArrayInputStream(mLogRecord3.response);
//      GZIPInputStream gzipIn = new GZIPInputStream(bais);
//      ObjectInputStream objectIn = new ObjectInputStream(gzipIn);
//      sb.append("" + objectIn.readObject());
//      objectIn.close();
//   }
//   catch (Throwable e){
//      throw new Error("Can not UNZIP binary data");
//   }

}
