/*
 * Copyright (c) 2017, Adam <Adam@sigterm.info>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package net.runelite.http.api.hiscore;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

@Slf4j
@RequiredArgsConstructor
public class HiscoreClient
{
	private final OkHttpClient client;

	public HiscoreResult lookup(String username, HiscoreEndpoint endpoint) throws IOException
	{
		return lookup(username, endpoint.getHiscoreURL());
	}

	public CompletableFuture<HiscoreResult> lookupAsync(String username, HiscoreEndpoint endpoint)
	{
		return lookupAsync(username, endpoint.getHiscoreURL());
	}

	public HiscoreResult lookup(String username, HttpUrl endpoint) throws IOException
	{
		return lookupSync(username, endpoint);
	}

	public HiscoreResult lookup(String username) throws IOException
	{
		return lookup(username, HiscoreEndpoint.NORMAL);
	}

	public SingleHiscoreSkillResult lookup(String username, HiscoreSkill skill, HiscoreEndpoint endpoint) throws IOException
	{
		HiscoreResult result = lookupSync(username, endpoint.getHiscoreURL());

		if (result == null)
		{
			return null;
		}

		Skill requested = result.getSkill(skill);
		SingleHiscoreSkillResult skillResult = new SingleHiscoreSkillResult();
		skillResult.setPlayer(username);
		skillResult.setSkillName(skill.getName());
		skillResult.setSkill(requested);
		return skillResult;
	}

	public SingleHiscoreSkillResult lookup(String username, HiscoreSkill skill) throws IOException
	{
		return lookup(username, skill, HiscoreEndpoint.NORMAL);
	}

	private HiscoreResult lookupSync(String username, HttpUrl hiscoreUrl) throws IOException
	{
		try (Response response = client.newCall(buildRequest(username, hiscoreUrl)).execute())
		{
			return processResponse(username, response);
		}
	}

	private CompletableFuture<HiscoreResult> lookupAsync(String username, HttpUrl hiscoreUrl)
	{
		CompletableFuture<HiscoreResult> future = new CompletableFuture<>();

		client.newCall(buildRequest(username, hiscoreUrl)).enqueue(new Callback()
		{
			@Override
			public void onFailure(Call call, IOException e)
			{
				future.completeExceptionally(e);
			}

			@Override
			public void onResponse(Call call, Response response) throws IOException
			{
				try
				{
					future.complete(processResponse(username, response));
				}
				finally
				{
					response.close();
				}
			}
		});

		return future;
	}

	private static Request buildRequest(String username, HttpUrl hiscoreUrl)
	{
		HttpUrl url = hiscoreUrl.newBuilder()
			.addQueryParameter("player", username)
			.build();

		log.debug("Built URL {}", url);

		return new Request.Builder()
			.url(url)
			.build();
	}

	private static HiscoreResult processResponse(String username, Response response) throws IOException
	{
		if (!response.isSuccessful())
		{
			if (response.code() == 404)
			{
				return null;
			}

			throw new IOException("Error retrieving data from Jagex Hiscores: " + response);
		}

		String responseStr = response.body().string();
		return parseResponse(username, responseStr);
	}

	private static HiscoreResult parseResponse(String username, String responseStr) throws IOException
	{
		CSVParser parser = CSVParser.parse(responseStr, CSVFormat.DEFAULT);

		HiscoreResultBuilder hiscoreBuilder = new HiscoreResultBuilder();
		hiscoreBuilder.setPlayer(username);

		int count = 0;

		for (CSVRecord record : parser.getRecords())
		{
			if (count++ >= HiscoreSkill.values().length)
			{
				log.warn("Jagex Hiscore API returned unexpected data");
				break; // rest is other things?
			}

			// rank, level, experience
			int rank = Integer.parseInt(record.get(0));
			int level = Integer.parseInt(record.get(1));

			// items that are not skills do not have an experience parameter
			long experience = -1;
			if (record.size() == 3)
			{
				experience = Long.parseLong(record.get(2));
			}

			Skill skill = new Skill(rank, level, experience);
			hiscoreBuilder.setNextSkill(skill);
		}

		return hiscoreBuilder.build();
	}
}
