package kz.cafe.client.rest;

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.*;
import kz.cafe.db.model.*;
import kz.cafe.db.service.*;
import kz.cafe.utils.*;


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

   private static final String LOG = "[JR USER] ";

//   private UserSession4 session = null;





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



   
   
   //---------------------------------------------------------------------------------------------
   //add new
   //---------------------------------------------------------------------------------------------
   @GET
   @Path("/system/new/{SID}/{lang}/{header}/{text}")
   @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
   public JREmptyResponse newSystemMessage(@PathParam("SID")    String SID,
                                           @PathParam("lang")   String sLang,
                                           @PathParam("header") String sHeader,
                                           @PathParam("text")   String sText){
      JREmptyResponse response = new JREmptyResponse();
      
      try{
         checkSession(SID);
         //MUser mUser = MongoUserService.getUserProfile(UID);
         
         
         MMessageSystem mMessageSystem = new MMessageSystem();
         mMessageSystem.ID = MongoService.getUniqueID(MMessageSystem.class);
         mMessageSystem.fromUID = super.UID;
         
         for(InterfaceConstants.LANGS lang : InterfaceConstants.LANGS.values()){
            mMessageSystem.header.put(lang, sHeader);
            mMessageSystem.text.put(lang, sText);
         }
         
         MongoService.save(mMessageSystem);
         
         Mailer.sendMail(ServerConstants.getRobot(),
                         ServerConstants.getEmailAddressSystemAdmin(),
                         sHeader,
                         sText);
      }
      catch(Throwable e){
         response = new JREmptyResponse(e);
      }
      
      updateStatistic(MAbstractStatistics.F_FEEDBACK_NEW);
      super.responseTime(response);
      return response;
   }
   


   
   
   
   
   
   
   @GET
   @Path("/cafe/new/{SID}/{CID}/{lang}/{header}/{text}")
   @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
   public JREmptyResponse newFeedbacks(@PathParam("SID")    String SID,
                                       @PathParam("CID")    int CID,
                                       @PathParam("lang")   String sLang,
                                       @PathParam("header") String sHeader,
                                       @PathParam("text")   String sText){
      
      
      JREmptyResponse response = new JREmptyResponse();
      
      try{
         checkSession(SID);
         MUser mUser = MongoUserService.getUserProfile(UID);
         
         MCafe mCafe = MongoService.getExceptionable(MCafe.class, CID);
         
         MMessageCafe mMessageCafe = new MMessageCafe();
         mMessageCafe.ID = MongoService.getUniqueID(MMessageSystem.class);
         mMessageCafe.fromUID = super.UID;
         mMessageCafe.toCID = super.UID;
         
         
         for(InterfaceConstants.LANGS lang : InterfaceConstants.LANGS.values()){
            mMessageCafe.header.put(lang, sHeader);
            mMessageCafe.text.put(lang, sText);
         }
         
         MongoService.save(mMessageCafe);
         
         MUser mCafeOwner = MongoUserService.getUserProfile(mCafe.ownerID);
         
         Mailer.sendMail(mUser.login, mCafeOwner.login, sHeader, sText);
      }
      catch(Throwable e){
         response = new JREmptyResponse(e);
      }
      
      updateStatistic(MAbstractStatistics.F_FEEDBACK_NEW);
      super.responseTime(response);
      return response;
      
   }
}

