import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Random;



/**
 * @author couedrao on 27/11/2019.
 * @project gctrl
 */
class MANOAPI {

    String deploy_gw(Map<String, String> vnfinfos) throws ClientProtocolException, IOException{
        String ip = vnfinfos.get("net").split("/")[0];
        Main.logger(this.getClass().getSimpleName(), "Deploying VNF ...");

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost;
        HttpPut httpPut;
        String inputJson, line;
        StringEntity stringEntity;
        HttpResponse response;
        BufferedReader br;
        StringBuffer result;
        
        //printing
        for (Entry<String, String> e : vnfinfos.entrySet()) {
            Main.logger(this.getClass().getSimpleName(), "\t" + e.getKey() + " : " + e.getValue());
        }
        //TODO ************ (giw2: 10.2.2.2 intern ==> 172.17.0.17 extern)
        
        //for (Entry<String, String> e : vnfinfos.entrySet()) {
	        httpclient = HttpClients.createDefault();
			httpPut = new HttpPut("http://127.0.0.1:5001/restapi/compute/dc1/"+vnfinfos.get("name"));
	        httpPut.setHeader("Accept", "application/json");
	        httpPut.setHeader("Content-type", "application/json");//vnfinfos.get("image")
	        inputJson = "{\n" +
	                "  \"image\": \""+vnfinfos.get("image")+"\",\n" +
	                "  \"network\": \"(id=input,ip="+ip+"/8)\"\n" +
	                "}";
	        stringEntity = new StringEntity(inputJson);
	        httpPut.setEntity(stringEntity);
	        response = httpclient.execute(httpPut);
	        br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
	        if (response.getStatusLine().getStatusCode() != 200) {
	            //break;
	        	//throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
	        }
	        result = new StringBuffer();
	        line = "";
	        while ((line = br.readLine()) != null) {
	            System.out.println("Response : \n" + result.append(line));
	        }
        //}
        return ip;
    }

    List<String> deploy_multi_gws_and_lb(List<Map<String, String>> vnfsinfos) {
        List<String> ips = new ArrayList<>();
        //TODO *********************
        for (Map<String, String> vnfsinfo : vnfsinfos) {
            try {
				ips.add(deploy_gw(vnfsinfo));
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
        return ips;
    }
}
