/*
 * jHears, acoustic fingerprinting framework.
 * Copyright (C) 2009-2010  Juha Heljoranta.
 *
 * This file is part of jHears.
 *
 * jHears is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * jHears 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with jHears.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jhears;

import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.commons.configuration.Configuration;
import org.apache.http.client.ClientProtocolException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.jhears.web.rs.AudioEntity;
import org.jhears.web.rs.Fingerprint;
import org.jhears.web.rs.InsertRequest;
import org.jhears.web.rs.SearchResult;

public class JHearsApiLoginWrapper implements IJHearsApi {

	private final JHearsApi api;

	public JHearsApiLoginWrapper(JHearsApi api) {
		this.api = api;
	}

	@Override
	public Boolean approveFingerprint(String fingerprintId) throws IOException,
			UnauthorizedException, URISyntaxException {
		try {
			return api.approveFingerprint(fingerprintId);
		} catch (UnauthorizedException e) {
			if (loginOk()) {
				return api.approveFingerprint(fingerprintId);
			} else {
				throw e;
			}
		}
	}

	@Override
	public AudioEntity getAudioEntity(String audioEntityId) throws IOException,
			UnauthorizedException, URISyntaxException {
		try {
			return api.getAudioEntity(audioEntityId);
		} catch (UnauthorizedException e) {
			if (loginOk()) {
				return api.getAudioEntity(audioEntityId);
			} else {
				throw e;
			}
		}
	}

	@Override
	public Configuration getConfiguration(String name) throws IOException,
			UnauthorizedException, URISyntaxException {
		try {
			return api.getConfiguration(name);
		} catch (UnauthorizedException e) {
			if (loginOk()) {
				return api.getConfiguration(name);
			} else {
				throw e;
			}
		}
	}

	@Override
	public Fingerprint getFingerprint(String fingerprintId) throws IOException,
			UnauthorizedException, URISyntaxException {
		try {
			return api.getFingerprint(fingerprintId);
		} catch (UnauthorizedException e) {
			if (loginOk()) {
				return api.getFingerprint(fingerprintId);
			} else {
				throw e;
			}
		}
	}

	@Override
	public Fingerprint insert(InsertRequest insert)
			throws JsonGenerationException, JsonMappingException, IOException,
			URISyntaxException, UnauthorizedException {
		try {
			return api.insert(insert);
		} catch (UnauthorizedException e) {
			if (loginOk()) {
				return api.insert(insert);
			} else {
				throw e;
			}
		}
	}

	private boolean loginOk() throws ClientProtocolException,
			URISyntaxException, IOException, UnauthorizedException {
		if (api.canTryLogin()) {
			Boolean ok = api.login();
			return ok != null && Boolean.TRUE.equals(ok);
		}
		return false;
	}

	@Override
	public SearchResult[] search(String fingerprint) throws IOException,
			UnauthorizedException, URISyntaxException {
		try {
			return api.search(fingerprint);
		} catch (UnauthorizedException e) {
			if (loginOk()) {
				return api.search(fingerprint);
			} else {
				throw e;
			}
		}
	}
	
}
