package com.yeejoin.amos.fas.business.action.result;

import com.alibaba.fastjson.JSON;
import com.yeejoin.amos.fas.business.util.JSONUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SimpleResult  extends AbstractActionResult  implements ActionResult
{
	List<Map<String, Object>> data = new ArrayList<>();

	@Override
	public JSON toJson()
	{
		List<Map<String, Object>> results = new ArrayList<>();
		for (Map<String, Object> tempMap : data)
		{
			for (Map.Entry<String, Object> entry : tempMap.entrySet())
			{
				Map<String,Object> result = new HashMap<>();
				result.put("label", entry.getKey());
				result.put("value", entry.getValue());
				results.add(result);
			}
		}
		return (JSON) JSON.toJSON(results);
	}

	@Override
	public void addAll(List<Object> datas)
	{
		for (Object o : datas) {
			this.data.add(JSONUtil.toMap(JSONUtil.toJson(o)));
		}
	}

	@Override
	public void add(Object data)
	{
		this.data.add(JSONUtil.toMap(JSONUtil.toJson(data)));
	}
	
	public void add(String key,Object value)
	{
		Map<String, Object> map = new HashMap<>();
		map.put(key, value);
		this.data.add(map);
	}
	@Override
	public List<Map<String, Object>> getData() {
		// TODO Auto-generated method stub
		return data;
	}
}
