package com.wechat.callback.passiveresponses;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import com.wechat.callback.passiveresponses.BasePassiveResponse.MsgTypeEnum;

import java.util.LinkedList;
import java.util.List;

@XmlRootElement(name = "xml")
public final class NewsResponse extends BasePassiveResponse {

    private static final class PictureArticle {
        private String title;
        private String desc;
        private String picurl;
        private String url;

        public PictureArticle(String title, String desc, String picurl, String url) {
            super();
            this.title = title;
            this.desc = desc;
            this.picurl = picurl;
            this.url = url;
        }

        @XmlElement(name = "Title")
        public String getTitle() {
            return title;
        }

        @XmlElement(name = "Description")
        public String getDesc() {
            return desc;
        }

        @XmlElement(name = "PicUrl")
        public String getPicurl() {
            return picurl;
        }

        @XmlElement(name = "Url")
        public String getUrl() {
            return url;
        }
    }

    private List<PictureArticle> articles = null;

    public NewsResponse() {
        setMsgType(MsgTypeEnum.NEWS);
        articles = new LinkedList<>();
    }   
    
    public NewsResponse appendArticle(String title, String desc, String picurl, String url) {
        articles.add(new PictureArticle(title, desc, picurl, url));
        return this;
    }

    @XmlElementWrapper(name = "Articles")
    @XmlElement(name = "item")
    public List<PictureArticle> getArticles() {
        return articles;
    }

    @XmlElement(name = "ArticleCount")
    public Integer getArticleCount() {
        return articles.size();
    }
}
