package kz.cafe.client.rest;

import java.util.*;
import javax.servlet.http.*;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import kz.cafe.client.*;
import kz.cafe.client.rest.wrappers.*;
import kz.cafe.constants.*;
import kz.cafe.db.*;
import kz.cafe.db.model.*;


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

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

//   private UserSession4 session = null;





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






   
   //---------------------------------------------------------------------------------------------
   //Sales
   //---------------------------------------------------------------------------------------------
   @GET
   @Path("/list/{LANG}/{TIME}")
   @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
   @SuppressWarnings({ "unchecked" })
   public JRSaleListResponse getSales(@PathParam("LANG") String sLang,
                                      @PathParam("TIME") long lTime){
      JRSaleListResponse response = new JRSaleListResponse();
      
      try{
         InterfaceConstants.LANGS lang = validateLang(sLang);
         
         for(MSale mSale : (List<MSale>) MongoService.getListFrontEnd(MSale.class, lTime)){

            if(response.checkDeleted(mSale)){
               JRSaleListResponse.JMItem item = response.add();
               item.fillIn(lang, mSale);
               
               item.CID = mSale.CID;

               item.date_from = mSale.date_from;
               item.date_notification = mSale.date_notification;
               item.date_to = mSale.date_to;
               item.main_screen = mSale.showMainScreen;
               item.news_screen = mSale.showNews;
            }
         }
      }
      catch(Throwable e){
         response = new JRSaleListResponse(e);
      }
      
      updateStatistic(MAbstractStatistics.F_SALES);
      super.responseTime(response);
      return response;
   }
}