/**
 * Copyright (c) 2020-2024 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.binding.soap.junit;

import java.util.concurrent.Future;

import javax.xml.ws.AsyncHandler;
import javax.xml.ws.Response;

import org.ow2.petals.bc.soap.simple.EchoRequest;
import org.ow2.petals.bc.soap.simple.EchoResponse;
import org.ow2.petals.bc.soap.simple.InvalidMessage;
import org.ow2.petals.bc.soap.simple.InvalidMessage_Exception;
import org.ow2.petals.bc.soap.simple.PrintRequest;
import org.ow2.petals.bc.soap.simple.Simple;

public class SimpleServiceImpl implements Simple {

    public static final String BUSINESS_FAULT_TO_ECHO = "BUSINESS_FAULT_TO_ECHO";

    public static final String TECHNICAL_ERROR_TO_ECHO = "TECHNICAL_ERROR_TO_ECHO";

    @Override
    public EchoResponse echo(final EchoRequest request) throws InvalidMessage_Exception {

        final String toEcho;
        if (request != null) {
            if (!request.getString().isEmpty()) {
                toEcho = request.getString().get(0);
            } else {
                toEcho = "no echo value found";
            }
        } else {
            toEcho = "no echo value found";
        }

        if (toEcho.equals(BUSINESS_FAULT_TO_ECHO)) {
            final InvalidMessage fault = new InvalidMessage();
            fault.getString().add(BUSINESS_FAULT_TO_ECHO);
            throw new InvalidMessage_Exception(BUSINESS_FAULT_TO_ECHO, fault);
        } else if (toEcho.equals(TECHNICAL_ERROR_TO_ECHO)) {
            throw new RuntimeException(TECHNICAL_ERROR_TO_ECHO);
        } else {
            final EchoResponse response = new EchoResponse();
            response.getString().add(toEcho);
            return response;
        }
    }

    @Override
    public void print(final PrintRequest request) throws InvalidMessage_Exception {

        final String toEcho;
        if (request != null) {
            if (!request.getMessage().isEmpty()) {
                toEcho = request.getMessage();
            } else {
                toEcho = "no echo value found";
            }
        } else {
            toEcho = "no echo value found";
        }

        if (toEcho.equals(BUSINESS_FAULT_TO_ECHO)) {
            final InvalidMessage fault = new InvalidMessage();
            fault.getString().add(BUSINESS_FAULT_TO_ECHO);
            throw new InvalidMessage_Exception(BUSINESS_FAULT_TO_ECHO, fault);
        } else if (toEcho.equals(TECHNICAL_ERROR_TO_ECHO)) {
            throw new RuntimeException(TECHNICAL_ERROR_TO_ECHO);
        }
    }

    @Override
    public Response<EchoResponse> echoAsync(final EchoRequest request) {
        // It is not needed to implement this method at web-service level because it is used only by web-service client
        // only
        return null;
    }

    @Override
    public Future<?> echoAsync(final EchoRequest request, final AsyncHandler<EchoResponse> asyncHandler) {
        // It is not needed to implement this method at web-service level because it is used only by web-service client
        // only
        return null;
    }

}
