package com.eactive.eai.transformer.function.generic;

import java.util.Stack;

import org.apache.commons.lang3.StringUtils;
import org.nfunk.jep.ParseException;
import org.nfunk.jep.function.PostfixMathCommand;


public class LeftPad extends PostfixMathCommand {
    public LeftPad() {
        numberOfParameters = 3;
    }

    public void run(Stack inStack) throws ParseException {
        checkStack(inStack);

        Object p3 = inStack.pop();

        if (p3 instanceof String) {
            String str3 = (String) p3;
            Object p2 = inStack.pop();

            if (p2 instanceof Number) {
                int _p2 = ((Number) p2).intValue();
                Object p1 = inStack.pop();

                if (p1 instanceof String) {
                    String str1 = (String) p1;
                    inStack.push(StringUtils.leftPad(str1, _p2, str3));
                } else if (p1 instanceof byte[]) {
                    byte[] bytes = (byte[]) p1;
                    inStack.push(StringUtils.leftPad(new String(bytes), _p2, str3));
                } else {
                    throw new ParseException("Invalid parameter type, p1 is not string/bytes");
                }
            } else {
                throw new ParseException("Invalid parameter type, p2 is not number");
            }
        } else {
            throw new ParseException("Invalid parameter type, p3 is not string");
        }
    }
}
