package com.jay.monitor.data.server.utils;

import com.jay.monitor.data.server.models.request.login.LoginRequest;
import org.junit.Test;
import org.springframework.context.expression.MapAccessor;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.ReflectivePropertyAccessor;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.*;

public class ExpressionUtilsTest {
    private TemplateParserContext templateParserContext = new TemplateParserContext("${", "}");

    @Test
    public void test1() {

        StandardEvaluationContext ctx = new StandardEvaluationContext();

        List<PropertyAccessor> propertyAccessors = new ArrayList<>();
        propertyAccessors.add(new ReflectivePropertyAccessor());
        propertyAccessors.add(new MapAccessor());

        ctx.setPropertyAccessors(propertyAccessors);
        Map<String, Object> variableMap = new HashMap<>();

        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setUsername("lkx");
        loginRequest.setPassword("123456");

        variableMap.put("name", "某某某");
        variableMap.put("sex", "男");
        variableMap.put("like", "jay");

        Map<String, Object> vMap = new HashMap<>();
        vMap.put("obj", variableMap);
        vMap.put("login", loginRequest);
        // 设置
        ctx.setRootObject(vMap);
        //        ctx.setVariables(variableMap);
        //                ctx.setVariable("obj", variableMap);
        //        ctx.setVariable("login", loginRequest);

        ExpressionParser parser = new SpelExpressionParser();
        //        Expression expression = parser.parseExpression("#obj");
        //        printLog(ctx, expression);

        Expression expression =
            parser.parseExpression("${login.username == 'lkx' && login.password == '123456'}", templateParserContext);
        String content = expression.getValue(ctx, String.class);
        System.out.println(content);
    }

    @Test
    public void testExpressionMap() {
//        standExpression("${login.username == 'lkx' && login.password == '123456'}");
//        standExpression("${obj.age == 16}");
//        standExpression("${obj.age < 18}");
        standExpression("${obj.age + 10 * 10}");
    }

    private void standExpression(String expressionStr) {
        StandardEvaluationContext ctx = new StandardEvaluationContext();

        List<PropertyAccessor> propertyAccessors = new ArrayList<>();
        propertyAccessors.add(new ReflectivePropertyAccessor());
        propertyAccessors.add(new MapAccessor());

        ctx.setPropertyAccessors(propertyAccessors);

        Map<String, Object> defaultValue = getDefaultValue();

        ExpressionParser parser = new SpelExpressionParser();
        Expression expression = parser.parseExpression(expressionStr, templateParserContext);
        Object value = expression.getValue(ctx, defaultValue);
        System.out.println(value);
    }

    private Map<String, Object> getDefaultValue() {
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setUsername("lkx");
        loginRequest.setPassword("123456");

        Map<String, Object> variableMap = new HashMap<>();
        variableMap.put("name", "某某某");
        variableMap.put("sex", "男");
        variableMap.put("like", "jay");
        variableMap.put("age", 16);
        variableMap.put("testList", Arrays.asList(10, 20, 30, 40, 50));

        Map<String, Object> vMap = new HashMap<>();
        vMap.put("obj", variableMap);
        vMap.put("login", loginRequest);
        return vMap;
    }

    private void printLog(StandardEvaluationContext ctx, Expression loginExpression) {
        Object value1 = loginExpression.getValue(ctx);
        System.out.println(value1);
    }

}