package cz.hsrs.rest.provider;

import java.sql.SQLException;
import java.util.Date;
import java.util.logging.Logger;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import cz.hsrs.db.util.DateUtil;
import cz.hsrs.db.util.TestUtil;
import cz.hsrs.rest.beans.TestBean;

/**
 * Testing class for RESTful services
 * @author mkepka
 *
 */
@Path("/test/")
public class TestRest {

	public static Logger logger = Logger.getLogger("TestRest");
    /**
     * Empty constructor
     */
    public TestRest(){
        super();
    }
    
    /**
     * Method catches rest/test/testget requests to test the connection to receiver
     * /rest/test/testget?value=<>
     * @param testValue
     * @return
     * @throws Exception
     */
    @Path("/testget")
    @GET
    @Produces("text/plain")
    public String testGet(@QueryParam("value") String testValue) throws Exception{
        if(testValue == null){
            return "empty";
        }
        else{
            //String modif = testValue.toUpperCase();
            Date parseMicro = DateUtil.parseTimestampMicro(testValue);
            Date parse = DateUtil.parseTimestamp(testValue);
            return ""+parseMicro+" : "+parseMicro.getTime()+"\n"+parse+" : "+parse.getTime();
        }
    }
    
    /**
     * /rest/test/testbean1?message=<>
     * @param testValue
     * @param userValue
     * @return
     */
    @Path("/testbean1")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public TestBean testXML(@QueryParam("message") String testValue, @QueryParam("user") String userValue){
        if(testValue == null){
            return null;
        }
        else{
            String modif = testValue.toUpperCase();
            return new TestBean(modif);
        }
    }
    
    /**
     * /rest/test/testbean2?message=<>
     * @param testValue
     * @return
     */
    @Path("/testbean2")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public TestBean testJSON(@QueryParam("message") String testValue){
        if(testValue == null){
            return null;
        }
        else{
            String modif = testValue.toUpperCase();
            return new TestBean(modif);
        }
    }
    
    /**
     * /rest/test/testdb?message=<>
     * /rest/test/testdb?message=pokus
     * @param testValue
     * @return
     * @throws SQLException 
     */
    @Path("/testdb")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public TestBean testDB(@QueryParam("message") String testValue) throws SQLException{
        if(testValue == null){
        	logger.info(testValue);
            return null;
        }
        else{ 
            String response = TestUtil.testQuery("SELECT unit_id FROM public.units LIMIT 1;");
            logger.info(response);
            return new TestBean(response);
        }
    }
}