package com.cesgroup.cesmonitor.service.weixin;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 微信模板消息
 *
 * @author zhang.erwei
 * @version 1.0.0 2017年5月16日
 */
public class TemplateMessage {

    /**
     * 微信模板消息参数值
     *
     * @author zhang.erwei
     * @version 1.0.0 2017年5月16日
     */
    public class Value {

        private String value;

        private String color;

        public Value() {

        }

        /**
         * 根据参数值创建参数
         *
         * @param value
         *            参数值
         */
        public Value(final String value) {
            this.value = value;
            this.color = "#173177";
        }

        public String getValue() {
            return value;
        }

        public void setValue(final String value) {
            this.value = value;
        }

        public String getColor() {
            return color;
        }

        public void setColor(final String color) {
            this.color = color;
        }

    }

    private String touser;

    private String template_id;

    private String topcolor = "#FF0000";

    private Map<String, Value> data = new HashMap<String, Value>();

    public String getTouser() {
        return touser;
    }

    public void setTouser(final String touser) {
        this.touser = touser;
    }

    public String getTemplate_id() {
        return template_id;
    }

    public void setTemplate_id(final String template_id) {
        this.template_id = template_id;
    }

    public String getTopcolor() {
        return topcolor;
    }

    public void setTopcolor(final String topcolor) {
        this.topcolor = topcolor;
    }

    public Map<String, Value> getData() {
        return data;
    }

    public void setData(final Map<String, Value> data) {
        this.data = data;
    }

    public void addValue(final String key, final Value v) {
        this.data.put(key, v);
    }

    public void addValue(final String key, final String v) {
        this.data.put(key, new Value(v));
    }

    /**
     * 转模板消息为json格式
     *
     * @return 转模板消息json字符串
     */
    public String toJson() {
        String json = null;
        try {
            //            Map<String, Object> map = new HashMap<String, Object>();
            //            map.put("touser", this.getTouser());
            //            map.put("template_id", this.getTemplate_id());
            //            map.put("topcolor", this.getTopcolor());
            //            Map<String, Object> d = new HashMap<String, Object>();
            //            List<Value> articles = new ArrayList<Value>();
            //            for (Article article : this.getData()) {
            //                Map<String, Object> a = new HashMap<String, Object>();
            //                a.put("title", article.getTitle());
            //                a.put("description", article.getDescription());
            //                a.put("url", article.getUrl());
            //                a.put("picurl", article.getPicUrl());
            //                articles.add(a);
            //            }
            //            map.put("articles", articles);
            json = new ObjectMapper().writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;

    }
}
