package ru.r2cloud.cloud;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eclipsesource.json.JsonArray;

import ru.r2cloud.SpectogramService;
import ru.r2cloud.model.Observation;
import ru.r2cloud.model.ObservationStatus;
import ru.r2cloud.satellite.ObservationDao;
import ru.r2cloud.util.Configuration;

public class R2ServerService {

	private static final Logger LOG = LoggerFactory.getLogger(R2ServerService.class);

	private final Configuration config;
	private final ObservationDao dao;
	private final R2ServerClient client;
	private final SpectogramService spectogramService;

	public R2ServerService(Configuration config, ObservationDao dao, R2ServerClient client, SpectogramService spectogramService) {
		this.config = config;
		this.dao = dao;
		this.client = client;
		this.spectogramService = spectogramService;
	}

	public void uploadObservation(Observation observation) {
		String apiKey = config.getProperty("r2cloud.apiKey");
		if (apiKey == null) {
			return;
		}
		LOG.info("[{}] uploading observation", observation.getId());
		Long id = client.saveMeta(observation);
		if (id == null) {
			return;
		}
		if (observation.getDataPath() != null) {
			client.saveBinary(id, observation.getDataPath());
		} else if (observation.getImagePath() != null) {
			client.saveJpeg(id, observation.getImagePath());
		}
		// update status to UPLOADED even if data or spectogram not
		// in future UPLOADED might be split into UPLOADED_META, UPLOADED_DATA, UPLOADED_SPECTOGRAM
		// right now, this seems too complicated
		observation.setStatus(ObservationStatus.UPLOADED);
		dao.update(observation);

		if (config.getBoolean("r2cloud.syncSpectogram")) {
			File spectogram;
			if (observation.getSpectogramPath() == null) {
				spectogram = spectogramService.create(observation);
				if (spectogram != null) {
					spectogram = dao.saveSpectogram(observation.getSatelliteId(), observation.getId(), spectogram);
				}
				if (spectogram == null) {
					LOG.info("[{}] unable to save spectogram", observation.getId());
				}
			} else {
				spectogram = observation.getSpectogramPath();
			}
			if (spectogram != null) {
				client.saveSpectogram(id, spectogram);
			}
		}
		LOG.info("[{}] observation uploaded", observation.getId());
	}

	public void saveMetrics(JsonArray metrics) {
		String apiKey = config.getProperty("r2cloud.apiKey");
		if (apiKey == null) {
			return;
		}
		client.saveMetrics(metrics);
	}

}

@@ -1695,16 +1695,190 @@
 m%22)) %7B%0A%09
+%09%09uploadSpectogram(observation, id);%0A%09%09%7D%0A%09%09LOG.info(%22%5B%7B%7D%5D observation uploaded%22, observation.getId());%0A%09%7D%0A%0A%09private void uploadSpectogram(Observation observation, Long id) %7B%0A
 %09%09File s
@@ -1888,17 +1888,16 @@
 togram;%0A
-%09
 %09%09if (ob
@@ -1936,25 +1936,24 @@
  null) %7B%0A%09%09%09
-%09
 spectogram =
@@ -1991,25 +1991,24 @@
 vation);%0A%09%09%09
-%09
 if (spectogr
@@ -2017,25 +2017,24 @@
  != null) %7B%0A
-%09
 %09%09%09%09spectogr
@@ -2121,25 +2121,24 @@
 ogram);%0A
-%09
 %09%09%09%7D%0A%09%09%09
 %09if (spe
@@ -2129,17 +2129,16 @@
 %09%09%09%7D%0A%09%09%09
-%09
 if (spec
@@ -2155,17 +2155,16 @@
 null) %7B%0A
-%09
 %09%09%09%09LOG.
@@ -2227,20 +2227,18 @@
 ());%0A%09%09%09
-%09
 %7D%0A
-%09
 %09%09%7D else
@@ -2239,25 +2239,24 @@
 %7D else %7B%0A%09%09%09
-%09
 spectogram =
@@ -2287,28 +2287,26 @@
 amPath();%0A%09%09
-%09
 %7D%0A
-%09
 %09%09if (specto
@@ -2321,17 +2321,16 @@
 null) %7B%0A
-%09
 %09%09%09clien
@@ -2369,76 +2369,9 @@
 ;%0A%09%09
-%09%7D%0A%09%09%7D%0A%09%09LOG.info(%22%5B%7B%7D%5D observation uploaded%22, observation.getId());
+%7D
 %0A%09%7D%0A

fix code smells