#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2016 Continuum Analytics, Inc.
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
#

"""
Application entry point.
"""

# Standard library imports
import sys

# Third party imports
from qtpy.QtCore import QObject

# Local imports
from anaconda_navigator.fonts import load_fonts
from anaconda_navigator.utils.qthelpers import qapplication
from anaconda_navigator.widgets.main_window import MainWindow
from anaconda_navigator.widgets.splash import SplashScreen


MAC = sys.platform == 'darwin'


def run_app(splash):
    """
    Create and show Navigator's main window.
    """
    window = MainWindow(splash=splash)
    window.setup()
    return window


class EventEater(QObject):
    def eventFilter(self, ob, event):
        if event.type() == 121 and MAC:
            app.window.show()
            if app.window.isMaximized():
                app.window.showMaximized()
            else:
                app.window.showNormal()
        return False


def main():
    global app
    app = qapplication(test_time=60)
    event_eater = EventEater(app)
    app.installEventFilter(event_eater)
    font = load_fonts()
    app.setFont(font)
    splash = SplashScreen()
    splash.show_message("Initializing...")
    window = run_app(splash)
    app.window = window
    app.exec_()


if __name__ == '__main__':
    main()
