import io

import pytest
import requests_mock
from lxml import etree
from pretend import stub
from six import StringIO

from tests.utils import DummyTransport, assert_nodes_equal
from zeep import Client, wsdl
from zeep.transports import Transport


@pytest.mark.requests
def test_parse_soap_wsdl():
    wsdl_file = io.StringIO(u"""
        <?xml version="1.0"?>
        <wsdl:definitions
          xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns:tns="http://tests.python-zeep.org/"
          xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
          xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
          targetNamespace="http://tests.python-zeep.org/">

          <wsdl:types>
            <xsd:schema
                targetNamespace="http://tests.python-zeep.org/"
                xmlns:tns="http://tests.python-zeep.org/"
                elementFormDefault="qualified">
              <xsd:element name="input" type="xsd:string"/>

              <xsd:element name="output">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="item_1" type="tns:type_1"/>
                    <xsd:element name="item_2" type="tns:type_2"/>
                  </xsd:sequence>
                </xsd:complexType>
             </xsd:element>

              <xsd:complexType name="type_1">
                <xsd:sequence>
                  <xsd:element name="subitem_1" type="xsd:string"/>
                  <xsd:element name="subitem_2" type="xsd:string"/>
                </xsd:sequence>
              </xsd:complexType>
              <xsd:complexType name="type_2">
                <xsd:sequence>
                  <xsd:element name="subitem_1" type="tns:type_1"/>
                  <xsd:element name="subitem_2" type="xsd:string"/>
                </xsd:sequence>
              </xsd:complexType>
            </xsd:schema>
          </wsdl:types>

          <wsdl:message name="TestOperationRequest">
            <wsdl:part name="response" element="tns:input"/>
          </wsdl:message>

          <wsdl:message name="TestOperationResponse">
            <wsdl:part name="response" element="tns:output"/>
          </wsdl:message>

          <wsdl:portType name="TestPortType">
            <wsdl:operation name="TestOperation">
              <wsdl:input message="TestOperationRequest"/>
              <wsdl:output message="TestOperationResponse"/>
            </wsdl:operation>
          </wsdl:portType>

          <wsdl:binding name="TestBinding" type="tns:TestPortType">
            <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
            <wsdl:operation name="TestOperation">
              <soap:operation soapAction=""/>
              <wsdl:input name="TestOperationRequest">
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
              </wsdl:input>
              <wsdl:output name="TestOperationResponse">
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
              </wsdl:output>
            </wsdl:operation>
          </wsdl:binding>
          <wsdl:service name="TestService">
            <wsdl:documentation>Test service</wsdl:documentation>
            <wsdl:port name="TestPortType" binding="tns:TestBinding">
              <soap:address location="http://tests.python-zeep.org/test"/>
            </wsdl:port>
          </wsdl:service>
        </wsdl:definitions>
    """.strip())

    content = """
        <?xml version="1.0"?>
        <soapenv:Envelope
            xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:tns="http://tests.python-zeep.org/">
           <soapenv:Body>
              <tns:TestOperationResponse>
                <tns:output>
                  <tns:item_1 href="#id0"/>
                  <tns:item_2>
                    <tns:subitem_1>
                      <tns:subitem_1>foo</tns:subitem_1>
                      <tns:subitem_2>bar</tns:subitem_2>
                    </tns:subitem_1>
                  <tns:subitem_2>bar</tns:subitem_2>
                  </tns:item_2>
                </tns:output>
              </tns:TestOperationResponse>

              <multiRef id="id0">
                <tns:subitem_1>foo</tns:subitem_1>
                <tns:subitem_2>bar</tns:subitem_2>
              </multiRef>
           </soapenv:Body>
        </soapenv:Envelope>
    """.strip()

    client = Client(wsdl_file, transport=Transport(),)
    response = stub(
        status_code=200,
        headers={},
        content=content)

    operation = client.service._binding._operations['TestOperation']
    result = client.service._binding.process_reply(
        client, operation, response)

    assert result.item_1.subitem_1 == 'foo'
    assert result.item_1.subitem_2 == 'bar'
    assert result.item_2.subitem_1.subitem_1 == 'foo'
    assert result.item_2.subitem_1.subitem_2 == 'bar'
    assert result.item_2.subitem_2 == 'bar'
