#!@python_path@
# coding: utf-8

# Copyright (C) 2017-present Robert Griesel
# 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 3 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 gi
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk
from gi.repository import Gio
from gi.repository import Adw

import sys, gettext, argparse, os.path

from setzer.workspace.workspace import Workspace
import setzer.workspace.workspace_viewgtk as view
import setzer.keyboard_shortcuts.shortcuts as shortcuts
from setzer.app.service_locator import ServiceLocator
from setzer.dialogs.dialog_locator import DialogLocator
from setzer.app.color_manager import ColorManager
from setzer.app.font_manager import FontManager
from setzer.popovers.popover_manager import PopoverManager
from setzer.app.latex_db import LaTeXDB
from setzer.settings.document_settings import DocumentSettings
from setzer.helpers.timer import timer


class MainApplicationController(Adw.Application):

    def __init__(self):
        Adw.Application.__init__(self, application_id='org.cvfosammmm.Setzer', flags=Gio.ApplicationFlags.HANDLES_OPEN)

        self.is_active = False

    def do_open(self, files, number_of_files, hint=""):
        if not self.is_active:
            Adw.Application.do_activate(self)

            self.activate()
            self.is_active = True

        # open first session file if any
        for file in files:
            if file.get_path().endswith('.stzs'):
                filename = file.get_path()

                active_document = self.workspace.get_active_document()
                documents = self.workspace.get_all_documents()
                unsaved_documents = self.workspace.get_unsaved_documents()
                dialog = DialogLocator.get_dialog('close_confirmation')
                not_save_to_close_documents = dialog.run(unsaved_documents)['not_save_to_close_documents']

                if len(not_save_to_close_documents) == 0:
                    if documents != None:
                        for document in documents:
                            self.workspace.remove_document(document)
                    self.workspace.load_documents_from_session_file(filename)

        # open latex and bibtex files
        for file in files:
            if file.get_path().endswith('.tex') or file.get_path().endswith('.bib') or file.get_path().endswith('.cls') or file.get_path().endswith('.sty'):
                document_candidate = self.workspace.get_document_by_filename(file.get_path())
                if document_candidate != None:
                    self.workspace.set_active_document(document_candidate)
                else:
                    document_candidate = self.workspace.create_document_from_filename(file.get_path())
                    if document_candidate != None:
                        self.workspace.set_active_document(document_candidate)

    def do_activate(self):
        if not self.is_active:
            Adw.Application.do_activate(self)

            self.activate()
            self.is_active = True

    def activate(self):
        # setup gettext
        gettext.install('setzer', names=('ngettext',), localedir='@localedir_path@')

        # get settings
        self.settings = ServiceLocator.get_settings()
        Adw.StyleManager.get_default().set_color_scheme(Adw.ColorScheme.FORCE_LIGHT)

        # init static variables
        ServiceLocator.set_setzer_version('@setzer_version@')
        ServiceLocator.set_resources_path('@resources_path@')
        ServiceLocator.set_app_icons_path('@app_icons_path@')
        resources_path = ServiceLocator.get_resources_path()
        app_icons_path = ServiceLocator.get_app_icons_path()

        # init main window, model, dialogs
        self.main_window = view.MainWindow(self)

        icon_theme = Gtk.IconTheme.get_for_display(self.main_window.get_display())
        icon_theme.add_search_path(os.path.join(resources_path, 'icons'))
        icon_theme.add_search_path(app_icons_path)
        for folder in ['arrows', 'greek_letters', 'misc_math', 'misc_text', 'operators', 'relations']:
            icon_theme.add_search_path(os.path.join(resources_path, 'symbols', folder))

        ServiceLocator.set_main_window(self.main_window)
        ColorManager.init(self.main_window)
        FontManager.init(self.main_window)

        self.workspace = Workspace()

        PopoverManager.init(self.main_window, self.workspace)
        LaTeXDB.init(resources_path)
        self.main_window.create_widgets()
        ServiceLocator.set_workspace(self.workspace)
        DialogLocator.init_dialogs(self.main_window, self.workspace)

        # init view
        if self.settings.get_value('window_state', 'is_maximized'):
            self.main_window.maximize()
        else: 
            self.main_window.unmaximize()
        self.main_window.set_default_size(self.settings.get_value('window_state', 'width'), 
                                          self.settings.get_value('window_state', 'height'))
        self.main_window.present()

        self.main_window.connect('close-request', self.on_window_close)

        # init controller
        self.workspace.init_workspace_controller()
        self.shortcuts = shortcuts.Shortcuts()
        self.workspace.actions.actions['quit'].connect('activate', self.on_quit_action)

    def save_window_state(self):
        main_window = self.main_window
        self.settings.set_value('window_state', 'width', main_window.get_property('default-width'))
        self.settings.set_value('window_state', 'height', main_window.get_property('default-height'))
        self.settings.set_value('window_state', 'is_maximized', main_window.get_property('maximized'))
        self.settings.set_value('window_state', 'show_help', self.workspace.show_help)
        self.settings.set_value('window_state', 'show_preview', self.workspace.show_preview)
        self.settings.set_value('window_state', 'preview_paned_position', main_window.preview_paned.target_position)
        self.settings.set_value('window_state', 'show_build_log', self.workspace.show_build_log)
        self.settings.set_value('window_state', 'build_log_paned_position', main_window.build_log_paned.target_position)
        self.settings.set_value('window_state', 'show_symbols', self.workspace.show_symbols)
        self.settings.set_value('window_state', 'show_document_structure', self.workspace.show_document_structure)
        self.settings.set_value('window_state', 'sidebar_paned_position', main_window.sidebar_paned.target_position)
        self.settings.pickle()

    def on_window_close(self, window=None, parameter=None):
        self.save_quit()
        return True

    def on_quit_action(self, action=None, parameter=None):
        self.save_quit()

    def save_quit(self, unsaved_documents=None):
        for document in self.workspace.open_documents: DocumentSettings.save_document_state(document)

        if unsaved_documents == None:
            unsaved_documents = self.workspace.get_unsaved_documents()

        if len(unsaved_documents) > 0:
            self.workspace.set_active_document(unsaved_documents[0])
            dialog = DialogLocator.get_dialog('close_confirmation')
            dialog.run({'unsaved_document': unsaved_documents[0], 'documents': unsaved_documents}, self.save_quit_callback)
        else:
            self.save_state_and_quit()

    def save_quit_callback(self, parameters):
        document = parameters['unsaved_document']
        unsaved_documents = parameters['documents']

        if parameters['response'] == 0:
            unsaved_documents.remove(document)
            self.save_quit(unsaved_documents)
        elif parameters['response'] == 2:
            document = parameters['unsaved_document']
            if document.get_filename() == None:
                DialogLocator.get_dialog('save_document').run(document, self.save_callback, parameters)
            else:
                document.save_to_disk()
                unsaved_documents.remove(document)
                self.save_quit(unsaved_documents)

    def save_callback(self, parameters):
        document = parameters['unsaved_document']
        unsaved_documents = parameters['documents']

        if not document.source_buffer.get_modified():
            unsaved_documents.remove(document)
        self.save_quit(unsaved_documents)

    def save_state_and_quit(self):
        self.save_window_state()
        self.workspace.save_to_disk()
        self.quit()


argparser = argparse.ArgumentParser(usage='%(prog)s [OPTION...] [FILE...]')
argparser.add_argument('-V', '--version', action='version', version='@setzer_version@')
argparser.add_argument('file', nargs='*', help=argparse.SUPPRESS)
argparser.parse_args()

main_controller = MainApplicationController()
exit_status = main_controller.run(sys.argv)
sys.exit(exit_status)
