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

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

<link rel="import" href="../../bower_components/strolch-wc-auth/strolch-wc-auth.html">
<link rel="import" href="../../bower_components/strolch-wc-inspector/strolch-wc-inspector.html">
<link rel="import" href="../../bower_components/strolch-wc-inspector/strolch-wc-users.html">
<link rel="import" href="../../bower_components/strolch-wc-inspector/strolch-wc-roles.html">
<link rel="import" href="../../bower_components/strolch-wc-inspector/strolch-wc-sessions.html">
<link rel="import" href="../../bower_components/strolch-wc-inspector/strolch-wc-operations-log.html">
<link rel="import" href="../../bower_components/strolch-wc-inspector/strolch-wc-jobs.html">
<link rel="import" href="../../bower_components/strolch-wc-inspector/strolch-wc-operations-log.html">
<link rel="import" href="../../bower_components/strolch-wc-inspector/strolch-wc-inspector-menu.html">
<link rel="import" href="../../bower_components/strolch-wc-inspector/strolch-wc-control.html">
<link rel="import" href="../../bower_components/strolch-wc-inspector/strolch-wc-i18n-editor.html">
<link rel="import" href="../../bower_components/strolch-wc-inspector/strolch-wc-components.html">
<link rel="import" href="../../bower_components/strolch-wc-reports/strolch-wc-reports.html">

<link rel="import" href="../behaviors/c-app-behavior.html">
<link rel="import" href="../styles/c-app-style.html">

<link rel="import" href="../views/c-view404.html">
<link rel="import" href="../views/c-demo-locations.html">
<link rel="import" href="../views/c-demo-books.html">

<dom-module id="c-pages">
    <template>
        <style is="custom-style" include="c-app-style">
            :root {
                display: block;
            }
        </style>

        <iron-pages selected="[[selectedPage]]" attr-for-selected="id" fallback-selection="view404">

            <strolch-wc-auth id="login"
                             app-title="[[localize('appTitle')]]"
                             toolbar-config="{{toolbarConfig}}"
                             base-path="../"
                             show-version
                             show-keep-alive></strolch-wc-auth>

            <c-demo-locations id="demo-locations"
                    toolbar-config="{{toolbarConfig}}"
                    search-term="[[searchTerm]]"
                    base-path="../"></c-demo-locations>
            <c-demo-books id="demo-books"
                    toolbar-config="{{toolbarConfig}}"
                    search-term="[[searchTerm]]"
                    base-path="../"></c-demo-books>

            <template is="dom-if" if="[[equal(page, 'inspector')]]" restamp>
                <strolch-wc-inspector id="inspector"
                                      toolbar-config="{{toolbarConfig}}"
                                      base-path="../"
                                      route="{{route}}"></strolch-wc-inspector>
            </template>
            <template is="dom-if" if="[[equal(page, 'agent-components')]]" restamp>
                <strolch-wc-components id="agent-components"
                                       base-path="../"
                                       base-rest-path="[[baseRestPath]]"
                                       toolbar-config="{{toolbarConfig}}"
                                       search-term="{{searchTerm}}"
                                       user-location="[[userLocation]]"
                                       route="{{routeTail}}"
                                       propagate-show-dialog></strolch-wc-components>
            </template>

            <template is="dom-if" if="[[equal(page, 'operations-log')]]" restamp>
                <strolch-wc-operations-log id="operations-log"
                                           toolbar-config="{{toolbarConfig}}"
                                           base-path="../"
                                           route="{{route}}"></strolch-wc-operations-log>
            </template>

            <template is="dom-if" if="[[equal(page, 'jobs')]]" restamp>
                <strolch-wc-jobs id="jobs"
                                 toolbar-config="{{toolbarConfig}}"
                                 base-path="../"
                                 route="{{route}}"></strolch-wc-jobs>
            </template>

            <template is="dom-if" if="[[equal(page, 'reports')]]" restamp>
                <strolch-wc-reports id="reports"
                                    toolbar-config="{{toolbarConfig}}"
                                    base-path="../"
                                    base-rest-path="[[baseRestPath]]"
                                    locales-path="../../../locales.json"
                                    route="{{route}}"
                                    facet-limit="[[reportFacetLimit]]"
                                    propagate-show-dialog></strolch-wc-reports>
            </template>

            <template is="dom-if" if="[[equal(page, 'users')]]" restamp>
                <strolch-wc-users id="users"
                                  toolbar-config="{{toolbarConfig}}"
                                  base-path="../"
                                  route="{{route}}"></strolch-wc-users>
            </template>

            <template is="dom-if" if="[[equal(page, 'roles')]]" restamp>
                <strolch-wc-roles id="roles"
                                  toolbar-config="{{toolbarConfig}}"
                                  base-path="../"
                                  route="{{route}}"></strolch-wc-roles>
            </template>

            <template is="dom-if" if="[[equal(page, 'sessions')]]" restamp>
                <strolch-wc-sessions id="sessions"
                                     toolbar-config="{{toolbarConfig}}"
                                     base-path="../"
                                     route="{{route}}"></strolch-wc-sessions>
            </template>

            <template is="dom-if" if="[[equal(page, 'control')]]" restamp>
                <strolch-wc-control id="control"
                                    class="padded-view"
                                    base-path="../"
                                    base-ws-path="[[baseWsPath]]"
                                    toolbar-config="{{toolbarConfig}}"
                                    search-term="{{searchTerm}}"
                                    user-location="{{userLocation}}"
                                    route="{{routeTail}}"
                                    propagate-show-dialog></strolch-wc-control>
            </template>

            <c-view404 id="view404"></c-view404>

        </iron-pages>

    </template>

    <script>

        Polymer({

            is: 'c-pages',

            behaviors: [
                CustomAppBehavior
            ],

            properties: {
                toolbarConfig: {
                    type: Object,
                    notify: true
                },
                searchTerm: {
                    type: String
                },
                page: {
                    type: String,
                    observer: 'observePage'
                },
                selectedPage: {
                    type: String,
                    observer: 'observeSelectedPage'
                },
                route: {
                    type: Object
                },
                reportFacetLimit: {
                    type: Number,
                    value: function () {
                        return 25;
                    }
                }
            },

            observers: [],

            listeners: {},

            observePage: function (newValue, oldValue) {
                if (newValue) {
                    this.async(function () {
                        this.selectedPage = newValue;
                    }, 10);
                }
            },
            observeSelectedPage: function (newValue, oldValue) {
                if (newValue) {
                    console.log("Page changed to " + newValue + ", reloading...");
                    this.reloadPage(newValue);
                }
            },

            logout: function () {
                this.$$('#login').logout();
            }
        });

    </script>

</dom-module>
