/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2015-2017, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library 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;
 *    version 2.1 of the License.
 *
 *    This library 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.
 */
package org.geotools.tile.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.MemoryCacheImageInputStream;
import org.geotools.tile.ImageLoader;
import org.geotools.tile.Tile;
import org.geotools.util.logging.Logging;
import org.geotools.image.io.ImageIOExtWeps;
import org.geotools.tile.impl.osm.OSMTileWeps;
//import usda.weru.gis.gui.geotools.tile.impl.osm.OSMTileWeps;

/**
 * The CachedImageLoader is a simple ImageLoader that uses your disk as a cache for tiles. You can
 * plug this implementation into a Tile object. Note that the TileService also has a cache of its
 * own, but for caching tiles, not necessarily their images.
 *
 * <p>
 *
 * <p>Image loading is an important performance factor to tile clients. Tests have shown that image
 * loading is more important than image rendering. The risk is, however, to fill your disk with tile
 * images, so make sure to empty the directory from time to time. Also note that some tile service
 * may not allow you to save tile locally. If you do so, you might be breaching licenses. So, be
 * nice.
 *
 * @author Ugo Taddei
 * @since 12
 */
public class CachedImageLoaderWeps implements ImageLoader {

    private ImageIOExtWeps imageIOExtWepsUrl;
    private ImageIOExtWeps imageIOExtWepsFile;
    private static final Logger LOGGER = Logging.getLogger(CachedImageLoaderWeps.class);

    public final File cacheDirectory;

    public CachedImageLoaderWeps(File cacheDirectory) {
        this.cacheDirectory = cacheDirectory;
        imageIOExtWepsUrl = new ImageIOExtWeps();
        imageIOExtWepsFile = new ImageIOExtWeps();
    }
    
    public File getCacheFile (Tile tile) {
        return new File(cacheDirectory, tile.getId() + ".png");
    }
    
    public boolean isTileCached (Tile tile) {
        return getCacheFile(tile).exists();
    }

    @Override
    public BufferedImage loadImageTileImage(Tile tile) throws IOException {

        BufferedImage img = null;
        //File imgFile = new File(this.cacheDirectory, tile.getId() + ".png");
        File imgFile = getCacheFile (tile);
        
        if (imgFile.exists()) {
//            if (LOGGER.isLoggable(Level.FINE)) {
//                LOGGER.fine(
//                        "Found image in cache for '"
//                                + tile.getId()
//                                + "' at "
//                                + imgFile.getAbsolutePath());
//            }
            // MEH
            FileImageInputStream stream = new FileImageInputStream (imgFile);
            img = imageIOExtWepsFile.readBufferedImage(stream);
        } else {
//            if (LOGGER.isLoggable(Level.FINE)) {
//                LOGGER.fine(
//                        "Not found in cache '" + tile.getId() + "'. Loading from " + tile.getUrl());
//            }
            // MEH
            InputStream stream;
            if (tile instanceof OSMTileWeps) {
                stream = ((OSMTileWeps)tile).setupInputStream();
            } else {
                stream = tile.getUrl().openStream();
            }
            MemoryCacheImageInputStream cacheStream = new MemoryCacheImageInputStream (stream);
            img = imageIOExtWepsUrl.readBufferedImage(cacheStream);
            if (img != null) {
                ImageIO.write(img, "png", imgFile);
            }
//            if (LOGGER.isLoggable(Level.FINE)) {
//                LOGGER.fine("Wrote to cache " + imgFile.getAbsolutePath());
//            }
        }
        return img;
    }
}
