package ru.r2cloud.satellite;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.contains;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import ru.r2cloud.TestConfiguration;
import ru.r2cloud.model.SatPass;
import ru.r2cloud.model.Satellite;
import ru.r2cloud.util.ProcessFactory;
import uk.me.g4dpz.satellite.SatPos;

public class ObservationTestIT {

	@Rule
	public TemporaryFolder tempFolder = new TemporaryFolder();

	private TestConfiguration config;
	private Satellite satellite;
	private ProcessFactory factory;

	@Test
	public void testSuccess() throws Exception {
		String rtlfm = UUID.randomUUID().toString();
		config.setProperty("satellites.sox.path", "sox");
		config.setProperty("satellites.rtlsdr.path", rtlfm);
		config.setProperty("satellites.wxtoimg.path", "wxtoimg");

		when(factory.create(contains("sox"), any(), anyBoolean())).thenCallRealMethod();
		when(factory.create(contains("wxtoimg"), any(), anyBoolean())).thenCallRealMethod();
		when(factory.create(contains(rtlfm), any(), anyBoolean())).thenReturn(new ProcessWrapperMock(new AutoGeneratedBytes(), null));

		SatPass nextPass = create(new Date(), new Date());
		Observation o = new Observation(config, satellite, nextPass, factory);
		o.start();
		o.stop();
		
		File a = new File(tempFolder.getRoot(), satellite.getId() + File.separator + "data" + File.separator + nextPass.getStart().getTime().getTime() + File.separator + "a.jpg");
		File b = new File(tempFolder.getRoot(), satellite.getId() + File.separator + "data" + File.separator + nextPass.getStart().getTime().getTime() + File.separator + "b.jpg");

		try (FileInputStream fis = new FileInputStream(a)) {
			assertStreamsEqual(ObservationTestIT.class.getClassLoader().getResourceAsStream("a.jpg"), fis);
		}
		try (FileInputStream fis = new FileInputStream(b)) {
			assertStreamsEqual(ObservationTestIT.class.getClassLoader().getResourceAsStream("b.jpg"), fis);
		}
		
	}

	private static void assertStreamsEqual(InputStream expected, InputStream actual) {
		while (true) {
			try {
				assertEquals(expected.read(), actual.read());
			} catch (IOException e) {
				fail("unexpected exception: " + e.getMessage());
			}
		}
	}

	@Before
	public void start() throws Exception {
		config = new TestConfiguration(tempFolder);
		config.setProperty("satellites.enabled", true);
		config.setProperty("satellites.basepath.location", tempFolder.getRoot().getAbsolutePath());
		config.update();

		satellite = new Satellite();
		satellite.setId(UUID.randomUUID().toString());
		satellite.setFrequency(10);
		satellite.setName(UUID.randomUUID().toString());

		factory = mock(ProcessFactory.class);
	}

	private static SatPass create(Date start, Date end) {
		SatPos startPos = new SatPos();
		startPos.setTime(start);
		SatPos endPos = new SatPos();
		endPos.setTime(end);
		SatPass result = new SatPass();
		result.setStart(startPos);
		result.setEnd(endPos);
		return result;
	}

}

@@ -319,16 +319,47 @@
 .when;%0A%0A
+import java.io.BufferedWriter;%0A
 import j
@@ -367,24 +367,24 @@
 va.io.File;%0A
-
 import java.
@@ -395,32 +395,59 @@
 ileInputStream;%0A
+import java.io.FileWriter;%0A
 import java.io.I
@@ -1747,19 +1747,17 @@
 stop();%0A
-%09%09
 %0A
+
 %09%09File a
@@ -2417,11 +2417,9 @@
 %09%09%7D%0A
-%09%09
 %0A
+
 %09%7D%0A%0A
@@ -2925,17 +2925,339 @@
 fig.
-update();
+setProperty(%22satellites.wxtoimg.license.path%22, %22/home/pi/.wxtoimglic%22);%0A%09%09config.update();%0A%0A%09%09try (BufferedWriter w = new BufferedWriter(new FileWriter(config.getProperty(%22satellites.wxtoimg.license.path%22)))) %7B%0A%09%09%09w.append(%222.11.2 beta%5Cn%22);%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09throw new RuntimeException(%22unable to setup wxtoimg%22, e);%0A%09%09%7D
 %0A%0A%09%09

fix integration test