/*
 * The MIT License
 *
 * Copyright 2015 Lukas Plechinger.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package at.plechinger.scrapeql.query.functions;

import at.plechinger.scrapeql.query.QueryContext;
import at.plechinger.scrapeql.query.expression.RootAwareVariable;
import at.plechinger.scrapeql.query.expression.StringVariable;
import at.plechinger.scrapeql.query.expression.Variable;
import com.google.common.base.Preconditions;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.jsoup.nodes.Element;

/**
 *
 * @author Lukas Plechinger
 */
class DateFunction implements FunctionDefinition {

    private static final String NAME = "date";

    private static final String BASE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    @Override
    public Variable execute(QueryContext context, List<Variable> parameters, Element baseElement) {
        //parameter count must be 2 or 3
        Preconditions.checkArgument(parameters.size() >= 2 && parameters.size() <= 3, "date(): argument count: %d", parameters.size());
        Preconditions.checkArgument(parameters.get(1) instanceof StringVariable, "Second parameter must be a string.");
        
        
        try {

            //parse input
            Variable param0=parameters.get(0);
            //execute input, because is could be a selector
            if(param0 instanceof RootAwareVariable){
                ((RootAwareVariable) param0).setRoot(baseElement);
            }
            
            param0.execute(context);
            
            
            Variable param1=parameters.get(1);
            
            
            SimpleDateFormat input = new SimpleDateFormat(param1.getValue());

            Date date = input.parse(param0.getValue());

            //parse output
            SimpleDateFormat outputFormat;

            if (parameters.size() == 3) {
                Preconditions.checkArgument(parameters.get(2) instanceof StringVariable, "Third parameter must be a string.");
                Variable param2=parameters.get(3);
                outputFormat = new SimpleDateFormat(param2.getValue());
            } else {
                outputFormat = new SimpleDateFormat(BASE_FORMAT);
            }

            return new StringVariable(outputFormat.format(date));
        } catch (ParseException ex) {
            return new StringVariable(parameters.get(0).getValue());
        }
    }

    @Override
    public String getName() {
        return NAME;
    }

}

@@ -2031,39 +2031,13 @@
 ze()
- %3E= 2 && parameters.size()
  %3C= 
-3
+2
 , %22d
@@ -2088,199 +2088,35 @@
 ));%0A
-        Preconditions.checkArgument(parameters.get(1) instanceof StringVariable, %22Second parameter must be a string.%22);%0A        %0A        %0A        try %7B%0A%0A            //parse input%0A
+%0A        try %7B%0A     
        
-     
 Vari
@@ -2124,15 +2124,19 @@
 ble 
-param0=
+toFormat = 
 para
@@ -2154,67 +2154,8 @@
 0);%0A
-            //execute input, because is could be a selector
 %0A   
@@ -2165,23 +2165,26 @@
       if
-(param0
+ (toFormat
  instanc
@@ -2205,16 +2205,17 @@
 ariable)
+ 
 %7B%0A      
@@ -2245,22 +2245,24 @@
 riable) 
-param0
+toFormat
 ).setRoo
@@ -2291,28 +2291,16 @@
       %7D%0A
-            
 %0A       
@@ -2304,22 +2304,24 @@
         
-param0
+toFormat
 .execute
@@ -2335,313 +2335,84 @@
 t);%0A
-            %0A            %0A            Variable param1=parameters.get(1);%0A            %0A            %0A            SimpleDateFormat input = new SimpleDateFormat(param1.getValue());%0A%0A            Date date = input.parse(param0.getValue());%0A%0A            //parse output%0A            SimpleDateFormat outputFormat;%0A
+%0A            SimpleDateFormat format;%0A            //is format parameter set?
 %0A   
@@ -2445,17 +2445,17 @@
 ze() == 
-3
+2
 ) %7B%0A    
@@ -2509,17 +2509,17 @@
 ers.get(
-2
+1
 ) instan
@@ -2544,12 +2544,13 @@
 e, %22
-Thir
+Secon
 d pa
@@ -2612,10 +2612,12 @@
 aram
-2=
+1 = 
 para
@@ -2627,17 +2627,17 @@
 ers.get(
-3
+1
 );%0A     
@@ -2642,32 +2642,25 @@
             
- outputF
+f
 ormat = new 
@@ -2681,17 +2681,17 @@
 at(param
-2
+1
 .getValu
@@ -2713,15 +2713,42 @@
    %7D
- 
 else
- 
 %7B
+ //no format set, base format
 %0A   
@@ -2764,23 +2764,15 @@
     
-outputF
+f
 ormat
- = 
+=
 new 
@@ -2808,32 +2808,119 @@
 ;%0A            %7D%0A
+            %0A            Date formatted=format.parse(toFormat.getValue());%0A            
 %0A            ret
@@ -2946,35 +2946,45 @@
 ble(
-outputFormat.format(date));
+Long.toString(formatted.getTime()));%0A
 %0A   

improve datefunction: now it returns time in miliseconds