/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * Version : 15/09/2016 : SCA : V2.2 (PSWS-H2020)
 *  Set a default J-map image if not exists
 * Version : 06/11/2017 : SCA : V2.2.2
 *  Redmine #5403 - DMPT3 
 * Version : 12/09/2023 : LBE : V2.8
 * DM-ID : Redmine#8799 : 12/09/2023 : new carrington maps
 * ----------------------------------------
 */
package fr.tlse.irap.server.serviceDAO.impl;

import java.io.File;
import java.util.Date;

import org.apache.log4j.Logger;

import fr.tlse.irap.map.CarringtonMap;
import fr.tlse.irap.map.CarringtonMapType;
import fr.tlse.irap.map.JMap;
import fr.tlse.irap.server.ApplicationContextHolder;
import fr.tlse.irap.server.serviceDAO.IFitsDAO;
import fr.tlse.irap.server.serviceDAO.constant.CONFIG;
import fr.tlse.irap.server.serviceDAO.utils.FitsHelper;
import fr.tlse.irap.spaceElement.Probe;

public class FitsDAOImpl implements IFitsDAO {

	private static final Logger logger = Logger.getLogger(FitsDAOImpl.class);

	private static CONFIG prop;

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

	/**
	 * Load Carrington map 
	 */
	@Override
	public CarringtonMap loadCarringtonMap(Date time, CarringtonMapType cmType) {

		logger.info("CRMAP PATH=" + prop.getDATA_PATH() + "/" + prop.getCRMAP_DIR());
		logger.debug("loadCarringtonMap with t=" + time + " probe="
				+ cmType.getProbe() + " instrument=" + cmType.getInstrument() + " measurement=" + cmType.getMeasurement());

		File path = new File(prop.getDATA_PATH(), prop.getCRMAP_DIR());
		CarringtonMap carringtonMap = null;

		if (path != null) {
			// Get directory containing the CarringtonMap for the given probe
			path = new File(path.getPath(), cmType.getProbe());
			// Then, get directory for the given measurement
			if (path != null) {
				path = new File(path.getPath(), cmType.getMeasurement());
			}
		}
		
		File file = FitsHelper.findCarringtonFile(cmType, time, path);
		if (file != null) {
			logger.info("Load Carringtonmap File " + file.getName() + " for date="+ time);
			boolean isModel = (cmType.getProbe().equals("MODEL"));
			try {
				carringtonMap = FitsHelper.loadImage(file);
				carringtonMap.getHeader().setProbe(cmType.getProbe());
				carringtonMap.getHeader().setMeasurement(cmType.getMeasurement());
				if (isModel) {
					carringtonMap.getHeader().setAltitude(Float.parseFloat(cmType.getRadialDistances().get(0)));
					carringtonMap.getHeader().setUnit(FitsHelper.getCarringtonUnit(cmType.getMeasurement()));
				}
				if (carringtonMap.getHeader().getRotationNumber() == 0) {
					carringtonMap.getHeader().setRotationNumber(FitsHelper.getCarringtonNumber(file.getName()));
				}
			} catch (Exception e) {
				logger.error("Reading failure for file " + file, e);
			}
		}
		return carringtonMap;
	}

	/**
	 * Load J-map
	 */
	@Override
	public JMap loadJMap(Probe probe, String type, Date d1, Date d2) {
		logger.info("J-maps dirname " + prop.getDATA_PATH() + "/" + prop.getJMAP_DIR());
		logger.debug("loadJMap between d1=" + d1 + " and d2=" + d2 + " probe=" + probe);

		File file = new File(prop.getDATA_PATH(), prop.getJMAP_DIR());
		JMap jMap = new JMap();

		// Then, get directory containing the J-maps for the given observer
		if (file != null) {
			file = new File(file.getPath(), probe.getName());
			if (!type.equals("REAL")) {
				if (file != null) {
					file = new File(file.getPath(), type);
				}
			}
		}
		if (file != null) {
			String correspondingJmapfileName = FitsHelper.calculateFileName(d1, d2, file);
			if (correspondingJmapfileName != null) {
				file = new File(file.getPath(), correspondingJmapfileName);
				logger.info("Load JMap File " + file.getName());
				try {
					jMap = FitsHelper.loadImage(d1, d2, file, type);
					jMap.getHeader().setObserver(probe);
				} catch (Exception e) {
					logger.error("Reading failure for file " + file, e);
				}
			}
		}
		return jMap;
	}

	@Override
	public JMap loadDefaultJMap(Probe probe, String type, Date d1, Date d2) {
		logger.info("Get default image");
		JMap jMap = FitsHelper.getDefaultImage(d1, d2, type);
		jMap.getHeader().setObserver(probe);
		return jMap;
	}

}
