package honk.tools.anidb;

import java.util.List;

/**
 * Wraps 2 AniDBConnections that will try to get the information from the cache first
 * @author honk
 */
public class Wrapper implements AniDBConnection {
    private AniDBConnectionCache cache;
    private AniDBConnection db;

    /**
     * Creates a wrapper for the given db and cache.
     * @param db the database connection
     * @param cache the local cache
     */
    public Wrapper(AniDBConnection db, AniDBConnectionCache cache) {
        this.db = db;
        this.cache = cache;
    }

    /* (non-Javadoc)
     * @see honk.tools.anidb.AniDBConnection#getAnime(int)
     */
    public AnimeInfo getAnime(int aid) {
        AnimeInfo info = cache.getAnime(aid);
        if (info != null) {
            return info; 
        }
        info = db.getAnime(aid);
        if (info != null) {
            cache.putAnime(info);
        }
        return info;
    }

    /* (non-Javadoc)
     * @see honk.tools.anidb.AniDBConnection#getFile(int, java.lang.String)
     */
    public FileInfo getFile(int size, String hash) {
        FileInfo info = cache.getFile(size, hash);
        if (info != null) {
            return info;
        }
        info = db.getFile(size, hash);
        if (info != null) {
            cache.putFile(info);
        }
        return info;
    }

    /* (non-Javadoc)
     * @see honk.tools.anidb.AniDBConnection#getAnimes()
     */
    public List<Integer> getAnimes() {
        return cache.getAnimes();
    }

    public void changePreferedName(int aid, String string) {
        cache.changePreferedName(aid, string);
    }

}
