#!/usr/bin/env python3

"""
alarm_list.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"

import sys
import os
from PySide2 import QtCore, QtGui, QtWidgets
from datetime import datetime


class AlarmList(QtWidgets.QListWidget):
    def __init__(self, NativeUI, *args, **kwargs):
        super(AlarmList, self).__init__(*args, **kwargs)
        """Display all the ongoing alarms, their timestamp, and priority level."""
        self.NativeUI = NativeUI
        self.labelList = []
        self.setSizePolicy(
            QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding
        )
        self.setStyleSheet("background-color:white;")
        self.setFont(NativeUI.text_font)

        iconpath_bell = os.path.join(NativeUI.iconpath, "bell-solid.png")
        iconpath_bellReg = os.path.join(NativeUI.iconpath, "bell-regular.png")

        self.solidBell = QtGui.QIcon(iconpath_bell)
        self.regularBell = QtGui.QIcon(iconpath_bellReg)

        newItem = QtWidgets.QListWidgetItem(" ")
        self.addItem(newItem)

    def acknowledge_all(self):
        self.NativeUI.q_send_cmd("RESET_ALARM", "TRUE")

    def addAlarm(self, abstractAlarm):
        """Use abstractAlarm object to create a new alarm in the list."""
        timestamp = str(abstractAlarm.startTime)[:-3]
        newItem = QtWidgets.QListWidgetItem(
            self.solidBell,
            timestamp
            + ": "
            + abstractAlarm.alarmPayload["alarm_type"]
            + " - "
            + abstractAlarm.alarmPayload["alarm_code"],
        )
        self.insertItem(0, newItem)  # add to the top

    def removeAlarm(self, abstractAlarm):
        """Use abstractAlarm object to remove alarm from list when it expires"""
        for x in range(self.count() - 1):
            if abstractAlarm.alarmPayload["alarm_code"] in self.item(x).text():
                self.takeItem(x)

    def localise_text(self, text: dict) -> int:
        """
        Change the language of text elements.
        """
        pass
