/*
 * Copyright
 *   2006 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 General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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 fr.gouv.finances.dgfip.xemelios.importers.EtatImporteur;
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.dgfip.utils.Pair;
import fr.gouv.finances.dgfip.xemelios.common.ToolException;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
import fr.gouv.finances.dgfip.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 boolean alwaysOverwrite = false;
	
	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,Pair Budget)	throws ToolException {
		logger.debug("getBudget");
		String codeBudget=null,libBudget=null;
		if(budget==null){
			Reader reader1 = new InputStreamReader(System.in);
            StringBuffer sb = new StringBuffer();
            sb.append("Quel est le Code Budget de ce document : ").append(fileName);
            if(budget!=null && budget.key!=null) sb.append(" <").append(budget.key).append(">");
            sb.append(" ? ");
			System.out.println(sb.toString());
			BufferedReader line1 = new BufferedReader(reader1);
			try {
				codeBudget=line1.readLine().toString();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			Reader reader2 = new InputStreamReader(System.in);
            sb = new StringBuffer();
            sb.append("Quel est le Libelle du Budget de ce document : ").append(fileName);
            if(budget!=null && budget.libelle!=null) sb.append(" <").append(budget.libelle).append(">");
            sb.append(" ? ");
			System.out.println(sb.toString());
			BufferedReader line2 = new BufferedReader(reader2);
			try {
				libBudget=line2.readLine().toString();
				line1.close();reader1.close();
				line2.close();reader2.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			budget=new Pair(codeBudget,libBudget);
		}
		return budget;
	}
	public Pair getCollectivite(DocumentModel dm, String fileName,Pair collectivite)	throws ToolException {
		logger.debug("getCollectivite");
		String codeColl=null,libColl=null;
		if(budget==null){
			Reader reader = new InputStreamReader(System.in);
            StringBuffer sb = new StringBuffer();
            sb.append("Quel est le code du (de la) ").append(dm.getLibelleCollectivite()).append(" de ce document : ").append(fileName);
            if(collectivite!=null && collectivite.key!=null) sb.append(" <").append(collectivite.key).append(">");
            sb.append(" ? ");
			System.out.println(sb.toString());
			BufferedReader line = new BufferedReader(reader);
			try {
				codeColl=line.readLine().toString();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
            sb = new StringBuffer();
            sb.append("Quel est le Libelle du (de la) ").append(dm.getLibelleCollectivite()).append(" de ce document : ").append(fileName);
            if(collectivite!=null && collectivite.libelle!=null) sb.append(" <").append(collectivite.libelle).append(">");
            sb.append(" ? ");
			System.out.println(sb.toString());
			line = new BufferedReader(reader);
			try {
				libColl=line.readLine().toString();
				line.close();reader.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			collectivite=new Pair(codeColl,libColl);
		}
		return collectivite;
	}
	public boolean getOverwrite(String docName) throws Exception {
		logger.debug("getOverwrite");
        if(alwaysOverwrite) return true;
		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;
		}
		line.close();reader.close();
		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) {
        budget = p;
    }
	public void setCollectivite(Pair p) {
        collectivite = p;
    }

    public void setEtatImporter(EtatImporteur ei) {
        // nothing to do
    }
    public void setAlwaysOverwrite(boolean b) { alwaysOverwrite = b; }

    public Pair getCollectivite() {
        return collectivite;
    }

    public Pair getBudget() {
        return budget;
    }
    
}