package com.atlassian.stash.internal.web.soy.functions;

import com.atlassian.soy.renderer.JsExpression;
import com.atlassian.soy.renderer.SoyClientFunction;
import com.atlassian.soy.renderer.SoyServerFunction;

import java.util.Collections;
import java.util.Date;
import java.util.Set;

import static com.google.common.base.Preconditions.checkArgument;

public class CompareDateFunction implements SoyServerFunction<Integer>, SoyClientFunction {

    private static final Set<Integer> VALID_ARG_SIZES = Collections.singleton(2);

    @Override
    public String getName() {
        return "compare_date";
    }

    @Override
    public Integer apply(Object... args) {
        checkArgument(args[0] instanceof Date || args[0] instanceof Number);
        checkArgument(args[1] instanceof Date || args[1] instanceof Number);

        final Date dateA = args[0] instanceof Date ? (Date) args[0] : new Date(((Number) args[0]).longValue());
        final Date dateB = args[1] instanceof Date ? (Date) args[1] : new Date(((Number) args[1]).longValue());

        return dateA.compareTo(dateB);
    }

    @Override
    public JsExpression generate(JsExpression... args) {
        return new JsExpression(args[0].getText() + " - " + args[1].getText());
    }

    @Override
    public Set<Integer> validArgSizes() {
        return VALID_ARG_SIZES;
    }
    
    
    
}
