package be.iminds.ilabt.jfed.lowlevel.connection;

import be.iminds.ilabt.jfed.TestAuthoritiesModule;
import org.apache.xmlrpc.XmlRpcRequest;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

import java.util.Objects;

/**
 * MockReplyLibrary
 */
public class MockReplyLibrary {

        private MockSfaConnection.MockXmlRpcReply gv = null;
        public MockSfaConnection.MockXmlRpcReply getVersion3() {
                if (gv == null)
                        gv = new MockSfaConnection.AbstractMockXmlRpcReply() {
                                @Override
                                public MockSfaConnection.MockXmlRpcAnswer matches(XmlRpcRequest request) {
                                        if (!Objects.equals(getServerUrl(request), TestAuthoritiesModule.urlA_am3) &&
                                                !Objects.equals(getServerUrl(request), TestAuthoritiesModule.urlB_am2))
                                                return null;
                                        if (Objects.equals(request.getMethodName(), "GetVersion") && request.getParameterCount() == 0)
                                                return new MockSfaConnection.MockXmlRpcAnswer(getReply());
                                        return null;
                                }

                                public String getReply() {
                                        return "<?xml version=\"1.0\"?>\n" +
                                                "<methodResponse>\n" +
                                                "<params>\n" +
                                                "<param><value><struct>\n" +
                                                "<member><name>geni_api</name><value><i4>3</i4></value></member>\n" +
                                                "<member><name>value</name><value><struct>\n" +
                                                "<member><name>geni_allocate</name><value><string>geni_disjoint</string></value></member>\n" +
                                                "<member><name>geni_ad_rspec_versions</name><value><array><data>\n" +
                                                "<value><struct>\n" +
                                                "<member><name>extensions</name><value><array><data>\n" +
                                                "</data></array></value>\n" +
                                                "</member>\n" +
                                                "<member><name>namespace</name><value><string>http://www.protogeni.net/resources/rspec/0.1</string></value></member>\n" +
                                                "<member><name>version</name><value><string>0.1</string></value>\n" +
                                                "</member>\n" +
                                                "<member><name>type</name><value><string>ProtoGENI</string></value></member>\n" +
                                                "<member><name>schema</name><value><string>http://www.protogeni.net/resources/rspec/0.1/ad.xsd</string></value></member>\n" +
                                                "</struct></value>\n" +
                                                "<value><struct>\n" +
                                                "<member><name>extensions</name><value><array><data>\n" +
                                                "</data></array></value>\n" +
                                                "</member>\n" +
                                                "<member><name>namespace</name><value><string>http://www.protogeni.net/resources/rspec/0.2</string></value></member>\n" +
                                                "<member><name>version</name><value><string>0.2</string></value>\n" +
                                                "</member>\n" +
                                                "<member><name>type</name><value><string>ProtoGENI</string></value></member>\n" +
                                                "<member><name>schema</name><value><string>http://www.protogeni.net/resources/rspec/0.2/ad.xsd</string></value></member>\n" +
                                                "</struct></value>\n" +
                                                "<value><struct>\n" +
                                                "<member><name>extensions</name><value><array><data>\n" +
                                                "<value><string>http://www.protogeni.net/resources/rspec/ext/emulab/1</string></value></data></array></value>\n" +
                                                "</member>\n" +
                                                "<member><name>namespace</name><value><string>http://www.protogeni.net/resources/rspec/2</string></value></member>\n" +
                                                "<member><name>version</name><value><string>2</string></value>\n" +
                                                "</member>\n" +
                                                "<member><name>type</name><value><string>ProtoGENI</string></value></member>\n" +
                                                "<member><name>schema</name><value><string>http://www.protogeni.net/resources/rspec/2/ad.xsd</string></value></member>\n" +
                                                "</struct></value>\n" +
                                                "<value><struct>\n" +
                                                "<member><name>extensions</name><value><array><data>\n" +
                                                "<value><string>http://www.protogeni.net/resources/rspec/ext/emulab/1</string></value></data></array></value>\n" +
                                                "</member>\n" +
                                                "<member><name>namespace</name><value><string>http://www.geni.net/resources/rspec/3</string></value></member>\n" +
                                                "<member><name>version</name><value><string>3</string></value>\n" +
                                                "</member>\n" +
                                                "<member><name>type</name><value><string>GENI</string></value></member>\n" +
                                                "<member><name>schema</name><value><string>http://www.geni.net/resources/rspec/3/ad.xsd</string></value></member>\n" +
                                                "</struct></value>\n" +
                                                "</data></array></value>\n" +
                                                "</member>\n" +
                                                "<member><name>hostname</name><value><string>boss.example.com</string></value></member>\n" +
                                                "<member><name>urn</name><value><string>urn:publicid:IDN+example.com+authority+cm</string></value></member>\n" +
                                                "<member><name>hrn</name><value><string>Am For Testing</string></value></member>\n" +
                                                "<member><name>geni_api_versions</name><value><struct>\n" +
                                                "<member><name>1</name><value><string>https://am.example.com/1.0</string></value></member>\n" +
                                                "<member><name>3</name><value><string>https://am.example.com/3.0</string></value></member>\n" +
                                                "<member><name>2</name><value><string>https://am.example.com/2.0</string></value></member>\n" +
                                                "</struct></value>\n" +
                                                "</member>\n" +
                                                "<member><name>geni_api</name><value><i4>3</i4></value></member>\n" +
                                                "<member><name>code_tag</name><value><string>417e3e6864d0516206fe1a4ac124080d9e023e06</string></value></member>\n" +
                                                "<member><name>geni_request_rspec_versions</name><value><array><data>\n" +
                                                "<value><struct>\n" +
                                                "<member><name>extensions</name><value><array><data>\n" +
                                                "</data></array></value>\n" +
                                                "</member>\n" +
                                                "<member><name>namespace</name><value><string>http://www.protogeni.net/resources/rspec/0.1</string></value></member>\n" +
                                                "<member><name>version</name><value><string>0.1</string></value>\n" +
                                                "</member>\n" +
                                                "<member><name>type</name><value><string>ProtoGENI</string></value></member>\n" +
                                                "<member><name>schema</name><value><string>http://www.protogeni.net/resources/rspec/0.1/request.xsd</string></value></member>\n" +
                                                "</struct></value>\n" +
                                                "<value><struct>\n" +
                                                "<member><name>extensions</name><value><array><data>\n" +
                                                "</data></array></value>\n" +
                                                "</member>\n" +
                                                "<member><name>namespace</name><value><string>http://www.protogeni.net/resources/rspec/0.2</string></value></member>\n" +
                                                "<member><name>version</name><value><string>0.2</string></value>\n" +
                                                "</member>\n" +
                                                "<member><name>type</name><value><string>ProtoGENI</string></value></member>\n" +
                                                "<member><name>schema</name><value><string>http://www.protogeni.net/resources/rspec/0.2/request.xsd</string></value></member>\n" +
                                                "</struct></value>\n" +
                                                "<value><struct>\n" +
                                                "<member><name>extensions</name><value><array><data>\n" +
                                                "<value><string>http://www.protogeni.net/resources/rspec/ext/emulab/1</string></value></data></array></value>\n" +
                                                "</member>\n" +
                                                "<member><name>namespace</name><value><string>http://www.protogeni.net/resources/rspec/2</string></value></member>\n" +
                                                "<member><name>version</name><value><string>2</string></value>\n" +
                                                "</member>\n" +
                                                "<member><name>type</name><value><string>ProtoGENI</string></value></member>\n" +
                                                "<member><name>schema</name><value><string>http://www.protogeni.net/resources/rspec/2/request.xsd</string></value></member>\n" +
                                                "</struct></value>\n" +
                                                "<value><struct>\n" +
                                                "<member><name>extensions</name><value><array><data>\n" +
                                                "<value><string>http://www.protogeni.net/resources/rspec/ext/emulab/1</string></value></data></array></value>\n" +
                                                "</member>\n" +
                                                "<member><name>namespace</name><value><string>http://www.geni.net/resources/rspec/3</string></value></member>\n" +
                                                "<member><name>version</name><value><string>3</string></value>\n" +
                                                "</member>\n" +
                                                "<member><name>type</name><value><string>GENI</string></value></member>\n" +
                                                "<member><name>schema</name><value><string>http://www.geni.net/resources/rspec/3/request.xsd</string></value></member>\n" +
                                                "</struct></value>\n" +
                                                "</data></array></value>\n" +
                                                "</member>\n" +
                                                "<member><name>url</name><value><string>https://www.example.com:12369/protogeni/xmlrpc/am</string></value></member>\n" +
                                                "<member><name>interface</name><value><string>aggregate</string></value></member>\n" +
                                                "<member><name>geni_credential_types</name><value><array><data>\n" +
                                                "<value><struct>\n" +
                                                "<member><name>geni_version</name><value><string>2</string></value>\n" +
                                                "</member>\n" +
                                                "<member><name>geni_type</name><value><string>geni_sfa</string></value></member>\n" +
                                                "</struct></value>\n" +
                                                "<value><struct>\n" +
                                                "<member><name>geni_version</name><value><string>3</string></value>\n" +
                                                "</member>\n" +
                                                "<member><name>geni_type</name><value><string>geni_sfa</string></value></member>\n" +
                                                "</struct></value>\n" +
                                                "</data></array></value>\n" +
                                                "</member>\n" +
                                                "<member><name>geni_single_allocation</name><value><string>1</string></value>\n" +
                                                "</member>\n" +
                                                "</struct></value>\n" +
                                                "</member>\n" +
                                                "<member><name>output</name><value><string></string></value></member>\n" +
                                                "<member><name>code</name><value><struct>\n" +
                                                "<member><name>protogeni_error_log</name><value><string>urn:publicid:IDN+example.com+log+9a8417e3a84ce6e17bc44299e85d78de</string></value></member>\n" +
                                                "<member><name>protogeni_error_url</name><value><string>https://www.example.com/spewlogfile.php3?logfile=9a8417e3a84ce6e17bc44299e85d78de</string></value></member>\n" +
                                                "<member><name>geni_code</name><value><i4>0</i4></value></member>\n" +
                                                "<member><name>am_code</name><value><i4>0</i4></value></member>\n" +
                                                "<member><name>am_type</name><value><string>protogeni</string></value></member>\n" +
                                                "</struct></value>\n" +
                                                "</member>\n" +
                                                "</struct></value>\n" +
                                                "</param>\n" +
                                                "</params>\n" +
                                                "</methodResponse>\n";
                                }
                        };
                return gv;
        }

        public static String getServerUrl(XmlRpcRequest request) {
                XmlRpcClientConfigImpl config = (XmlRpcClientConfigImpl) request.getConfig();
                return config.getServerURL().toExternalForm();
        }
}
