package net.northking.cctp.element;

import com.alibaba.fastjson.JSON;
import jdk.nashorn.internal.scripts.JD;
import net.northking.cctp.element.collections.keywords.Collections;
import net.northking.cctp.element.core.IExecuteContext;
import net.northking.cctp.element.core.annotation.Autowired;
import net.northking.cctp.element.test.AbstractLibraryTest;
import net.northking.cctp.element.test.ParamsBuilder;
import net.northking.cctp.element.test.SampleExecuteContext;
import org.testng.annotations.Test;

import java.util.*;

public class CollectionsLibraryTest extends AbstractLibraryTest {

    @Autowired
    protected Collections collections;

    @Test
    public void testList() throws Exception {
        ParamsBuilder builder = new ParamsBuilder();
        List src = new ArrayList<>();
        builder.add("list", src).add("values", "[1,2,\"3\"]");
        library.runKeyword("appendToList", builder.build());
        System.out.println(JSON.toJSONString(src));

        List dest = (List) library.runKeyword("copyList", builder.build());
        dest.remove(0);
        System.out.println(JSON.toJSONString(dest));
        System.out.println(JSON.toJSONString(src));

        builder.add("value", 2);
        int count = (Integer) library.runKeyword("countValuesInList", builder.build());
        System.out.println(count);
    }

    @Test
    public void testMap() throws Exception {
        ParamsBuilder builder = new ParamsBuilder();
        Map src = new LinkedHashMap<>();
        src.put("a", 1);
        src.put("b", 2);
        builder.add("map", src);
        Map dest = (Map) library.runKeyword("copyMap", builder.build());
        dest.remove("a");
        System.out.println(JSON.toJSONString(dest));
        System.out.println(JSON.toJSONString(src));
        builder.add("keys", "[\"b\"]");
        Object result = library.runKeyword("keepInMap", builder.build());
        System.out.println(JSON.toJSONString(result));
    }

    @Test
    public void tes(){
        IExecuteContext build = new SampleExecuteContext.Builder().build();
        System.out.println(collections.countValuesInList(Arrays.asList("3","3",3),3,"1","#{aaa}",build));
    }


}