package de.evoila.cf.broker.service.impl;

import de.evoila.cf.broker.bean.impl.BackupConfiguration;
import de.evoila.cf.broker.exception.ServiceInstanceDoesNotExistException;
import de.evoila.cf.broker.service.BackupService;
import de.evoila.cf.broker.service.InstanceCredentialService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.Base64;
import java.util.HashMap;

@Service
@ConditionalOnBean(BackupConfiguration.class)
public class BackupServiceImpl implements BackupService{
    private final RestTemplate rest;
    private final HttpHeaders headers;
    InstanceCredentialService credentialService;
    BackupConfiguration config;

    public BackupServiceImpl (BackupConfiguration config, InstanceCredentialService credentialService) {
        this.config = config;
        this.credentialService = credentialService;

        this.rest = new RestTemplate();
        this.headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("Accept", "application/json");
        headers.add("Authorization", encodeCredentials());
    }

    private String encodeCredentials () {
        String str = config.getUser()+":"+config.getPassword();
        return "Basic " + Base64.getEncoder().encodeToString(str.getBytes());
    }

    @Override
    public ResponseEntity<HashMap> backupNow (String serviceInstanceId, HashMap fileDestination) throws ServiceInstanceDoesNotExistException {
        HashMap credentials = credentialService.getCredentialsForInstanceId(serviceInstanceId);
        HashMap body = new HashMap();
        body.put("source", credentials);
        body.put("destination", fileDestination);

        RequestEntity e  = new RequestEntity(body, headers, HttpMethod.PUT,
                                             URI.create(config.getUri()+"/backup"));
        ResponseEntity response = rest.exchange(e,HashMap.class);
        return response;
    }

    @Override
    public ResponseEntity<HashMap> restoreNow (String serviceInstanceId, HashMap fileDestination) throws ServiceInstanceDoesNotExistException {
        HashMap credentials = credentialService.getCredentialsForInstanceId(serviceInstanceId);
        HashMap body = new HashMap();
        body.put("destination", credentials);
        body.put("source", fileDestination);

        RequestEntity e  = new RequestEntity(body, headers, HttpMethod.PUT,
                                             URI.create(config.getUri()+"/restore"));
        ResponseEntity response = rest.exchange(e,HashMap.class);
        return response;
    }

    @Override
    public ResponseEntity<HashMap> getJobs (String serviceInstanceId, int page, int pageSize) {
        HashMap queryParams = new HashMap();
        queryParams.put("page", page);
        queryParams.put("page_size", pageSize);
        HttpEntity entity = new HttpEntity(headers);
        ResponseEntity response = rest.exchange(config.getUri()+"/jobs/byInstance/"+serviceInstanceId,
                                                HttpMethod.GET, entity, HashMap.class, queryParams);

        return response;
    }

    @Override
    public ResponseEntity<HashMap> getPlans (String serviceInstanceId) {
        HttpEntity entity = new HttpEntity(headers);
        ResponseEntity response = rest.exchange(config.getUri()+"/plans/byInstance/"+serviceInstanceId,
                                                HttpMethod.GET, entity, HashMap.class);
        return response;
    }

    @Override
    public ResponseEntity<HashMap> deleteJob (String serviceInstanceId, String jobid) {
        HttpEntity entity = new HttpEntity(headers);
        ResponseEntity response = rest.exchange(config.getUri()+"/plans",
                                                HttpMethod.POST, entity, HashMap.class);
        return response;
    }

    @Override
    public ResponseEntity<HashMap> postPlan (String serviceInstanceId, HashMap plan) throws ServiceInstanceDoesNotExistException {
        HashMap credentials = credentialService.getCredentialsForInstanceId(serviceInstanceId);
        plan.put("source", credentials);
        HttpEntity entity = new HttpEntity(credentials, headers);
        ResponseEntity response = rest.exchange(config.getUri()+"/plans",
                                                HttpMethod.POST, entity, HashMap.class);
        return response;
    }

    @Override
    public ResponseEntity<HashMap> deletePlan (String serviceInstanceId, String planid) {
        HttpEntity entity = new HttpEntity(headers);
        ResponseEntity response = rest.exchange(config.getUri()+"/plan"+planid,
                                                HttpMethod.DELETE, entity, HashMap.class);
        return response;
    }

    @Override
    public ResponseEntity<HashMap> updatePlan (String serviceInstanceId, String planId, HashMap plan) throws ServiceInstanceDoesNotExistException {
        HashMap credentials = credentialService.getCredentialsForInstanceId(serviceInstanceId);
        plan.put("source", credentials);
        HttpEntity entity = new HttpEntity(credentials, headers);
        ResponseEntity response = rest.exchange(config.getUri()+"/plan"+planId,
                                                HttpMethod.PATCH, entity, HashMap.class);
        return null;
    }
}

@@ -273,32 +273,77 @@
 dentialService;%0A
+import org.codehaus.groovy.util.ListHashMap;%0A
 import org.sprin
@@ -401,16 +401,76 @@
 OnBean;%0A
+import org.springframework.core.ParameterizedTypeReference;%0A
 import o
@@ -667,16 +667,39 @@
 HashMap;
+%0Aimport java.util.List;
 %0A%0A@Servi
@@ -3192,32 +3192,41 @@
   ResponseEntity
+%3CHashMap%3E
  response = rest
@@ -3250,17 +3250,19 @@
 getUri()
-+
+ + 
 %22/jobs/b
@@ -3264,33 +3264,35 @@
 obs/byInstance/%22
-+
+ + 
 serviceInstanceI
@@ -3298,40 +3298,8 @@
 Id,%0A
-                                
     
@@ -3334,29 +3334,66 @@
 entity, 
-HashMap.class
+new ParameterizedTypeReference%3CList%3CHashMap%3E%3E() %7B%7D
 , queryP
@@ -3745,37 +3745,74 @@
 ET, entity, 
-HashMap.class
+new ParameterizedTypeReference%3CList%3CHashMap%3E%3E() %7B%7D
 );%0A        r

Change HashMap to ParameterizedTypeReference to ge valid results from the endpoints