<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:apikit="http://www.mulesoft.org/schema/mule/apikit"
      xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns:spring="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
        http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
        http://www.mulesoft.org/schema/mule/apikit http://www.mulesoft.org/schema/mule/apikit/current/mule-apikit.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd ">

    <http:listener-config name="httpListenerConfig" host="0.0.0.0" port="${http.port}" />
    <apikit:config name="leaguesApi" raml="leagues.raml" consoleEnabled="false"/>
    <spring:bean id="league-api" class="org.mule.examples.leagues.LeagueAPI" init-method="initialize"/>
    <context:property-placeholder location="leagues.properties" />

    <apikit:mapping-exception-strategy name="apiKitGlobalExceptionMapping">
        <apikit:mapping statusCode="404">
            <apikit:exception value="org.mule.module.apikit.exception.NotFoundException" />
            <set-property propertyName="Content-Type" value="application/json"/>
            <set-payload value="{ &quot;message&quot;: &quot;Resource not found&quot; }" />
        </apikit:mapping>
        <apikit:mapping statusCode="405">
            <apikit:exception value="org.mule.module.apikit.exception.MethodNotAllowedException" />
            <set-property propertyName="Content-Type" value="application/json"/>
            <set-payload value="{ &quot;message&quot;: &quot;Method not allowed&quot; }" />
        </apikit:mapping>
        <apikit:mapping statusCode="415">
            <apikit:exception value="org.mule.module.apikit.exception.UnsupportedMediaTypeException" />
            <set-property propertyName="Content-Type" value="application/json"/>
            <set-payload value="{ &quot;message&quot;: &quot;Unsupported media type&quot; }" />
        </apikit:mapping>
        <apikit:mapping statusCode="406">
            <apikit:exception value="org.mule.module.apikit.exception.NotAcceptableException" />
            <set-property propertyName="Content-Type" value="application/json"/>
            <set-payload value="{ &quot;message&quot;: &quot;Not acceptable&quot; }" />
        </apikit:mapping>
        <apikit:mapping statusCode="400">
            <apikit:exception value="org.mule.module.apikit.exception.BadRequestException" />
            <set-property propertyName="Content-Type" value="application/json"/>
            <set-payload value="{ &quot;message&quot;: &quot;Bad request&quot; }" />
        </apikit:mapping>
        <apikit:mapping statusCode="409">
            <apikit:exception value="org.mule.examples.leagues.exceptions.ConflictException" />
            <set-payload value="conflict" />
        </apikit:mapping>
    </apikit:mapping-exception-strategy>

    <flow name="main">
        <http:listener config-ref="httpListenerConfig" path="/api/*" />
        <apikit:router config-ref="leaguesApi"/>
        <exception-strategy ref="apiKitGlobalExceptionMapping" />
    </flow>

    <flow name="console">
        <http:listener config-ref="httpListenerConfig" path="/console/*" />
        <apikit:console config-ref="leaguesApi"/>
    </flow>

    <flow name="delete:/teams/{teamId}:leaguesApi">
        <expression-component>
            app.registry['league-api'].deleteTeam(teamId);
        </expression-component>
    </flow>

    <flow name="get:/teams:leaguesApi">
        <expression-component>
            payload = app.registry['league-api'].getTeams(message.inboundProperties['http.query.params']['city']);
        </expression-component>
    </flow>

    <flow name="get:/fixture/{homeTeamId}/{awayTeamId}:leaguesApi">
        <expression-component>
            payload = app.registry['league-api'].getMatch(homeTeamId, awayTeamId);
        </expression-component>
    </flow>

    <flow name="get:/teams/{teamId}:leaguesApi">
        <expression-component>
            payload = app.registry['league-api'].getTeam(teamId);
        </expression-component>
    </flow>

    <flow name="get:/positions:leaguesApi">
        <expression-component>
            payload = app.registry['league-api'].getPositions();
        </expression-component>
    </flow>

    <flow name="get:/fixture:leaguesApi">
        <expression-component>
            payload = app.registry['league-api'].getFixture();
        </expression-component>
    </flow>

    <flow name="post:/teams:leaguesApi">
        <custom-transformer class="org.mule.module.apikit.transform.PayloadNormalizerTransformer"
                            returnClass="org.mule.examples.leagues.request.Team"/>
        <set-property propertyName="Location" value="http://localhost:${http.port}/api/teams/#[payload.id]"/>
        <expression-component>
            app.registry['league-api'].addTeam(payload);
        </expression-component>
        <set-payload value="#[org.mule.transport.NullPayload.getInstance()]"/>
    </flow>

    <flow name="put:/fixture/{homeTeamId}/{awayTeamId}:leaguesApi">
        <custom-transformer class="org.mule.module.apikit.transform.PayloadNormalizerTransformer"
                            returnClass="org.mule.examples.leagues.request.Score"/>
        <expression-component>
            app.registry['league-api'].updateScore(homeTeamId, awayTeamId, payload);
        </expression-component>
        <set-payload value="#[org.mule.transport.NullPayload.getInstance()]"/>
    </flow>

    <flow name="put:/teams/{teamId}:leaguesApi">
        <custom-transformer class="org.mule.module.apikit.transform.PayloadNormalizerTransformer"
                            returnClass="org.mule.examples.leagues.request.UpdateTeam"/>
        <expression-component>
            app.registry['league-api'].updateTeam(teamId, payload);
        </expression-component>
        <set-payload value="#[org.mule.transport.NullPayload.getInstance()]"/>
    </flow>

</mule>
