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

import java.math.BigDecimal;
import java.util.Stack;

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


/**
 * 소수점이 없는걸 있게 변환하는 함수.
 * 입력 값은 byte[], 결과는 byte[]임.
 */
public class ADDPOINT extends PostfixMathCommand {
    public ADDPOINT() {
        numberOfParameters = 3;
    }

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

        Object p3 = inStack.pop();

        if (p3 instanceof Number) {
            int _p3 = ((Number) p3).intValue();
            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;
                	if ( str1.getBytes().length != _p2 + _p3) {
                		throw new ParseException("Invalid length");
                	}                
                    inStack.push(addPoint(str1,_p2,_p3));
                } else if (p1 instanceof byte[]) {
                    byte[] bytes = (byte[]) p1;
                	if ( bytes.length != _p2 + _p3) {
                		throw new ParseException("Invalid length");
                	}                
                    inStack.push(addPoint(new String(bytes),_p2,_p3));
                } 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 number");
        }
    }
    
    public byte[] addPoint(String str , int plus , int minus) throws ParseException {
    	if (str == null || str.trim().length() ==0 ){
    		return "0".getBytes();
    	}
    	else {
    		str = str.trim();
    	}
    	
    	if(str.length() > (plus+minus) ) {
    		throw new ParseException("data size overflow > "+(plus+minus) +", data="+ str);
    	}
    	//추가로직 juns 20211022
    	if (minus==0) {
    		if (str.getBytes().length == plus){
    			return str.getBytes();
    		}else{
    			return StringUtils.leftPad(str, plus).getBytes();
    		}
    	}
    	byte[] result = new byte[plus + minus + 1]; 
    	System.arraycopy(str.getBytes(), 0, result, 0, plus);
    	System.arraycopy(".".getBytes(), 0, result, plus, 1);
    	System.arraycopy(str.getBytes(), plus, result, plus + 1, minus);
    	return result;
    }
    public static void main(String[] args) throws Exception{
    	ADDPOINT point = new ADDPOINT();
    	// System.out.println(new String(point.addPoint("000000000100",12,0)));
    	try{
    	// System.out.println(Long.valueOf(new String(point.addPoint("000000000100",12,0))));
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	try{
    		// System.out.println(new BigDecimal(new String(point.addPoint("000000000100",12,0))));
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	
    }
}
