#!/usr/bin/python3 -su

'''
alert - Displays a dialog message box containing text from a YAML configuration
        file

Copyright (C) 2014 - 2015 Jason Mehring <nrgaway@gmail.com>
License: GPL-2+
Authors: Jason Mehring

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

import sys
import signal
import argparse
import locale
import yaml

from PyQt5 import QtCore, QtGui, QtWidgets

DEFAULT_LANG = 'en'


class Messages():
    filename = None
    data = None
    language = DEFAULT_LANG
    title = None
    icon = None
    message = None

    def __init__(self, section, filename):
        self.filename = filename

        try:
            language = locale.getlocale()[0].split('_')[0]
        except:
            language = DEFAULT_LANG

        self.language = language

        try:
            stream = open(filename, 'r')
            data = yaml.load(stream, Loader=yaml.FullLoader)

            if section in data.keys():
                section = data[section]

                self.icon = section.get('icon', None)

                language = section.get(self.language, DEFAULT_LANG)

                self.title = language.get('title', None)
                self.message = language.get('message', None)
            else:
                self.title = "alert script by msgcollector"
                self.message = "section " + section + " not found! [...............................]"

        except:
            error_msg = "Unexpected error: " + str(sys.exc_info()[0])
            print(error_msg)
            self.title = "alert script by msgcollector"
            self.message = error_msg



class AlertMessageBox(QtWidgets.QMessageBox):
    def __init__(self, message):
        super(AlertMessageBox, self).__init__()
        self.message = message
        self.initUI()

    def initUI(self):
        message = self.message

        if message.title:
            self.setWindowTitle(message.title)

        if message.icon:
            self.setIcon(getattr(QtWidgets.QMessageBox, message.icon))

        if message.message:
            self.setText(message.message)
            self.exec_()


def signal_handler(sig, frame):
    sys.exit(128 + sig)


def main():
    parser = argparse.ArgumentParser(description='Display a QT Message Box')

    parser.add_argument('section', help="Message section")
    parser.add_argument('filename', help="File including full path")

    args = parser.parse_args()

    if not args.filename and args.section:
        parser.usage()
        sys.exit(1)

    app = QtWidgets.QApplication(sys.argv)

    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM, signal_handler)

    timer = QtCore.QTimer()
    timer.start(500)
    timer.timeout.connect(lambda: None)

    message = Messages(args.section, args.filename)
    dialog = AlertMessageBox(message)
    sys.exit()

if __name__ == "__main__":
    main()
