<!--

    Copyright © 2016-2018 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->

<md-content flex tb-expand-fullscreen tb-confirm-on-exit is-dirty="vm.isConfirmOnExit"
            expand-tooltip-direction="bottom" layout="column" class="tb-rulechain"
            ng-keydown="vm.keyDown($event)"
            ng-keyup="vm.keyUp($event)" on-fullscreen-changed="vm.isFullscreen = expanded">
    <section class="tb-rulechain-container" flex layout="column">
        <div class="tb-rulechain-layout" flex layout="row">
            <section layout="row" layout-wrap
                     class="tb-header-buttons md-fab tb-library-open">
                <md-button ng-show="!vm.isLibraryOpen"
                           class="tb-btn-header tb-btn-open-library md-primary md-fab md-fab-top-left"
                           aria-label="{{ 'rulenode.open-node-library' | translate }}"
                           ng-click="vm.isLibraryOpen = true">
                    <md-tooltip md-direction="{{vm.isFullscreen ? 'bottom' : 'top'}}">
                        {{ 'rulenode.open-node-library' | translate }}
                    </md-tooltip>
                    <ng-md-icon icon="menu"></ng-md-icon>
                </md-button>
            </section>
            <md-sidenav class="tb-rulechain-library md-sidenav-left md-whiteframe-4dp"
                        md-disable-backdrop
                        md-is-locked-open="vm.isLibraryOpen"
                        md-is-open="vm.isLibraryOpen"
                        md-component-id="rulechain-library-sidenav" layout="column">
                <md-toolbar>
                    <div class="md-toolbar-tools">
                        <md-button class="md-icon-button tb-small" aria-label="{{ 'action.search' | translate }}">
                            <md-icon aria-label="{{ 'action.search' | translate }}" class="material-icons">search</md-icon>
                            <md-tooltip md-direction="{{vm.isFullscreen ? 'bottom' : 'top'}}">
                                {{'rulenode.search' | translate}}
                            </md-tooltip>
                        </md-button>
                        <div layout="row" md-theme="tb-dark" flex>
                            <md-input-container flex>
                                <label>&nbsp;</label>
                                <input ng-model="vm.ruleNodeSearch" placeholder="{{'rulenode.search' | translate}}"/>
                            </md-input-container>
                        </div>
                        <md-button class="md-icon-button tb-small" aria-label="Close"
                                   ng-show="vm.ruleNodeSearch"
                                   ng-click="vm.ruleNodeSearch = ''">
                            <md-icon aria-label="Close" class="material-icons">close</md-icon>
                            <md-tooltip md-direction="{{vm.isFullscreen ? 'bottom' : 'top'}}">
                                {{ 'action.clear-search' | translate }}
                            </md-tooltip>
                        </md-button>
                        <md-button class="md-icon-button tb-small" aria-label="Close" ng-click="vm.isLibraryOpen = false">
                            <md-icon aria-label="Close" class="material-icons">chevron_left</md-icon>
                            <md-tooltip md-direction="{{vm.isFullscreen ? 'bottom' : 'top'}}">
                                {{ 'action.close' | translate }}
                            </md-tooltip>
                        </md-button>
                    </div>
                </md-toolbar>
                <md-expansion-panel-group flex
                                          ng-if="vm.ruleChainLibraryLoaded" class="tb-rulechain-library-panel-group"
                                          md-component-id="libraryPanelGroup" auto-expand="true" multiple>
                    <md-expansion-panel md-component-id="{{typeId}}" id="{{typeId}}" ng-repeat="(typeId, typeModel) in vm.ruleNodeTypesModel">
                        <md-expansion-panel-collapsed ng-mouseenter="vm.typeHeaderMouseEnter($event, typeId)"
                                                      ng-mouseleave="vm.destroyTooltips()">
                            <div class="tb-panel-title" translate>{{vm.types.ruleNodeType[typeId].name}}</div>
                            <md-expansion-panel-icon></md-expansion-panel-icon>
                        </md-expansion-panel-collapsed>
                        <md-expansion-panel-expanded>
                            <md-expansion-panel-header ng-mouseenter="vm.typeHeaderMouseEnter($event, typeId)"
                                                       ng-mouseleave="vm.destroyTooltips()"
                                                       ng-click="vm.$mdExpansionPanel(typeId).collapse()">
                                <div class="tb-panel-title" translate>{{vm.types.ruleNodeType[typeId].name}}</div>
                                <md-expansion-panel-icon></md-expansion-panel-icon>
                            </md-expansion-panel-header>
                            <md-expansion-panel-content>
                                <fc-canvas id="tb-rulechain-{{typeId}}"
                                           model="vm.ruleNodeTypesModel[typeId].model" selected-objects="vm.ruleNodeTypesModel[typeId].selectedObjects"
                                           automatic-resize="false"
                                           callbacks="vm.nodeLibCallbacks"
                                           node-width="170"
                                           node-height="50"
                                           control="vm.ruleNodeTypesCanvasControl[typeId]"
                                           drop-target-id="'tb-rulchain-canvas'"></fc-canvas>
                            </md-expansion-panel-content>
                        </md-expansion-panel-expanded>
                    </md-expansion-panel>
                </md-expansion-panel-group>
            </md-sidenav>
            <md-menu flex style="position: relative;" md-position-mode="target target" tb-offset-x="-20" tb-offset-y="-45" tb-mousepoint-menu>
                <div class="tb-absolute-fill tb-rulechain-graph" ng-click="" tb-contextmenu="vm.openRuleChainContextMenu($event, $mdOpenMousepointMenu)">
                    <fc-canvas id="tb-rulchain-canvas"
                               model="vm.ruleChainModel"
                               selected-objects="vm.selectedObjects"
                               edge-style="curved"
                               node-width="170"
                               node-height="50"
                               automatic-resize="true"
                               control="vm.canvasControl"
                               callbacks="vm.editCallbacks">
                    </fc-canvas>
                </div>
                <md-menu-content id="tb-rule-chain-context-menu" width="4" ng-mouseleave="$mdCloseMousepointMenu()">
                    <div class="tb-context-menu-header {{vm.contextInfo.headerClass}}">
                        <md-icon aria-label="node-type-icon"
                                 class="material-icons">{{vm.contextInfo.icon}}</md-icon>
                        <div flex>
                            <div class="tb-context-menu-title">{{vm.contextInfo.title}}</div>
                            <div class="tb-context-menu-subtitle">{{vm.contextInfo.subtitle}}</div>
                        </div>
                    </div>
                    <div ng-repeat="item in vm.contextInfo.items">
                        <md-menu-item>
                            <md-button ng-disabled="!item.enabled" ng-click="item.action(vm.contextMenuEvent)">
                                <md-icon ng-if="item.icon" md-menu-align-target aria-label="{{ item.value | translate }}" class="material-icons">{{item.icon}}</md-icon>
                                <span translate>{{item.value}}</span>
                                <span ng-if="item.shortcut" class="tb-alt-text"> {{ item.shortcut | keyboardShortcut }}</span>
                            </md-button>
                        </md-menu-item>
                    </div>
                </md-menu-content>
            </md-menu>
        </div>
        <tb-details-sidenav class="tb-rulenode-details-sidenav"
                            header-title="{{vm.editingRuleNode.name}}"
                            header-subtitle="{{(vm.types.ruleNodeType[vm.editingRuleNode.component.type].name | translate)
                            + ' - ' + vm.editingRuleNode.component.name}}"
                            is-read-only="vm.selectedRuleNodeTabIndex > 0"
                            is-open="vm.isEditingRuleNode"
                            tb-enable-backdrop
                            is-always-edit="true"
                            on-close-details="vm.onEditRuleNodeClosed()"
                            on-toggle-details-edit-mode="vm.onRevertRuleNodeEdit(vm.ruleNodeForm)"
                            on-apply-details="vm.saveRuleNode(vm.ruleNodeForm)"
                            the-form="vm.ruleNodeForm">
            <details-buttons tb-help="vm.helpLinkIdForRuleNodeType()" help-container-id="help-container">
                <div id="help-container"></div>
            </details-buttons>
            <md-tabs md-selected="vm.selectedRuleNodeTabIndex"
                     id="ruleNodeTabs" md-border-bottom flex class="tb-absolute-fill" ng-if="vm.isEditingRuleNode">
                <md-tab label="{{ 'rulenode.details' | translate }}">
                    <form name="vm.ruleNodeForm">
                        <tb-rule-node
                                rule-node="vm.editingRuleNode"
                                rule-chain-id="vm.ruleChain.id.id"
                                is-edit="true"
                                is-read-only="false"
                                on-delete-rule-node="vm.deleteRuleNode(event, vm.editingRuleNode)"
                                the-form="vm.ruleNodeForm">
                        </tb-rule-node>
                    </form>
                </md-tab>
                <md-tab ng-if="vm.isEditingRuleNode && vm.editingRuleNode.ruleNodeId"
                        md-on-select="vm.triggerResize()" label="{{ 'rulenode.events' | translate }}">
                    <tb-event-table flex entity-type="vm.types.entityType.rulenode"
                                    entity-id="vm.editingRuleNode.ruleNodeId.id"
                                    tenant-id="vm.ruleChain.tenantId.id"
                                    debug-event-types="{{vm.types.debugEventType.debugRuleNode.value}}"
                                    default-event-type="{{vm.types.debugEventType.debugRuleNode.value}}">
                    </tb-event-table>
                </md-tab>
            </md-tabs>
        </tb-details-sidenav>
        <tb-details-sidenav class="tb-rulenode-link-details-sidenav"
                            header-title="{{vm.editingRuleNodeLink.label}}"
                            header-subtitle="{{'rulenode.link-details' | translate}}"
                            is-read-only="false"
                            is-open="vm.isEditingRuleNodeLink"
                            tb-enable-backdrop
                            is-always-edit="true"
                            on-close-details="vm.onEditRuleNodeLinkClosed()"
                            on-toggle-details-edit-mode="vm.onRevertRuleNodeLinkEdit(vm.ruleNodeLinkForm)"
                            on-apply-details="vm.saveRuleNodeLink(vm.ruleNodeLinkForm)"
                            the-form="vm.ruleNodeLinkForm">
            <details-buttons tb-help="vm.helpLinkIdForRuleNodeLink()" help-container-id="link-help-container">
                <div id="link-help-container"></div>
            </details-buttons>
            <form name="vm.ruleNodeLinkForm" ng-if="vm.isEditingRuleNodeLink">
                <tb-rule-node-link
                        link="vm.editingRuleNodeLink"
                        labels="vm.editingRuleNodeLinkLabels"
                        is-edit="true"
                        is-read-only="false"
                        the-form="vm.ruleNodeLinkForm">
                </tb-rule-node-link>
            </form>
        </tb-details-sidenav>
    </section>
    <section layout="row" layout-wrap class="tb-footer-buttons md-fab" layout-align="start end">
        <md-button ng-disabled="$root.loading" ng-show="vm.objectsSelected()" class="tb-btn-footer md-accent md-hue-2 md-fab"
                   ng-click="vm.deleteSelected()" aria-label="{{ 'action.delete' | translate }}">
            <md-tooltip md-direction="top">
                {{ 'rulenode.delete-selected-objects' | translate }}
            </md-tooltip>
            <ng-md-icon icon="delete"></ng-md-icon>
        </md-button>
        <md-button ng-disabled="$root.loading  || vm.isInvalid || (!vm.isDirty && !vm.isImport)"
                   class="tb-btn-footer md-accent md-hue-2 md-fab"
                   aria-label="{{ 'action.apply' | translate }}"
                   ng-click="vm.saveRuleChain()">
            <md-tooltip md-direction="top">
                {{ 'action.apply-changes' | translate }}
            </md-tooltip>
            <ng-md-icon icon="done"></ng-md-icon>
        </md-button>
        <md-button ng-disabled="$root.loading || !vm.isDirty"
                   class="tb-btn-footer md-accent md-hue-2 md-fab"
                   aria-label="{{ 'action.decline-changes' | translate }}"
                   ng-click="vm.revertRuleChain()">
            <md-tooltip md-direction="top">
                {{ 'action.decline-changes' | translate }}
            </md-tooltip>
            <ng-md-icon icon="close"></ng-md-icon>
        </md-button>
    </section>
</md-content>
