package com.bayer.edam.migration.attic;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import com.bayer.edam.migration.LoggingRequestInterceptor;
import com.bayer.edam.migration.adam.ReadRecord;
import com.bayer.edam.migration.adam.Token;
import com.bayer.edam.migration.adam.WriteRecord;
import com.bayer.edam.migration.mam.Entry;

public class MediaAssistantMigrationSimple {
	
	private static final Logger log = LoggerFactory.getLogger(MediaAssistantMigrationSimple.class);
	
	public MediaAssistantMigrationSimple() {
		Properties prop = new Properties();
		InputStream input = null;
		try {
			input = new FileInputStream("secret.properties");
			prop.load(input);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		
        RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
        List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new LoggingRequestInterceptor());
        restTemplate.setInterceptors(interceptors);
        
        HttpHeaders headers = new HttpHeaders();
        headers.add("Registration", "ADAMBAYER");
        headers.add("API-VERSION", "1");
        headers.add("Authorization", "Basic " + prop.getProperty("adam.auth"));
        HttpEntity<String> entity = new HttpEntity<String>("Parameters", headers);
        Token token = restTemplate.exchange("https://edam-q.bayer.com:2015/auth", HttpMethod.GET, entity, Token.class).getBody();
        
        headers = new HttpHeaders();
//        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        headers.add("Accept", "application/hal+json");
        headers.add("Registration", "ADAMBAYER");
        headers.add("API-VERSION", "1");
        headers.add("Authorization", "Token " + token.getToken());
        headers.add("Content-Type", "application/json");
        entity = new HttpEntity<String>("Parameters", headers);
        
        ResponseEntity<String> r1 = restTemplate.exchange("https://edam-q.bayer.com:2015/record/f5bccf48-6695-4255-8d70-a7e5008e1720", HttpMethod.GET, entity, String.class);
        log.info(r1.getBody().toString());
        r1 = restTemplate.exchange("https://edam-q.bayer.com:2015/record/f5bccf48-6695-4255-8d70-a7e5008e1720/fields", HttpMethod.GET, entity, String.class);
        log.info(r1.getBody().toString());
        r1 = restTemplate.exchange("https://edam-q.bayer.com:2015/record/f5bccf48-6695-4255-8d70-a7e5008e1720/classifications", HttpMethod.GET, entity, String.class);
        log.info(r1.getBody().toString());
        
        ResponseEntity<ReadRecord> r2 = restTemplate.exchange("https://edam-q.bayer.com:2015/record/f46147866c6f49f690ffa7900097b861", HttpMethod.GET, entity, ReadRecord.class);
        log.info(r2.getBody().toString());
        
        HttpEntity<String> newRecordString = new HttpEntity<String>("{'contentType': 'Record'}", headers);
        
        try {
	        URI location = restTemplate.postForLocation("https://edam-q.bayer.com:2015/records", newRecordString);
	        log.info("Location: " + location);
        }
        catch (HttpClientErrorException hcee) {
        	log.error(hcee.getStatusCode().toString());
        	log.error(hcee.getResponseBodyAsString());
        }
        
        HttpEntity<WriteRecord> newRecord = new HttpEntity<WriteRecord>(new WriteRecord(), headers);
        
        try {
	        URI location = restTemplate.postForLocation("https://edam-q.bayer.com:2015/records", newRecord, WriteRecord.class);
	        log.info("Location: " + location);
        }
        catch (HttpClientErrorException hcee) {
        	log.error(hcee.getStatusCode().toString());
        	log.error(hcee.getResponseBodyAsString());
        }
        
//        ResponseEntity<String> r3 = restTemplate.exchange("https://edam-q.bayer.com:2015/records", HttpMethod.POST, newRecord, String.class);
//        log.info(r3.getBody());
        
        
        headers = new HttpHeaders();
        headers.add("Registration", "ADAMBAYER");
        headers.add("API-VERSION", "1");
        headers.add("Authorization", "Token " + token.getToken());
        entity = new HttpEntity<String>("Parameters", headers);
        ResponseEntity<String> rz = restTemplate.exchange("https://edam-q.bayer.com:2015/auth", HttpMethod.DELETE, entity, String.class);
        log.info(rz.toString());
        
        headers = new HttpHeaders();
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
		headers.add("Authorization", "Basic " + prop.getProperty("mam.auth"));
		headers.add("From", "migration@bayer.com");
		headers.add("Content-Type", "application/json");
        entity = new HttpEntity<String>("", headers);
        ResponseEntity<Entry> m1 = restTemplate.exchange("https://service.media-assistant.animalhealth.bayer.com/rest/migration/entry/71948", HttpMethod.GET, entity, Entry.class);
        log.info(m1.toString());
	}
	
	public static void main(String args[]) {
		new MediaAssistantMigrationSimple();
    }

}
