#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
main_window

The main window and system tray for the commotion_client pyqt GUI.

Key componenets handled within:
 * exiting/hiding the application
 * Creating the main window and systemTray icon.

"""
#Standard Library Imports
import logging

#PyQt imports
from PyQt4 import QtCore
from PyQt4 import QtGui

#Commotion Client Imports
import commotion_assets_rc
from commotion_client.GUI.menu_bar import MenuBar
from commotion_client.GUI.crash_report import CrashReport
from commotion_client.GUI import welcome_page
from commotion_client.GUI import toolbar_builder
from commotion_client.utils import extension_manager


class MainWindow(QtGui.QMainWindow):
    """
    The central widget for the commotion client. This widget initalizes all other sub-widgets and extensions as well as defines the paramiters of the main GUI container.
    """
    
    #Clean up signal atched by children to do any clean-up or saving needed
    clean_up = QtCore.pyqtSignal()
    app_message = QtCore.pyqtSignal(str)

    def __init__(self, parent=None):
        super().__init__()
        #Keep track of if the gui needs any clean up / saving.
        self._dirty = False
        self.log = logging.getLogger("commotion_client."+__name__)
        self.translate = QtCore.QCoreApplication.translate

        self.init_crash_reporter()
        self.setup_menu_bar()
        #Setup extension manager for viewports
        self.ext_manager = extension_manager.ExtensionManager()
        self.viewport = welcome_page.ViewPort
        self.apply_viewport(self.viewport)
        
        #Default Paramiters #TODO to be replaced with paramiters saved between instances later
        try:
            self.load_settings()
        except Exception as _excp:
            self.log.critical(self.translate("logs", "Failed to load window settings."))