package cz.zcu.kma;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

public class GetUrl {

	/**
	 * @param args
	 * @throws MalformedURLException 
	 */
	public static void main(String[] args) throws Exception {

		String poc = "http://geoportal.gov.cz/ArcGIS/services/CENIA/cenia_rt_ortofotomapa_aktualni/MapServer/WMSServer?BBOX=";
		String end = "&WIDTH=400&HEIGHT=300&CRS=EPSG:102067&Layers=0&version=1.3.0&service=WMS&format=image/jpeg&request=GetMap&styles=";
		String obr = "./orto";
		String obr2 = ".jpg";
		String goopoc = "https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:";
		String gooend = "&chl=IGD|Hospoda";
		String gooobr = "./chart";
		String gooobr2 = ".jpg";
		int y1 = -678900;
		int y2 = -678700;
		int x1 = -1002700;
		int x2 = -1002550;
		int goopct1 = 40;
		for (int i = 1; i <= 10; i++) {
			String pom = "" + i;
			y1 = y1 + 10;
			y2 = y2 + 10;
			x1 = x1 + 10;
			x2 = x2 + 10;
			goopct1 = goopct1 + 5;
			int goopct2 = 100 - goopct1;
			String pomy1 = "" + y1;
			String pomy2 = "" + y2;
			String pomx1 = "" + x1;
			String pomx2 = "" + x2;
			String goopom1 = "" + goopct1;
			String goopom2 = "" + goopct2;
			URL result1 = new URL(poc+pomy1+","+pomx1+","+pomy2+","+pomx2+end);
			URL result2 = new URL(goopoc+goopom1+","+goopom2+gooend);
			//new GetUrl().readResult(result1.openStream());
			new GetUrl().saveResult(result1.openStream(), obr+pom+obr2);
			new GetUrl().saveResult(result2.openStream(), gooobr+pom+gooobr2);
		}
	}

	private void readResult(InputStream stream) throws Exception {

		BufferedReader in = new BufferedReader(new InputStreamReader(stream));
		String inputLine;	
		String result = "";
		while ((inputLine = in.readLine()) != null) {
			System.out.println(inputLine);
			}
		}
	private void saveResult(InputStream stream, String destinationFile) throws IOException {
		OutputStream os = new FileOutputStream(destinationFile);

		byte[] b = new byte[2048];
		int length;

		while ((length = stream.read(b)) != -1) {
			os.write(b, 0, length);
		}

		stream.close();
		os.close();
	}
	}



