package org.arkiwi.app.feed;

import java.util.HashMap;
import java.util.Iterator;

public class FeedQuery {
	@SuppressWarnings("unused")
	private final static String LOG_TAG = "FeedQuery";
	private HashMap<String, FeedItem> feeds;
	
	private int qid;        //riempito solo con l'estrazione dal db
	private String refresh; //l'ultimo refresh
    private String last;    //ultimo aggiornamento
    private String title;
    private String link;	
    private String desc;
    private String status;
	
	public FeedQuery() {
		feeds = new HashMap<String, FeedItem>();
		initVar();
	}
	
	public void clear(){
		if (feeds.size() > 0)
			feeds.clear();
		
		initVar();
	}
	
	private void initVar(){
		this.refresh = "";
		this.last = "";
		this.title = "";
		this.link = "";
		this.desc = "";
		this.qid = -1;
		this.status = "";
	}
	/**
	 * Ritorna il feed dell'hashmap FeedQuery che ha chiave "key" 
	 * 
	 * @param String key : è il valore di feeditem.getGuid()
	 */
	public FeedItem getFeedItem(String key){
		return feeds.get(key);
	}
	/**
	 * Ritorna un hashmap con il "feed" 
	 * 
	 * @param FeedItem feed
	 */
	public HashMap<String, FeedItem> getFeedItem(FeedItem feed){
		HashMap<String, FeedItem> feeds = new HashMap<String, FeedItem>();
		feeds.put(feed.getGuid(), feed);
		
		return feeds;
	}
	/**
	 * Ritorna un hashmap con tutti i feed 
	 */
	public HashMap<String, FeedItem> getFeedItems(){
		return feeds;
	}
	/**
	 * Ritorna un hashmap con solo i feed del tipo "type".
	 * 
	 * @param type : R.string.rss_type_*
	 */
	public HashMap<String, FeedItem> getFeedItems(String type){
		HashMap<String, FeedItem> typeFeeds = new HashMap<String, FeedItem>();
		
		Iterator<String> iterator = feeds.keySet().iterator();
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			FeedItem feed = getFeedItem(key);
			if(feed.getMediaType().equalsIgnoreCase(type))
				typeFeeds.put(key, feed);
				
		}
		
		return typeFeeds;
	}
	/**
	 * Ritorna il numero di feed nel hashmap "feeds"
	 * @param HashMap<String, FeedItem> feeds
	 */
	public int getNumFeedItem(HashMap<String, FeedItem> feeds){
		return feeds.size();
	}
	/**
	 * Ritorna il numero di feed nel hashmap feedquery
	 */
	public int getNumFeedItem(){
		return feeds.size();
	}
	
	public FeedItem addFeed(String key, String type){
		FeedItem fi;
		if (type.equalsIgnoreCase("dir"))
			fi = new FeedItemDir();
		else
			fi = new FeedItemFile();
		
		feeds.put(key, fi);
		fi.setGuid(key);
		fi.setMediaType(type);
		
		return fi;
	}
	
	//API: getters and setters
	public String getRefresh() {
		return refresh;
	}

	public void setRefresh(String refresh) {
		this.refresh = refresh;
	}

	public String getLast() {
		return last;
	}

	public void setLast(String last) {
		this.last = last;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}
	
	public int getQid() {
		return qid;
	}

	public void setQid(int qid) {
		this.qid = qid;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
}
