package tim.prune.function.settings;

import java.awt.Rectangle;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JFileChooser;

import tim.prune.App;
import tim.prune.GenericFunction;
import tim.prune.I18nManager;
import tim.prune.config.Config;
import tim.prune.config.ConfigPaths;
import tim.prune.data.DoubleRange;
import tim.prune.data.Track;
import tim.prune.tips.TipManager;

/**
 * Class to provide the function to save the config settings
 */
public class SaveConfig extends GenericFunction
{
	/**
	 * Constructor
	 * @param inApp application object for callback
	 */
	public SaveConfig(App inApp) {
		super(inApp);
	}

	/** Get the name key */
	public String getNameKey() {
		return "function.saveconfig";
	}

	/**
	 * Begin the function
	 */
	public void begin()
	{
		File configFile = getConfig().getConfigFile();
		if (configFile == null) {
			configFile = ConfigPaths.getSuggestedConfigFile();
		}
		JFileChooser chooser = new JFileChooser(configFile.getAbsoluteFile().getParent());
		chooser.setSelectedFile(configFile);
		int response = chooser.showSaveDialog(_parentFrame);
		if (response == JFileChooser.APPROVE_OPTION)
		{
			File saveFile = chooser.getSelectedFile();
			saveConfig(getConfig(), saveFile);
			if (!saveFile.getName().equals(ConfigPaths.getDefaultFilename()))
			{
				// User chose a name which isn't the default, so show a tip
				_app.showTip(TipManager.Tip_NonStandardConfigFile);
			}
		}
	}

	/**
	 * Autosave the settings file without any prompts
	 * @return true if save was successful
	 */
	public boolean silentSave() {
		return saveConfig(getConfig(), getConfig().getConfigFile());
	}

	/**
	 * Autosave has been turned on or off, so maybe need to save
	 * @param inSaveOn true if autosave was switched on
	 */
	public void autosaveSwitched(boolean inSaveOn)
	{
		File configFile = getConfig().getConfigFile();
		if (inSaveOn && configFile == null) {
			begin();
		}
		else if (!inSaveOn && configFile != null) {
			silentSave();
		}
	}

	/**
	 * Actually save the config file
	 * @param inConfig config to save
	 * @param inSaveFile file to save
	 * @return true if save was successful
	 */
	private boolean saveConfig(Config inConfig, File inSaveFile)
	{
		// Set current window position in config
		Rectangle currBounds = _app.getFrame().getBounds();
		String windowBounds = "" + currBounds.x + "x" + currBounds.y + "x"
			+ currBounds.width + "x" + currBounds.height;
		inConfig.setConfigString(Config.KEY_WINDOW_BOUNDS, windowBounds);

		final String latlonString = createLatLonStringForConfig();
		if (latlonString != null) {
			inConfig.setConfigString(Config.KEY_LATLON_RANGE, latlonString);
		}

		boolean saved = false;
		try (FileOutputStream outStream = new FileOutputStream(inSaveFile))
		{
			inConfig.getAllConfig().store(outStream, "GpsPrune config file");
			saved = true;
		} catch (IOException ioe) {
			_app.showErrorMessageNoLookup(getNameKey(),
				I18nManager.getText("error.save.failed") + " : " + ioe.getMessage());
		} catch (NullPointerException npe) {
		}
		// Remember where it was saved to
		inConfig.setConfigFile(inSaveFile);
		return saved;
	}

	/**
	 * @return semicolon-separated string containing the four values, or null
	 */
	private String createLatLonStringForConfig()
	{
		Track track = _app.getTrackInfo().getTrack();
		if (track.getNumPoints() >= 2)
		{
			final DoubleRange latRange = track.getLatRange();
			final DoubleRange lonRange = track.getLonRange();
			if (latRange.getRange() > 0.0 && lonRange.getRange() > 0.0)
			{
				return latRange.getMinimum() + ";"
					+ latRange.getMaximum() + ";"
					+ lonRange.getMinimum() + ";"
					+ lonRange.getMaximum();
			}
		}
		return null;
	}
}
