package org.commcare.engine.extensions;

import android.content.Context;

import org.commcare.dalvik.R;
import org.commcare.utils.EthiopianDateHelper;
import org.commcare.utils.NepaliDateUtilities;
import org.javarosa.core.model.condition.EvaluationContext;
import org.javarosa.core.model.condition.IFunctionHandler;
import org.javarosa.xpath.XPathUnsupportedException;
import org.javarosa.xpath.expr.XPathFuncExpr;

import java.util.Date;
import java.util.Vector;

/**
 * @author ctsims
 */
public class CalendaredDateFormatHandler implements IFunctionHandler {

    private final Context context;

    public CalendaredDateFormatHandler(Context context) {
        this.context = context;
    }

    @Override
    public String getName() {
        return "format-date-for-calendar";
    }

    @Override
    public Vector getPrototypes() {
        Vector v = new Vector();
        v.add(new Class[]{Date.class, String.class});
        return v;
    }

    @Override
    public boolean rawArgs() {
        return false;
    }

    @Override
    public Object eval(Object[] args, EvaluationContext ec) {
        if ("".equals(args[0])) {
            return "";
        }
        Date d = (Date)XPathFuncExpr.toDate(args[0]);
        String calendar = (String)args[1];
        if ("ethiopian".equals(calendar)) {
            return EthiopianDateHelper.ConvertToEthiopian(context, d);
        } else if ("nepali".equals(calendar)) {
            return NepaliDateUtilities.convertToNepaliString(
                    context.getResources().getStringArray(R.array.nepali_months), d);
        } else {
            throw new XPathUnsupportedException("Unsupported calendar type: " + calendar);
        }
    }
}

Revert "remove now uneeded calendar format handler"
Revert "remove now uneeded calendar format handler"

This reverts commit 586c5f9bb5495aa709a9c0adca5e17bb941f5ee2.
