/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * ----------------------------------------
 */
package fr.tlse.irap.server.serviceDAO.impl;

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

import fr.tlse.irap.server.ApplicationContextHolder;
import fr.tlse.irap.server.serviceDAO.IImageDAO;
import fr.tlse.irap.server.serviceDAO.constant.CONFIG;
import fr.tlse.irap.server.serviceDAO.utils.ScanDirectory;

public class ImageDAOImpl implements IImageDAO {

	/* Constants for directories containing Fit images files */
	private static final Logger logger = Logger.getLogger(ImageDAOImpl.class);

	private static CONFIG prop;
	private static final String CLICK_PLOT = "CLICKPLOT";
	private static final String FIT_ERROR_PLOT = "ERROR";
//	private static final String CIR_CATALOG = "CIR";
//	private static final String CME_CATALOG = "CME";
	//private static final String ECLIPTIC_BMAP = "B";

	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd");

	public ImageDAOImpl() {
		logger.info("IMAGE DAO IMPL LOADED");
		CONFIG prop = (CONFIG) ApplicationContextHolder.getContext().getBean("confData");
	}

	/**
	 * Load plot image from CIR catalogue
	 * <p>initialTime : time of the plot
	 * <p>probeName : observer 
	 */
	@Override
	public Image loadPlotCIR(Date initialTime, String probeName) {
		return getFitImage(initialTime, prop.getCIRFIT_DIR(), probeName, CLICK_PLOT);
	}

	/**
	 * Load error plot image from CIR catalogue
	 * <p>initialTime : time of the plot
	 * <p>probeName : observer 
	 */
	@Override
	public Image loadErrorPlotCIR(Date initialTime, String probeName) {
		return getFitImage(initialTime, prop.getCIRFIT_DIR(), probeName, FIT_ERROR_PLOT);
	}

	/**
	 * Load plot image from CME catalogue
	 * <p>initialTime : time of the plot
	 * <p>probeName : observer 
	 */
	@Override
	public Image loadPlotCME(Date initialTime, String probeName) {
		return getFitImage(initialTime, prop.getCMEFIT_DIR(), probeName, CLICK_PLOT);
	}

	/**
	 * Load error plot image from CME catalogue
	 * <p>initialTime : time of the plot
	 * <p>probeName : observer 
	 */
	@Override
	public Image loadErrorPlotCME(Date initialTime, String probeName) {
		return getFitImage(initialTime, prop.getCMEFIT_DIR(), probeName, FIT_ERROR_PLOT);
	}

//	/**
//	 * Load ecliptic map of magnetic field as a PNG
//	 * <p>initialTime : time of the plot
//	 */
//	@Override
//	public Image loadEclipticBMap(Date initialTime) {
//		return getEclipticImage(initialTime, ECLIPTIC_BMAP);
//	}

	/**
	 * Get image PNG from Fit catalog
	 * @param time
	 * @param catalog
	 * @param probeName
	 * @param imageType
	 * @return
	 */
	private Image getFitImage(Date time, String fitCatalog, String probeName, String imageType) {

		logger.info("dirname " + prop.getDATA_PATH() + "/" + fitCatalog);
		logger.debug("getFitImage with t=" + time + " probe=" + probeName + " type=" + imageType);
		
		// The image to return
		Image readImage = null;
		File file = new File(prop.getDATA_PATH(), fitCatalog);
//		if (file != null) {
//			file = new File(file.getPath(), catalog);
			if (file != null) {
				file = new File(file.getPath(), probeName);
			}
//		}

		// Scan directory to get the first file corresponding to time
		File[] files = ScanDirectory.fetchMatchingFiles(file, "_" + imageType + "_" + simpleDateFormat.format(time) + "_", ".png");
		if (files != null) {
			if (files.length > 0) {
				logger.debug("Load fit plot File : " + files[0].getAbsolutePath());
				try {
					readImage = ImageIO.read(files[0]);
				} catch (IOException e) {
					logger.error("Reading failure for file " + files[0].getName(), e);
					readImage = null;
				}
				logger.debug("Read image is OK");
			}
		}
		return readImage;
	}

//	/**
//	 * Get image PNG from Ecliptic maps catalogue 
//	 * @param time
//	 * @param imageType
//	 * @return
//	 */
//	private Image getEclipticImage(Date time, String imageType) {
//
//		logger.info("dirname " + prop.getDATA_PATH() + " Ecliptic dirname " + prop.getECLIPTIC_DIRNAME());
//		logger.debug("getEclipticImage with t=" + time + " type=" + imageType);
//		
//		// The image to return
//		Image readImage = null;
//		File file = new File(prop.getDATA_PATH(), prop.getECLIPTIC_DIRNAME());
//		// Scan directory to get the first file corresponding to time
//		File[] files = ScanDirectory.fetchMatchingFiles(file, "_" + imageType + "_" + simpleDateFormat.format(time) + "_", ".png");
//		if (files != null) {
//			if (files.length > 0) {
//				logger.debug("Load image File : " + files[0].getAbsolutePath());
//				try {
//					readImage = ImageIO.read(files[0]);
//				} catch (IOException e) {
//					logger.error("Reading failure for file " + files[0].getName(), e);
//					readImage = null;
//				}
//				logger.debug("Read image is OK");
//			}
//		}
//		return readImage;
//	}

}
