/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package main.java.eu.dnetlib.oa.graph.operausagestats.export;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

/**
 * @author D.Pierrakos
 */
public class DownloadReportsFromOpera {

	private String operaBaseURL;
	private String operaReportPath;
	private String startPeriod;
	private String endPeriod;
	private String operaMeasuresList;
	private static final Logger logger = LoggerFactory.getLogger(UsageStatsExporter.class);

	public DownloadReportsFromOpera(String operaBaseURL, String operaReportPath, String operaMeasuresList,
		String startPeriod, String endPeriod)
		throws MalformedURLException, Exception {

		this.operaBaseURL = operaBaseURL;
		this.operaReportPath = operaReportPath;
		this.operaMeasuresList = operaMeasuresList;
		this.startPeriod = startPeriod;
		this.endPeriod = endPeriod;
	}

	public void downloadReports() throws ParseException {

		String operaMeasuresArray[] = operaMeasuresList.split(",", -1);

		for (String operaMeasure : operaMeasuresArray) {
			String operaMeasureURI[] = operaMeasure.split("/", -1);
			String operaProvider = operaMeasureURI[1];

			int max_tries = 5;
			for (int count = 0; count < max_tries; count++) {
				try {
					String url = operaBaseURL + "events?filter=measure_uri:https://" + operaMeasure
						+ "&start_date=" + startPeriod + "&end_date=" + endPeriod;
					logger.info("Trying attempt:" + count + " for measure_uri:" + url);
					BufferedInputStream in = new BufferedInputStream(new URL(url).openStream());
					HttpURLConnection connection = (HttpURLConnection) (new URL(url).openConnection());
					connection.setRequestMethod("GET");
					connection.connect();
					int code = connection.getResponseCode();
					if (code != 502) {
						BufferedReader streamReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
						String inputStr;
						StringBuilder responseStrBuilder2 = new StringBuilder();
						while ((inputStr = streamReader.readLine()) != null) {
							responseStrBuilder2.append(inputStr);
						}
						FileSystem fs = FileSystem.get(new Configuration());
						FSDataOutputStream fin = fs
							.create(
								new Path(operaReportPath + "/" + operaProvider + ".json"),
								true);
						byte[] jsonObjectRawBytes = responseStrBuilder2.toString().getBytes();
						fin.write(jsonObjectRawBytes);
						fin.writeChar('\n');

						fin.close();
						fin.close();
						count = max_tries;
					}
				} catch (IOException e) {
					System.out.println(e);
				}
			}

		}
	}
}
