<!--

    Copyright © 2016-2020 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.

-->
<div flex layout="column" tb-confirm-on-exit is-dirty="vm.isDirty">
    <div flex layout="column" tb-expand-fullscreen="vm.fullscreen" hide-expand-button="true">
        <md-toolbar class="md-whiteframe-z1 tb-edit-toolbar md-hue-3">
            <div flex class="md-toolbar-tools">
                <md-input-container>
                    <label>&nbsp;</label>
                    <input ng-disabled="vm.isReadOnly()" ng-model="vm.widget.widgetName" placeholder="{{ 'widget.title' | translate }}">
                </md-input-container>
                <md-input-container>
                    <md-select ng-disabled="vm.isReadOnly()" placeholder="{{ 'widget.type' | translate }}" required id="widgetType"
                               ng-model="vm.widget.type">
                        <md-option ng-repeat="type in vm.widgetTypes" value="{{type.value}}">
                            {{ type.name | translate }}
                        </md-option>
                    </md-select>
                </md-input-container>
                <span flex=""></span>
                <md-button hide-xs hide-sm aria-label="{{ 'widget.run' | translate }}" ng-disabled="!vm.iframeWidgetEditModeInited"
                           ng-click="vm.applyWidgetScript()">
                    <md-tooltip md-direction="bottom">
                        {{ 'widget.run' | translate }} (CTRL + Return)
                    </md-tooltip>
                    <md-icon aria-label="{{ 'action.run' | translate }}">play_arrow</md-icon>
                    <span translate>action.run</span>
                </md-button>
                <md-button hide-xs hide-sm ng-disabled="vm.undoDisabled()" class="md-raised"
                           aria-label="{{ 'widget.undo' | translate }}" ng-click="vm.undoWidget()">
                    <md-tooltip md-direction="bottom">
                        {{ 'widget.undo' | translate }} (CTRL + Q)
                    </md-tooltip>
                    <md-icon aria-label="{{ 'action.undo' | translate }}">undo</md-icon>
                    <span translate>action.undo</span>
                </md-button>
                <md-button ng-if="!vm.isReadOnly()" hide-xs hide-sm ng-disabled="vm.saveDisabled()" class="md-raised"
                           aria-label="{{ 'widget.save' | translate }}" ng-click="vm.saveWidget()" tb-circular-progress="vm.saveWidgetPending">
                    <md-tooltip md-direction="bottom">
                        {{ 'widget.save' | translate }} (CTRL + S)
                    </md-tooltip>
                    <md-icon aria-label="{{ 'action.save' | translate }}">save</md-icon>
                    <span translate>action.save</span>
                </md-button>
                <md-button hide-xs hide-sm ng-disabled="vm.saveAsDisabled()" class="md-raised"
                           aria-label="{{ 'widget.saveAs' | translate }}" ng-click="vm.saveWidgetAs($event)" tb-circular-progress="vm.saveWidgetAsPending">
                    <md-tooltip md-direction="bottom">
                        {{ 'widget.saveAs' | translate }} (Shift + CTRL + S)
                    </md-tooltip>
                    <md-icon aria-label="{{ 'action.saveAs' | translate }}">save</md-icon>
                    <span translate>action.saveAs</span>
                </md-button>
                <md-button hide-xs hide-sm aria-label="{{ 'widget.toggle-fullscreen' | translate }}" ng-click="vm.toggleFullscreen()">
                    <md-tooltip md-direction="bottom">
                        {{ 'widget.toggle-fullscreen' | translate }} (Shift + CTRL + F)
                    </md-tooltip>
                    <md-icon ng-show="!vm.fullscreen" aria-label="{{ 'widget.toggle-fullscreen' | translate }}">
                        fullscreen
                    </md-icon>
                    <md-icon ng-show="vm.fullscreen" aria-label="{{ 'widget.toggle-fullscreen' | translate }}">
                        fullscreen_exit
                    </md-icon>
                    <span translate hide-xs hide-sm>widget.toggle-fullscreen</span>
                </md-button>
                <md-menu hide-gt-sm md-position-mode="target-right target">
                    <md-button class="md-icon-button"  aria-label="{{ 'home.open-user-menu' | translate }}" ng-click="$mdOpenMenu($event)">
                        <md-icon md-menu-origin aria-label="{{ 'home.open-user-menu' | translate }}" class="material-icons">more_vert</md-icon>
                    </md-button>
                    <md-menu-content width="4">
                        <md-menu-item>
                            <md-button ng-disabled="!vm.iframeWidgetEditModeInited"
                                    ng-click="vm.applyWidgetScript()">
                                <md-icon md-menu-align-target aria-label="{{ 'action.run' | translate }}" class="material-icons">play_arrow</md-icon>
                                <span translate>action.run</span>
                            </md-button>
                        </md-menu-item>
                        <md-menu-item>
                            <md-button ng-disabled="vm.undoDisabled()"
                                       ng-click="vm.undoWidget()">
                                <md-icon md-menu-align-target aria-label="{{ 'action.undo' | translate }}" class="material-icons">undo</md-icon>
                                <span translate>action.undo</span>
                            </md-button>
                        </md-menu-item>
                        <md-menu-item ng-if="!vm.isReadOnly()">
                            <md-button ng-disabled="vm.saveDisabled()"
                                    ng-click="vm.saveWidget()">
                                <md-icon md-menu-align-target aria-label="{{ 'action.save' | translate }}" class="material-icons">save</md-icon>
                                <span translate>action.save</span>
                            </md-button>
                        </md-menu-item>
                        <md-menu-item>
                            <md-button ng-disabled="vm.saveAsDisabled()"
                                       ng-click="vm.saveWidgetAs($event)">
                                <md-icon md-menu-align-target aria-label="{{ 'action.saveAs' | translate }}" class="material-icons">save</md-icon>
                                <span translate>action.saveAs</span>
                            </md-button>
                        </md-menu-item>
                    </md-menu-content>
                </md-menu>
            </div>
        </md-toolbar>
        <div flex style="position: relative;">
            <div class="tb-editor tb-absolute-fill">
                <div id="top_panel" class="tb-split tb-split-vertical">
                    <div id="top_left_panel" class="tb-split tb-content">
                        <md-tabs md-selected="1" md-dynamic-height md-border-bottom class="container">
                            <md-tab label="{{ 'widget.resources' | translate }}">
                                <div class="tb-resize-container" style="background-color: #fff;">
                                    <div class="md-padding">
                                        <div flex layout="row"
                                             ng-repeat="resource in vm.widget.resources track by $index"
                                             style="max-height: 40px;" layout-align="start center">
                                            <md-input-container flex md-no-float class="md-block"
                                                                style="margin: 10px 0 0; max-height: 40px;">
                                                <input placeholder="{{ 'widget.resource-url' | translate }}"
                                                       ng-required="true" name="resource" ng-model="resource.url">
                                            </md-input-container>
                                            <md-button ng-disabled="$root.loading" class="md-icon-button md-primary"
                                                       ng-click="vm.removeResource($index)"
                                                       aria-label="{{ 'action.remove' | translate }}">
                                                <md-tooltip md-direction="top">
                                                    {{ 'widget.remove-resource' | translate }}
                                                </md-tooltip>
                                                <md-icon aria-label="{{ 'action.delete' | translate }}"
                                                         class="material-icons">
                                                    close
                                                </md-icon>
                                            </md-button>
                                        </div>
                                        <div>
                                            <md-button ng-disabled="$root.loading" class="md-primary md-raised"
                                                       ng-click="vm.addResource()"
                                                       aria-label="{{ 'action.add' | translate }}">
                                                <md-tooltip md-direction="top">
                                                    {{ 'widget.add-resource' | translate }}
                                                </md-tooltip>
                                                <span translate>action.add</span>
                                            </md-button>
                                        </div>
                                    </div>
                                </div>
                            </md-tab>
                            <md-tab label="{{ 'widget.html' | translate }}">
                                <div class="tb-resize-container" tb-expand-fullscreen expand-button-id="expand-button">
                                    <div class="tb-editor-area-title-panel">
                                        <md-button aria-label="{{ 'widget.tidy' | translate }}"
                                                   ng-click="vm.beautifyHtml()">{{ 'widget.tidy' | translate }}
                                        </md-button>
                                        <md-button id="expand-button"
                                                   aria-label="{{ 'fullscreen.fullscreen' | translate }}"
                                                   class="md-icon-button tb-md-32"></md-button>
                                    </div>
                                    <div id="html_input" ui-ace="vm.htmlEditorOptions"
                                         ng-model="vm.widget.templateHtml"></div>
                                </div>
                            </md-tab>
                            <md-tab label="{{ 'widget.css' | translate }}">
                                <div class="tb-resize-container" tb-expand-fullscreen expand-button-id="expand-button">
                                    <div class="tb-editor-area-title-panel">
                                        <md-button aria-label="{{ 'widget.tidy' | translate }}"
                                                   ng-click="vm.beautifyCss()">{{ 'widget.tidy' | translate }}
                                        </md-button>
                                        <md-button id="expand-button"
                                                   aria-label="{{ 'fullscreen.fullscreen' | translate }}"
                                                   class="md-icon-button tb-md-32"></md-button>
                                    </div>
                                    <div id="css_input" ui-ace="vm.cssEditorOptions"
                                         ng-model="vm.widget.templateCss"></div>
                                </div>
                            </md-tab>
                        </md-tabs>
                    </div>
                    <div id="top_right_panel" class="tb-split tb-content">
                        <md-tabs md-dynamic-height md-border-bottom class="container">
                            <md-tab label="{{ 'widget.settings-schema' | translate }}">
                                <div class="tb-resize-container" tb-expand-fullscreen expand-button-id="expand-button">
                                    <div class="tb-editor-area-title-panel">
                                        <md-button aria-label="{{ 'widget.tidy' | translate }}"
                                                   ng-click="vm.beautifyJson()">{{ 'widget.tidy' | translate }}
                                        </md-button>
                                        <md-button id="expand-button"
                                                   aria-label="{{ 'fullscreen.fullscreen' | translate }}"
                                                   class="md-icon-button tb-md-32"></md-button>
                                    </div>
                                    <div id="settings_json_input" ui-ace="vm.jsonSettingsEditorOptions"
                                         ng-model="vm.widget.settingsSchema"></div>
                                </div>
                            </md-tab>
                            <md-tab label="{{ 'widget.datakey-settings-schema' | translate }}">
                                <div class="tb-resize-container" tb-expand-fullscreen expand-button-id="expand-button">
                                    <div class="tb-editor-area-title-panel">
                                        <md-button aria-label="{{ 'widget.tidy' | translate }}"
                                                   ng-click="vm.beautifyDataKeyJson()">{{ 'widget.tidy' | translate }}
                                        </md-button>
                                        <md-button id="expand-button"
                                                   aria-label="{{ 'fullscreen.fullscreen' | translate }}"
                                                   class="md-icon-button tb-md-32"></md-button>
                                    </div>
                                    <div id="settings_json_input" ui-ace="vm.dataKeyJsonSettingsEditorOptions"
                                         ng-model="vm.widget.dataKeySettingsSchema"></div>
                                </div>
                            </md-tab>
                        </md-tabs>
                    </div>
                </div>
                <div id="bottom_panel" class="tb-split tb-split-vertical">
                    <div id="javascript_panel" class="tb-split tb-content">
                        <div class="tb-resize-container" tb-expand-fullscreen expand-button-id="expand-button">
                            <div class="tb-editor-area-title-panel">
                                <label translate for="javascript_input">widget.javascript</label>
                                <md-button aria-label="{{ 'widget.tidy' | translate }}" ng-click="vm.beautifyJs()">{{
                                    'widget.tidy' | translate }}
                                </md-button>
                                <md-button id="expand-button" aria-label="{{ 'fullscreen.fullscreen' | translate }}"
                                           class="md-icon-button tb-md-32"></md-button>
                            </div>
                            <div id="javascript_input" ui-ace="vm.jsEditorOptions"
                                 ng-model="vm.widget.controllerScript"></div>
                        </div>
                    </div>
                    <div id="frame_panel" class="tb-split tb-content" style="overflow-y: hidden; position: relative;">
                        <md-content flex layout="column" class="tb-progress-cover" layout-align="center center"
                                    ng-show="!vm.iframeWidgetEditModeInited">
                            <md-progress-circular ng-disabled="vm.iframeWidgetEditModeInited" md-mode="indeterminate" class="md-warn"
                                                  md-diameter="100"></md-progress-circular>
                        </md-content>
                        <div tb-expand-fullscreen expand-button-id="expand-button" class="container">
                            <iframe frameborder="0" height="100%" width="100%"></iframe>
                            <md-button id="expand-button" aria-label="Fullscreen"
                                       class="md-icon-button tb-fullscreen-button-style"
                                       style="position: absolute; top: 10px; left: 10px; bottom: initial;"
                            ></md-button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <md-content flex layout="column" class="tb-progress-cover" layout-align="center center" ng-show="!vm.layoutInited">
        <md-progress-circular md-mode="indeterminate" ng-disabled="vm.layoutInited" class="md-warn" md-diameter="100"></md-progress-circular>
    </md-content>
</div>
