# Copyright 2018 Jetperch LLC
#
# 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.

from PySide2 import QtCore, QtWidgets
from . import joulescope_rc
from .meter_value_widget import MeterValueWidget
import logging
log = logging.getLogger(__name__)


FIELDS = [
    ('current', 'A', 'Amps'),
    ('voltage', 'V', 'Volts'),
    ('power', 'W', 'Watts'),
    ('energy', 'J', 'Joules'),
]


class MeterWidget(QtWidgets.QWidget):

    def __init__(self, *args, **kwargs):
        QtWidgets.QWidget.__init__(self, *args, **kwargs)
        self.verticalLayout = QtWidgets.QVBoxLayout(self)
        self.verticalLayout.setObjectName("verticalLayout")
        self.verticalLayout.setSpacing(0)

        self.controlWidget = QtWidgets.QWidget(self)
        self.controlLayout = QtWidgets.QHBoxLayout(self.controlWidget)
        self.verticalLayout.addWidget(self.controlWidget)

        self.accumulateButton = QtWidgets.QPushButton(self.controlWidget)
        self.accumulateButton.setCheckable(True)
        self.accumulateButton.setObjectName("accumulateButton")
        self.controlLayout.addWidget(self.accumulateButton)
        self.accumulateButton.toggled.connect(self.on_accumulate_toggled)

        self.controlSpacer = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.controlLayout.addItem(self.controlSpacer)

        self.values = {}
        for name, units_short, units_long in FIELDS:
            w = MeterValueWidget(self)
            w.setStyleSheet("QWidget { background-color : black; color : green; }")
            w.configure(name.capitalize(), units_short, units_long)
            self.values[name] = w
            w.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout.addWidget(w)
        self.values['energy'].configure_energy()

        self.sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        self.sizePolicy.setHorizontalStretch(0)
        self.sizePolicy.setVerticalStretch(0)
        self.setSizePolicy(self.sizePolicy)
        self.retranslateUi()

    @QtCore.Slot(bool)
    def on_accumulate_toggled(self, checked):
        self.values['current'].accumulate_enable = checked
        self.values['voltage'].accumulate_enable = checked
        self.values['power'].accumulate_enable = checked

    def update(self, statistics):
        """Update the multimeter display

        :param statistics: The statistics data structure
        """
        for name, field in statistics['signals'].items():
            d = field['statistics']
            self.values[name].update_value(mean=d['μ'], variance=d['σ2'], v_min=d['min'], v_max=d['max'])
        energy = statistics['accumulators']['energy']['value']
        charge = statistics['accumulators']['charge']['value']
        self.values['energy'].update_energy(energy, charge)

    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.accumulateButton.setText(_translate("meter_widget", "Accumulate"))

[((19, 6, 19, 33), 'logging.getLogger', 'logging.getLogger', ({(19, 24, 19, 32): '__name__'}, {}), '(__name__)', False, 'import logging\n'), ((67, 5, 67, 22), 'PySide2.QtCore.Slot', 'QtCore.Slot', ({(67, 17, 67, 21): 'bool'}, {}), '(bool)', False, 'from PySide2 import QtCore, QtWidgets\n'), ((33, 8, 33, 57), 'PySide2.QtWidgets.QWidget.__init__', 'QtWidgets.QWidget.__init__', ({(33, 35, 33, 39): 'self', (33, 41, 33, 46): '*args'}, {}), '(self, *args, **kwargs)', False, 'from PySide2 import QtCore, QtWidgets\n'), ((34, 30, 34, 57), 'PySide2.QtWidgets.QVBoxLayout', 'QtWidgets.QVBoxLayout', ({(34, 52, 34, 56): 'self'}, {}), '(self)', False, 'from PySide2 import QtCore, QtWidgets\n'), ((38, 29, 38, 52), 'PySide2.QtWidgets.QWidget', 'QtWidgets.QWidget', ({(38, 47, 38, 51): 'self'}, {}), '(self)', False, 'from PySide2 import QtCore, QtWidgets\n'), ((39, 29, 39, 70), 'PySide2.QtWidgets.QHBoxLayout', 'QtWidgets.QHBoxLayout', ({(39, 51, 39, 69): 'self.controlWidget'}, {}), '(self.controlWidget)', False, 'from PySide2 import QtCore, QtWidgets\n'), ((42, 32, 42, 73), 'PySide2.QtWidgets.QPushButton', 'QtWidgets.QPushButton', ({(42, 54, 42, 72): 'self.controlWidget'}, {}), '(self.controlWidget)', False, 'from PySide2 import QtCore, QtWidgets\n'), ((48, 29, 48, 122), 'PySide2.QtWidgets.QSpacerItem', 'QtWidgets.QSpacerItem', ({(48, 51, 48, 53): '40', (48, 55, 48, 57): '20', (48, 59, 48, 90): 'QtWidgets.QSizePolicy.Expanding', (48, 92, 48, 121): 'QtWidgets.QSizePolicy.Minimum'}, {}), '(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.\n    QSizePolicy.Minimum)', False, 'from PySide2 import QtCore, QtWidgets\n'), ((61, 26, 61, 113), 'PySide2.QtWidgets.QSizePolicy', 'QtWidgets.QSizePolicy', ({(61, 48, 61, 79): 'QtWidgets.QSizePolicy.Expanding', (61, 81, 61, 112): 'QtWidgets.QSizePolicy.Expanding'}, {}), '(QtWidgets.QSizePolicy.Expanding, QtWidgets.\n    QSizePolicy.Expanding)', False, 'from PySide2 import QtCore, QtWidgets\n')]