<!--

    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.

-->
<table class="tb-legend" ng-class="isRowDirection ? 'tb-legend-row' : 'tb-legend-column'"
       ng-style="{ 'marginRight': isRowDirection && !displayHeader() ? 'auto' : '20px' }">
    <thead>
        <tr class="tb-legend-header" ng-if="!isRowDirection">
            <th colspan="2"></th>
            <th ng-if="legendConfig.showMin === true">{{ 'legend.min' | translate }}</th>
            <th ng-if="legendConfig.showMax === true">{{ 'legend.max' | translate }}</th>
            <th ng-if="legendConfig.showAvg === true">{{ 'legend.avg' | translate }}</th>
            <th ng-if="legendConfig.showTotal === true">{{ 'legend.total' | translate }}</th>
        </tr>
    </thead>
    <tbody>
        <tr class="tb-legend-keys" ng-repeat="legendKey in legendData.keys"
            ng-if="!isRowDirection && legendData.keys[legendKey.dataIndex].dataKey.inLegend">
            <td><span class="tb-legend-line"  ng-style="{backgroundColor: legendKey.dataKey.color}"></span></td>
            <td class="tb-legend-label"
                ng-click="toggleHideData(legendKey.dataIndex)"
                ng-class="{ 'tb-hidden-label': legendData.keys[legendKey.dataIndex].dataKey.hidden, 'tb-horizontal': isHorizontal }">
                {{ legendKey.dataKey.label }}
            </td>
            <td class="tb-legend-value" ng-if="legendConfig.showMin === true">{{ legendData.data[legendKey.dataIndex].min }}</td>
            <td class="tb-legend-value" ng-if="legendConfig.showMax === true">{{ legendData.data[legendKey.dataIndex].max }}</td>
            <td class="tb-legend-value" ng-if="legendConfig.showAvg === true">{{ legendData.data[legendKey.dataIndex].avg }}</td>
            <td class="tb-legend-value" ng-if="legendConfig.showTotal === true">{{ legendData.data[legendKey.dataIndex].total }}</td>
        </tr>
        <tr class="tb-legend-keys" ng-class="{ 'tb-row-direction': !displayHeader() }" ng-if="isRowDirection">
            <td></td>
            <td ng-repeat="legendKey in legendData.keys" ng-if="legendData.keys[legendKey.dataIndex].dataKey.inLegend">
                <span class="tb-legend-line" ng-style="{backgroundColor: legendKey.dataKey.color}"></span>
                <span class="tb-legend-label"
                      ng-click="toggleHideData(legendKey.dataIndex)"
                      ng-class="{ 'tb-hidden-label': legendData.keys[legendKey.dataIndex].dataKey.hidden}">
                {{ legendKey.dataKey.label }}
                </span>
            </td>
        </tr>
        <tr class="tb-legend-keys" ng-if="isRowDirection && legendConfig.showMin === true">
            <td class="tb-legend-type">{{ 'legend.min' | translate }}</td>
            <td class="tb-legend-value" ng-repeat="legendKey in legendData.keys"
                ng-if="legendData.keys[legendKey.dataIndex].dataKey.inLegend">
                {{ legendData.data[legendKey.dataIndex].min }}
            </td>
        </tr>
        <tr class="tb-legend-keys" ng-if="isRowDirection && legendConfig.showMax === true">
            <td class="tb-legend-type">{{ 'legend.max' | translate }}</td>
            <td class="tb-legend-value" ng-repeat="legendKey in legendData.keys"
                ng-if="legendData.keys[legendKey.dataIndex].dataKey.inLegend">
                {{ legendData.data[legendKey.dataIndex].max }}
            </td>
        </tr>
        <tr class="tb-legend-keys" ng-if="isRowDirection && legendConfig.showAvg === true">
            <td class="tb-legend-type">{{ 'legend.avg' | translate }}</td>
            <td class="tb-legend-value" ng-repeat="legendKey in legendData.keys"
                ng-if="legendData.keys[legendKey.dataIndex].dataKey.inLegend">
                {{ legendData.data[legendKey.dataIndex].avg }}
            </td>
        </tr>
        <tr class="tb-legend-keys" ng-if="isRowDirection && legendConfig.showTotal === true">
            <td class="tb-legend-type">{{ 'legend.total' | translate }}</td>
            <td class="tb-legend-value" ng-repeat="legendKey in legendData.keys"
                ng-if="legendData.keys[legendKey.dataIndex].dataKey.inLegend">
                {{ legendData.data[legendKey.dataIndex].total }}
            </td>
        </tr>
    </tbody>
</table>
