#!/usr/bin/env python3

"""
startup_calibration_widget.py
"""

__author__ = ["Benjamin Mummery", "Tiago Sarmento"]
__credits__ = ["Benjamin Mummery", "Dónal Murray", "Tim Powell", "Tiago Sarmento"]
__license__ = "GPL"
__version__ = "0.0.1"
__maintainer__ = "Tiago Sarmento"
__email__ = "tiago.sarmento@stfc.ac.uk"
__status__ = "Prototype"

from PySide2 import QtWidgets, QtGui, QtCore
from datetime import datetime
import os
from widget_library.localised_base_widgets import LocalisedQPushButton
import logging


class calibrationWidget(QtWidgets.QWidget):
    def __init__(self, NativeUI, key, infoDict, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.NativeUI = NativeUI
        self.key = key
        self.infoDict = infoDict
        hlayout = QtWidgets.QHBoxLayout()

        self.button = LocalisedQPushButton(NativeUI, infoDict["label"])
        hlayout.addWidget(self.button)

        self.progBar = QtWidgets.QProgressBar()
        self.progBar.setStyleSheet(
            "QProgressBar{"
            "   background-color:"
            + NativeUI.colors["button_background_enabled"].name()
            + ";"
            "   color:" + NativeUI.colors["page_foreground"].name() + ";"
            "   text-align: center;"
            "}"
            "QProgressBar::chunk{"
            "   background-color:" + NativeUI.colors["highlight"].name() + ";"
            "}"
        )
        hlayout.addWidget(self.progBar)

        self.lastTime = datetime.fromtimestamp(infoDict["last_performed"])
        self.lastTime.strftime("%d-%m-%y %H:%M")
        self.lineEdit = QtWidgets.QLineEdit(str(self.lastTime))
        hlayout.addWidget(self.lineEdit)

        self.setLayout(hlayout)

    def localise_text(self, text: dict) -> int:
        self.button.localise_text(text)
        return 0

    def setFont(self, font) -> int:
        for widget in [self.button, self.progBar, self.lineEdit]:
            widget.setFont(font)

    def set_size(self, x: int = None, y: int = None, spacing: int = 10) -> int:
        widgets = [self.button, self.progBar, self.lineEdit]
        if x is not None and y is not None:
            self.setFixedSize(x, y)
            for widget in widgets:
                widget.setFixedSize(int(x / 3 - spacing), y)
        elif x is not None and y is None:
            self.setFixedWidth(x)
        elif x is None and y is not None:
            self.setFixedHeight(y)
            for widget in widgets:
                widget.setFixedHeight(y)
        else:
            logging.error("set_size called with incomplete information")
        return 0
