package com.braintree.graphql.commands.impl;

import com.braintree.commands.BrainTreePingCommand;
import com.braintree.commands.impl.AbstractCommand;
import de.hybris.platform.payment.AdapterException;

import java.util.Map;

import static com.braintree.constants.BraintreeConstants.RESULT_DATA;

public class DefaultBrainTreeGraphQLPingCommand extends AbstractCommand implements BrainTreePingCommand {

    private static final String MUTATION_NAME = "ping";

    @Override
    public String perform(Object o) throws AdapterException{
        Map<String, Object> result;
        try {
            result = makeGraphQlCall(MUTATION_NAME);
        } catch (Exception exception) {
            throw new AdapterException(exception.getMessage(), exception);
        }

        return translateResponse((Map<String, Object>) result.get(RESULT_DATA));
    }

    protected String translateResponse(Map<String, Object> data) {
        return objectMapper.convertValue(data.get(MUTATION_NAME), String.class);
    }

}
