/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package testmeetup;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Abstract class of Query. Because every Query has different parameters, each Query has a specific class, but share some things in common.
 * @author Oriol
 */
public abstract class QueryObject {
    
    //My Personal API Meetup key
    public final String API_KEY = "241c21844546f7a69469246542704a"; 
    
    public enum Ordering {
        DISTANCE_UP,DISTANCE_DOWN,TIME_UP,TIME_DOWN,TRENDING_UP,TRENDING_DOWN
    }
    private String nResults; //MAX: 10.000
    private Ordering order;
    private String offset;
    private String URL;
    
    /**
     * Shared Parameters
     * @param nResults
     * @param order
     * @param offset 
     */
    public QueryObject (String nResults, Ordering order, String offset){
        this.nResults = nResults;
        this.order = order;
        this.offset = offset;
    }
    
    
    public QueryObject(){
        
    }
    
    
    public void generateQuery() throws Exception{      
        addParam("sign","true");
        addParam("key",API_KEY);
        addParam("photo-host","public");
        addParam("limited_events","false");
        addParam("text_format","plain");
        addParam("format","json");
        addParam("order","time"); //Hard coded
    }
    
    /**
     * Adds a parameter to the URL variable. To do it, it encodes the parameter in UTF-8 and adds "&" and "=".
     * @param paramName
     * @param paramValue 
     */
    public void addParam(String paramName, String paramValue){
        if(paramValue != null){
            try {
                String encodedUrl = URLEncoder.encode(paramValue, "UTF-8"); // Can be safely ignored because UTF-8 is always supported
                URL += "&" + paramName + "=" + encodedUrl;

            } catch (UnsupportedEncodingException ex) {
                // Can be safely ignored because UTF-8 is always supported
            }
        }
    }

        
    /**
     * Method that converts DateFormat to timestamp. Used in time parameters.
     * @param humanTime Time in format dd/MM/yyyy
     * @return Timestamp
     */
    private String convertTimeFormat(String humanTime){
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date date = null;
        try {
            date = df.parse(humanTime);
        } catch (ParseException ex) {
            Logger.getLogger(MeetupDataManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        long epoch = date.getTime();
        return Long.toString(epoch);
    }
    
    /**
     * Method that takes 2 Dates and returns the parameter time as timestamp,timestamp.
     * @param humanTimeStart Date start
     * @param humanTimeEnd Date end
     * @return Timestamp,Timestamp
     */
    private String convertRangeFormat(String humanTimeStart,String humanTimeEnd){
        String epochStart = convertTimeFormat(humanTimeStart);
        String epochEnd = convertTimeFormat(humanTimeEnd);
        return epochStart + "," + epochEnd;
    }
    
    //GETTERS AND SETTERS

    public String getnResults() {
        return nResults;
    }

    public void setnResults(String nResults) {
        this.nResults = nResults;
    }

    public Ordering getOrder() {
        return order;
    }

    public void setOrder(Ordering order) {
        this.order = order;
    }

    public String getOffset() {
        return offset;
    }

    public void setOffset(String offset) {
        this.offset = offset;
    }

    public String getURL() {
        return URL;
    }

    public void setURL(String URL) {
        this.URL = URL;
    }
    
    
    
}
