package org.arkiwi.app.rss;

import java.util.ArrayList;
import java.util.HashMap;

import org.arkiwi.app.interfaces.RssInterface;

import android.util.Log;

public class RssDoc implements RssInterface {
	private final static String LOG_TAG = "RssDoc";
	private static RssDoc instance = null;
	private static int n_back_press;

	//Cache di documenti e array di chiavi che li ordina
	private static HashMap<String, String> docsCached;
	private static ArrayList<String> keyCachedOrder;
	private int max_cache;  
	//Stack di chiavi
	private static ArrayList<String> tree;
	
	private RssDoc(int n) {
		this.max_cache = n; 
		docsCached = new HashMap<String, String>();
		keyCachedOrder = new ArrayList<String>();
		tree = new ArrayList<String>();
	}
	
	public static RssDoc getInstance(int max_cache) {
		if (instance == null) {
			instance = new RssDoc(max_cache);
		} 
		
		return instance;
	}
	
	public String getDocFromCache(String toSearch){
		String xml="";
		if (isCached(toSearch))
			xml=docsCached.get(toSearch);
		
		return xml;
	}
	
	public void addKeyCacheOrder(String query){
		keyCachedOrder.add(query);
	}
	
	public void removeKeyCacheOrder(){
		keyCachedOrder.remove(keyCachedOrder.size()-1);
	}
	
	public int setRss(String xml,boolean toDraw){
		Log.i(LOG_TAG, "setRss()");
		RssReader rssReader = RssReader.getInstance();
		rssReader.clearFeedQuery();
		
		return rssReader.setFeedQuery(xml);
	}
	
	
	private boolean isCached(String toSearch){
		boolean inCache = false;
		if (docsCached.containsKey(toSearch))
			inCache = true;
		
		Log.d(LOG_TAG, "isCached() "+ inCache);
		
		return inCache;
	}
	
	public void rotateCache(String xml){
		// mantengo cache dei dom per evitare ricerche consecutive identiche.
		if (keyCachedOrder.size() == max_cache) {
			docsCached.remove(keyCachedOrder.get(0).toString());
			keyCachedOrder.remove(0);
		}

		docsCached.put(keyCachedOrder.get(keyCachedOrder.size() - 1), xml);
	}
	
	//gestione dell'albero del filesystem
	/*
	 * Lo stack della visualizzazione del contenuto è cancellato solo quando
	 * viene effettuata una nuova ricerca nella activity 'Search'
	 */
	public void treeClear(){
		if (! tree.isEmpty())
			tree.clear();
	}
	
	public String treeGetElement(int i){
		String elem = "";
		if (tree.size() -1 > i)
			elem = tree.get(i);
		
		return elem;
	}
	
	private String treeGetParent(){
		String elem = "";
		
		if (! tree.isEmpty()) {
			elem = tree.get(tree.size() - 2); 
		}
		
		return elem;
	}
	
	public void treePushElement(String query){
		tree.add(query);
	}
	
	public String treePopElement(){
		String elem = "";
		
		if (! tree.isEmpty()) {
			int index = tree.size()-1;
			elem = treeGetParent();
			tree.remove(index);
		}

		return elem;
	}
	
	public int treeNum(){
		return tree.size();
	}
	
	public ArrayList<String> treeArray(){
		return tree;
	}
	
	public static int getNBackPress() {
		return n_back_press;
	}
	
	public static int resetNBackPress() {
		return n_back_press = 0;
	}
	
	public static int raiseNBackPress() {
		return n_back_press++;
	}
	//end tree
}
