/*
 * 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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import testmeetup.QueryObject.Ordering;

/**
 * Following a builder patterm, this class is designed to "clear code" when creating an object of the class QueryEvent. Instead of having a constructor with a lot of 
 * parameters in an arbitrary order, or using setters or getters, this class has all the parameters of QueryEvent to make the main class more readable.
 * @author Oriol
 */
public class QueryEventBuilder {
    
    //QueryObject Attributes
    private String _nResults = "100";
    private QueryObject.Ordering _order = Ordering.TIME_DOWN;
    private String _offset;
    
    //QueryEvent Attributes
    private String _eventID = null;
    private String _time = null;
    private String _category = null;
    private String _radius = null;
    private String _country;
    private String _state;
    private String _city;
    private String _status;

    
    public QueryEventBuilder(){}
    
    /**
     * Method that creates a new instance of QueryEvent following a basic constructor.
     * @return Object QueryEvent created.
     */
    public QueryEvent buildQuery(){
        return new QueryEvent(_nResults, _order, _offset, _country, _state, _city, _category, _time, _radius, _status);
    }

    //Special setters and getters that return the same object, so they can be chained together, as it can be seen in the main class.
    public QueryEventBuilder nResults(String _nResults) {
        this._nResults = _nResults;
        return this;
    }

    public QueryEventBuilder order(QueryObject.Ordering _order) {
        this._order = _order;
        return this;
    }

    public QueryEventBuilder offset(String _offset) {
        this._offset = _offset;
        return this;
    }

    public QueryEventBuilder eventID(String _eventID) {
        this._eventID = _eventID;
        return this;
    }

    public QueryEventBuilder time(String _time) {
        this._time = convertTimeFormat(_time);
        return this;
    }
    
    public QueryEventBuilder convertedTime(String _time) {
        this._time = _time;
        return this;
    }
    
    public QueryEventBuilder time(String _timeS,String _timeE) {
        this._time = convertRangeFormat(_timeS,_timeE);
        return this;
    }

    public QueryEventBuilder category(String _category) {
        this._category = _category;
        return this;
    }

    public QueryEventBuilder radius(String _radius) {
        this._radius = _radius;
        return this;
    }

    public QueryEventBuilder country(String _country) {
        this._country = _country;
        return this;
    }

    public QueryEventBuilder state(String _state) {
        this._state = _state;
        return this;
    }

    public QueryEventBuilder city(String _city) {
        this._city = _city;
        return this;
    }
    
    public QueryEventBuilder status(String _status) {
        this._status = _status;
        return this;
    }
    
    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);
    }
    
    private String convertRangeFormat(String humanTimeStart,String humanTimeEnd){
        String epochStart = convertTimeFormat(humanTimeStart);
        String epochEnd = convertTimeFormat(humanTimeEnd);
        return epochStart + "," + epochEnd;
    }
    

    
}
