package com.mgaetan89.showsrage.network;

import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.mgaetan89.showsrage.Constants;
import com.mgaetan89.showsrage.model.Indexer;

import retrofit.RequestInterceptor;
import retrofit.RestAdapter;

public final class SickRageApi implements RequestInterceptor {
	private String apiKey = "";

	@NonNull
	private String apiUrl = "";

	@NonNull
	private static final SickRageApi INSTANCE = new SickRageApi();

	private String path = "";

	private SickRageServices services = null;

	@NonNull
	private String webRoot = "";

	private SickRageApi() {
	}

	@NonNull
	public static SickRageApi getInstance() {
		return INSTANCE;
	}

	@NonNull
	public String getApiUrl() {
		return buildFullApiUrl(this.apiUrl, this.path, this.apiKey);
	}

	@NonNull
	public String getBannerUrl(int indexerId, @Nullable Indexer indexer) {
		if (indexer == null) {
			return this.getApiUrl() + "?cmd=show.getbanner";
		}

		return String.format("%s?cmd=show.getbanner&%s=%d", this.getApiUrl(), indexer.getParamName(), indexerId);
	}

	@NonNull
	public String getFanArtUrl(int indexerId, @Nullable Indexer indexer) {
		if (indexer == null) {
			return this.getApiUrl() + "?cmd=show.getfanart";
		}

		return String.format("%s?cmd=show.getfanart&%s=%d", this.getApiUrl(), indexer.getParamName(), indexerId);
	}

	@NonNull
	public String getPosterUrl(int indexerId, @Nullable Indexer indexer) {
		if (indexer == null) {
			return this.getApiUrl() + "?cmd=show.getposter";
		}

		return String.format("%s?cmd=show.getposter&%s=%d", this.getApiUrl(), indexer.getParamName(), indexerId);
	}

	public SickRageServices getServices() {
		return this.services;
	}

	@NonNull
	public String getVideosUrl() {
		return this.apiUrl + "videos";
	}

	public void init(@NonNull SharedPreferences preferences) {
		boolean useHttps = preferences.getBoolean("use_https", false);
		String address = preferences.getString("server_address", "");
		String portNumber = preferences.getString("server_port_number", "");

		this.apiKey = preferences.getString("api_key", "");
		this.apiUrl = buildApiUrl(useHttps, address, portNumber);
		this.path = getApiPath(preferences.getString("server_path", ""));
		this.webRoot = getWebRoot(this.path);

		RestAdapter.Builder builder = new RestAdapter.Builder();
		builder.setEndpoint(this.apiUrl);
		builder.setRequestInterceptor(this);
		builder.setLogLevel(Constants.NETWORK_LOG_LEVEL);

		this.services = builder.build().create(SickRageServices.class);
	}

	@Override
	public void intercept(RequestFacade request) {
		request.addEncodedPathParam("api_path", this.path);
		request.addPathParam("api_key", this.apiKey);
		request.addEncodedPathParam("web_root", this.webRoot);
	}

	@NonNull
	private static String buildApiUrl(boolean useHttps, String address, String portNumber) {
		// Retrofit requires a non-empty endpoint
		// So we use the local url
		if (address.isEmpty()) {
			return "http://127.0.0.1/";
		}

		StringBuilder builder = new StringBuilder();

		if (useHttps) {
			builder.append("https://");
		} else {
			builder.append("http://");
		}

		builder.append(address);

		if (!portNumber.isEmpty()) {
			builder.append(":").append(portNumber);
		}

		builder.append("/");

		return builder.toString();
	}

	@NonNull
	private static String buildFullApiUrl(String apiUrl, String path, String apiKey) {
		String cleanPath = path.replaceAll("^/*|/*$", "");
		StringBuilder builder = new StringBuilder();
		builder.append(apiUrl);

		if (!cleanPath.isEmpty()) {
			builder.append(cleanPath).append("/");
		}

		if (!apiKey.isEmpty()) {
			builder.append(apiKey).append("/");
		}

		return builder.toString();
	}

	@NonNull
	/* package */ static String getApiPath(String apiPath) {
		if (apiPath == null || apiPath.isEmpty()) {
			return "";
		}

		return apiPath.replaceAll("^/+|/$+", "");
	}

	@NonNull
	/* package */ static String getWebRoot(String apiPath) {
		if (apiPath == null || apiPath.isEmpty()) {
			return "";
		}

		String path = apiPath;

		// Add a leading /
		if (!path.startsWith("/")) {
			path = "/" + path;
		}

		// Add a trailing /
		if (!path.endsWith("/")) {
			path += "/";
		}

		// If the path ends with /api/, we ignore that last segment
		if (path.endsWith("/api/")) {
			return path.substring(1, path.length() - 4);
		}

		return apiPath.replaceAll("^/+|/$+", "") + "/";
	}
}

@@ -3432,61 +3432,8 @@
 ) %7B%0A
-%09%09String cleanPath = path.replaceAll(%22%5E/*%7C/*$%22, %22%22);%0A
 %09%09St
@@ -3509,22 +3509,17 @@
 %0A%09%09if (!
-cleanP
+p
 ath.isEm
@@ -3549,14 +3549,9 @@
 end(
-cleanP
+p
 ath)

Remove unnecessary regex