/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jboss.pnc.rest.utils; //TODO move out of utils

import org.apache.commons.codec.binary.Base64;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.jboss.logging.Logger;
import org.jboss.pnc.common.Configuration;
import org.jboss.pnc.common.json.ConfigurationParseException;
import org.jboss.pnc.common.json.moduleconfig.BpmModuleConfig;
import org.jboss.pnc.common.json.moduleprovider.PncConfigProvider;
import org.jboss.pnc.common.util.HttpUtils;
import org.jboss.pnc.rest.notifications.websockets.JSonOutputConverter;
import org.jboss.pnc.spi.BuildResult;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:matejonnet@gmail.com">Matej Lazar</a>
 */
@ApplicationScoped
public class BpmNotifier { //TODO rename: remove bpm for name

    private final Logger log = Logger.getLogger(BpmNotifier.class);
    private BpmModuleConfig bpmConfig;

    @Deprecated
    public BpmNotifier() { //CDI workaround
    }

    @Inject
    public BpmNotifier(Configuration configuration) throws ConfigurationParseException {
        bpmConfig = configuration.getModuleConfig(new PncConfigProvider<BpmModuleConfig>(BpmModuleConfig.class));
    }

    public void sendBuildExecutionCompleted(String uri, BuildResult buildResult) {
        HttpPost request = new HttpPost(uri);

        List<NameValuePair> urlParameters = new ArrayList<>();

        JSonOutputConverter converter = new JSonOutputConverter();
        urlParameters.add(new BasicNameValuePair("event", converter.apply(buildResult)));

        try {
            request.setEntity(new UrlEncodedFormEntity(urlParameters));
        } catch (UnsupportedEncodingException e) {
            log.error("Error occurred preparing callback request.", e);
        }

        request.addHeader("Authorization", getAuthHeader());
        log.info("Executing request " + request.getRequestLine());

        try (CloseableHttpClient httpClient = HttpUtils.getPermissiveHttpClient()) {
            try (CloseableHttpResponse response = httpClient.execute(request)) {
                log.info(response.getStatusLine());
            }
        } catch (IOException e) {
            log.error("Error occurred executing the callback.", e);
        }
    }

    private String getAuthHeader() {
        byte[] encodedBytes = Base64.encodeBase64((bpmConfig.getUsername() + ":" + bpmConfig.getPassword()).getBytes());
        return "Basic " + new String(encodedBytes);
    }

    public void simpleHttpPostCallback(String uri) {
        HttpPost request = new HttpPost(uri);
        request.addHeader("Authorization", getAuthHeader());
        log.info("Executing request " + request.getRequestLine());

        try (CloseableHttpClient httpClient = HttpUtils.getPermissiveHttpClient()) {
            try (CloseableHttpResponse response = httpClient.execute(request)) {
                log.info(response.getStatusLine());
            }
        } catch (IOException e) {
            log.error("Error occurred executing the callback.", e);
        }
    }
}

@@ -1460,52 +1460,71 @@
 est.
-notifications.websockets.JSonOutputConverter
+restmodel.BuildResultRest;%0Aimport org.jboss.pnc.spi.BuildResult
 ;%0Aim
@@ -1546,27 +1546,58 @@
 pnc.spi.
-B
+b
 uild
-Result
+driver.exception.BuildDriverException
 ;%0A%0Aimpor
@@ -2442,264 +2442,607 @@
     
-HttpPost request = new HttpPost(uri);%0A%0A        List%3CNameValuePair%3E urlParameters = new ArrayList%3C%3E();%0A%0A        JSonOutputConverter converter = new JSonOutputConverter();%0A        urlParameters.add(new BasicNameValuePair(%22event%22, converter.apply(buildResult)
+BuildResultRest buildResultRest = null;%0A        String errMessage = %22%22;%0A        try %7B%0A            buildResultRest = new BuildResultRest(buildResult);%0A        %7D catch (BuildDriverException e) %7B%0A            log.error(%22Cannot construct rest result.%22, e);%0A            errMessage = %22Cannot construct rest result: %22 + e.getMessage();%0A        %7D%0A%0A        HttpPost request = new HttpPost(uri);%0A%0A        List%3CNameValuePair%3E urlParameters = new ArrayList%3C%3E();%0A        urlParameters.add(new BasicNameValuePair(%22event%22, buildResultRest != null ? buildResultRest.toString() : %22%7B%5C%22error%5C%22, %5C%22%22 + errMessage + %22%5C%22%7D%22
 ));%0A

Update bpm notification serializatipon.