package com.jwipc.spe.logic;

import com.google.common.collect.Maps;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorString;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Component
public class GetLevelAvaia extends AbstractFunction {
    /**
     * Get the function name
     */
    @Override
    public String getName() {
        return "getLevelAvaia";
    }

    @Override
    public AviatorObject call(Map<String, Object> env) {
        Map<String, String> STR_LEVEL_RNAGE = Maps.newHashMap();
        STR_LEVEL_RNAGE.put("ZW_PZ_25>=90", "A");
        STR_LEVEL_RNAGE.put("ZW_PZ_25>=80&&ZW_PZ_25<90", "B");
        STR_LEVEL_RNAGE.put("ZW_PZ_25>=70&&ZW_PZ_25<80", "C");
        STR_LEVEL_RNAGE.put("ZW_PZ_25<70", "D");
        String levelStr = "";
        for (String s : STR_LEVEL_RNAGE.keySet()) {
            final Boolean execute = (Boolean) AviatorEvaluator.execute(s, env);
            if (execute) {
                levelStr = STR_LEVEL_RNAGE.get(s);
                break;
            }
        }
        return new AviatorString(levelStr);
    }

    @PostConstruct
    public void addFunction() {
        AviatorEvaluator.addFunction(new GetLevelAvaia());
    }
}