/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2006 JP.Tessier- jean-philippe.tessier@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

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 code du (de la) "+dm.getLibelleCollectivite()+" 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 du (de la) "+dm.getLibelleCollectivite()+" 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) {}
    
}
