package com.bayer.edam.migration.attic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.bayer.edam.migration.MamMigrationSelective;
import com.bayer.edam.migration.adam.Token;

//@SpringBootApplication
@PropertySource("file:secret.properties")
public class ShowRecord implements CommandLineRunner {

	private static final Logger log = LoggerFactory.getLogger(MamMigrationSelective.class);

	static String adamHost = "https://edam-q.bayer.com:2015";

	@Value("${adam.auth}")
	private String adamAuth;

	RestTemplate restTemplate;

	@Override
	public void run(String... strings) throws Exception {
		restTemplate = new RestTemplate();

		HttpHeaders headers = new HttpHeaders();
		headers.add("Registration", "ADAMBAYER");
		headers.add("API-VERSION", "1");
		headers.add("Accept", "application/hal+json");
		headers.add("Content-Type", "application/json");
		headers.add("Authorization", "Basic " + adamAuth);
		HttpEntity<String> headersEntity = new HttpEntity<String>("", headers);
		Token token = restTemplate.exchange(adamHost + "/auth", HttpMethod.GET, headersEntity, Token.class).getBody();

		// replace auth header
		headers.remove("Authorization");
		headers.add("Authorization", "Token " + token.getToken());

		// replace accept-charset header (in order to declutter log)
		headers.add("Accept-Charset", "us-ascii, utf-16, utf-8");

		headersEntity = new HttpEntity<String>("", headers);

		
		
		ResponseEntity<String> r1 = restTemplate.exchange(
				"https://edam-q.bayer.com:2015/record/f5bccf48-6695-4255-8d70-a7e5008e1720", HttpMethod.GET,
				headersEntity, String.class);
		log.info(r1.getBody());
		r1 = restTemplate.exchange("https://edam-q.bayer.com:2015/record/f5bccf48-6695-4255-8d70-a7e5008e1720/fields",
				HttpMethod.GET, headersEntity, String.class);
		log.info(r1.getBody());
		r1 = restTemplate.exchange(
				"https://edam-q.bayer.com:2015/record/f5bccf48-6695-4255-8d70-a7e5008e1720/classifications",
				HttpMethod.GET, headersEntity, String.class);
		log.info(r1.getBody());

		
		// logout
		ResponseEntity<String> response = restTemplate.exchange(adamHost + "/auth", HttpMethod.DELETE, headersEntity,
				String.class);
		log.info("Logged out from adam API: " + response.toString());

	}

	public static void main(String args[]) {
		SpringApplication.run(ShowRecord.class, args);
	}

}
