package osm.surveyor.task.plateau;

import java.nio.file.Files;
import java.nio.file.Path;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class PlateauService {

	@Value("${task-bldg.plateau.dir}")
	private String dir;		// task-bldg.plateau.dir=plateau-data
	
	public Boolean getPlateauStatus() throws Exception {
		try {
			Path base = Path.of(dir);
			if (Files.exists(base)) {
				return Boolean.valueOf(true);
			}
			else {
				return Boolean.valueOf(false);
			}
		}
		catch (Exception e) {
			return Boolean.valueOf(false);
		}
		
	}

    public String getPlateau(String citycode) {
		try {
			Path base = Path.of(dir);
			if (Files.exists(base)) {
				Files.createDirectory(base);
			}
			Path city = Path.of(dir, citycode);
			if (Files.exists(city)) {
				Files.createDirectory(city);
			}
			return "OK";
		}
		catch (Exception e) {
			return e.toString();
		}

		/*
		Path path = Path.of(dir.toString(), fileName);
		PathResource resource = new PathResource(path);
		
		try {
			RestTemplate restTemplate = new RestTemplate();
			System.out.println(String.format("INFO: httpGet(%s)", url));
			ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
			HttpStatus httpStatus = response.getStatusCode();
			if (httpStatus.isError()) {
				System.out.println("ERROE: Can not access '"+ url +"'");
				throw new Exception("ERROE: Can not access '"+ url +"'");
			}
			String body = response.getBody();
			return body;
		}
		catch (Exception e) {
			return e.toString();
		}
		*/
    }
}
