<link rel="import" href="../../../../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../../../../../bower_components/iron-ajax/iron-ajax.html">

<!--

`<channelstream-connection>` allows you to connect and interact with channelstream server
abstracting websocket/long-polling connections from you.

In typical use, just slap some `<channelstream-connection>` at the top of your body:

    <body>
          <channelstream-connection
          username="{{user.username}}"
          connect-url="http://127.0.0.1:8000/demo/connect"
          disconnect-url="http://127.0.0.1:8000/disconnect"
          subscribe-url="http://127.0.0.1:8000/demo/subscribe"
          message-url="http://127.0.0.1:8000/demo/message"
          long-poll-url="http://127.0.0.1:8000/listen"
          websocket-url="http://127.0.0.1:8000/ws"
          channels-url='["channel1", "channel2"]' />

Then you can do `channelstreamElem.connect()` to kick off your connection.
This element also handles automatic reconnections.

## Default handlers

By default element has a listener attached that will fire `startListening()` handler on `channelstream-connected` event.

By default element has a listener attached that will fire `retryConnection()` handler on `channelstream-connect-error` event,
this handler will forever try to re-establish connection to the server incrementing intervals between retries up to 1 minute.

-->
<dom-module id="channelstream-connection">
    <template>

        <iron-ajax
            id="ajaxConnect"
            url=""
            handle-as="json"
            method="post"
            content-type="application/json"
            loading="{{loadingConnect}}"
            last-response="{{connectLastResponse}}"
            on-response="_handleConnect"
            on-error="_handleConnectError"
            debounce-duration="100"></iron-ajax>

        <iron-ajax
            id="ajaxDisconnect"
            url=""
            handle-as="json"
            method="post"
            content-type="application/json"
            loading="{{loadingDisconnect}}"
            last-response="{{_disconnectLastResponse}}"
            on-response="_handleDisconnect"
            debounce-duration="100"></iron-ajax>

        <iron-ajax
            id="ajaxSubscribe"
            url=""
            handle-as="json"
            method="post"
            content-type="application/json"
            loading="{{loadingSubscribe}}"
            last-response="{{subscribeLastResponse}}"
            on-response="_handleSubscribe"
            debounce-duration="100"></iron-ajax>

        <iron-ajax
            id="ajaxUnsubscribe"
            url=""
            handle-as="json"
            method="post"
            content-type="application/json"
            loading="{{loadingUnsubscribe}}"
            last-response="{{unsubscribeLastResponse}}"
            on-response="_handleUnsubscribe"
            debounce-duration="100"></iron-ajax>

        <iron-ajax
            id="ajaxMessage"
            url=""
            handle-as="json"
            method="post"
            content-type="application/json"
            loading="{{loadingMessage}}"
            last-response="{{messageLastResponse}}"
            on-response="_handleMessage"
            on-error="_handleMessageError"
            debounce-duration="100"></iron-ajax>

        <iron-ajax
            id="ajaxListen"
            url=""
            handle-as="text"
            loading="{{loadingListen}}"
            last-response="{{listenLastResponse}}"
            on-request="_handleListenOpen"
            on-error="_handleListenError"
            on-response="_handleListenMessageEvent"
            debounce-duration="100"></iron-ajax>

    </template>
    <script src="channelstream-connection.js"></script>
</dom-module>