/*
 * Cr�� le 8 nov. 2006
 * 
 */

package fr.gouv.finances.cp.xemelios.importers.batch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.log4j.Logger;

import fr.gouv.finances.cp.utils.Pair;
import fr.gouv.finances.cp.xemelios.common.ToolException;
import fr.gouv.finances.cp.xemelios.common.config.DocumentModel;
import fr.gouv.finances.cp.xemelios.importers.ImportServiceProvider;
/**
 * This class implements a service for a batch import.
 * 
 * @since 3.1
 * 
 * @author jptessier
*/
public class ImportServiceBatchProvider implements ImportServiceProvider {
	private static Logger logger = Logger.getLogger(ImportServiceBatchProvider.class);
	public Pair collectivite,budget;
	public DocumentModel dm;
	
	public ImportServiceBatchProvider(DocumentModel dm,Pair collectivite,Pair budget) {
		super();
		this.dm=dm;
		this.budget=budget;
		this.collectivite=collectivite;
	}
	public Pair getBudget(DocumentModel dm, Pair collectivite, String fileName)	throws ToolException {
		logger.debug("getBudget");
		String codeBudget=null,libBudget=null;
		if(budget==null){
			Reader reader1 = new InputStreamReader(System.in);
			System.out.println("Quel est le Code Budget de ce document : "+fileName+" ?");
			BufferedReader line1 = new BufferedReader(reader1);
			try {
				codeBudget=line1.readLine().toString();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			Reader reader2 = new InputStreamReader(System.in);
			System.out.println("Quel est le Libelle du Budget de ce document : "+fileName+" ?");
			BufferedReader line2 = new BufferedReader(reader2);
			try {
				libBudget=line2.readLine().toString();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			budget=new Pair(codeBudget,libBudget);
		}
		return budget;
	}
	public Pair getCollectivite(DocumentModel dm, String fileName)	throws ToolException {
		logger.debug("getCollectivite");
		String codeColl=null,libColl=null;
		if(budget==null){
			Reader reader = new InputStreamReader(System.in);
			System.out.println("Quel est le SIRET de la collectivit� de ce document : "+fileName+" ?");
			BufferedReader line = new BufferedReader(reader);
			try {
				codeColl=line.readLine().toString();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			System.out.println("Quel est le Libelle de la collectivit� de ce document : "+fileName+" ?");
			line = new BufferedReader(reader);
			try {
				libColl=line.readLine().toString();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			collectivite=new Pair(codeColl,libColl);
		}
		return collectivite;
	}
	public boolean getOverwrite(String docName) throws Exception {
		logger.debug("getOverwrite");
		boolean ret=false;
		Reader reader = new InputStreamReader(System.in);
		BufferedReader line = new BufferedReader(reader);
		System.out.println("Voulez vous ecrasez le document "+docName+"? Repondre par oui ou non.");
		if(line.readLine().toLowerCase().matches("oui")){
			ret=true;
		}
		return ret;
	}
	public void displayMessage(String msg, int msgType) {
		System.out.println(msgType==ImportServiceProvider.MSG_TYPE_INFORMATION?"INFO : "+msg:"ERROR : "+msg);
	}
	
	public void displayException(Throwable t) {}
	public void displayProgress(int nbFiles) {}
	public void endLongWait() {}
	public void pushCurrentProgress(int progress) throws NullPointerException {}
	public void startLongWait() {}

    public void setDisplayFeedback(boolean display) {
        // ignored
    }

    public boolean shouldDisplayFeedback() {
        return true;
    }
	public void setBudget(Pair p) {}
	public void setCollectivite(Pair p) {}
    
}
