#!/usr/bin/env python3

#    Copyright (C) 2012-2017 Christian Thomas Jacobs.

#    This file is part of PyQSO.

#    PyQSO 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.
#
#    PyQSO 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 PyQSO.  If not, see <http://www.gnu.org/licenses/>.

from gi import require_version
require_version('Gtk', '3.0')
require_version('PangoCairo', '1.0')
from gi.repository import Gtk, Gdk, GdkPixbuf
import argparse
try:
    import configparser
except ImportError:
    import ConfigParser as configparser
import os
import os.path
import sys
import signal
import pkg_resources

import logging
logging.basicConfig(level=logging.INFO)
logging.info("PyQSO version 1.0.0")

# This will help Python find the PyQSO modules that need to be imported below.
pyqso_path = os.path.join(os.path.realpath(os.path.dirname(__file__)), os.pardir)
sys.path.insert(0, pyqso_path)

# PyQSO modules.
from pyqso.adif import *
from pyqso.logbook import *
from pyqso.menu import *
from pyqso.popup import *
from pyqso.toolbar import *
from pyqso.toolbox import *
from pyqso.preferences_dialog import *


class PyQSO:

    """ The PyQSO application class. """

    def __init__(self, logbook_path=None):
        """ Set up the main (root) window, start the event loop, and open a logbook (if the logbook's path is specified by the user in the command line).

        :arg str logbook_path: An optional argument containing the path of the logbook file to open. If no value is provided, this defaults to None and no logbook is opened.
        """

        # Get the PyQSO main window defined in the Glade file.
        self.builder = Gtk.Builder()
        glade_file_path = pkg_resources.resource_filename("pyqso", os.path.join("res", "pyqso.glade"))
        self.builder.add_from_file(glade_file_path)
        self.window = self.builder.get_object("pyqso")

        # Check that the directory for holding PyQSO configuration files exists. If it doesn't, create it now.
        config_directory = os.path.expanduser('~/.config/pyqso')
        if not os.path.exists(config_directory):
            try:
                os.makedirs(config_directory)
            except Exception as e:
                logging.error("An error occurred whilst creating a directory for PyQSO configuration files. Try creating the directory '~/.config/pyqso' manually.")
                logging.exception(e)

        # Get any application-specific preferences from the configuration file.
        config = configparser.ConfigParser()

        # Check that the configuration file actually exists (and is readable)
        # otherwise, we will resort to the defaults.
        have_config = (config.read(config_directory + "/preferences.ini") != [])

        # Kills the application if the close button is clicked on the main window itself.
        self.window.connect("delete-event", Gtk.main_quit)

        self.statusbar = self.builder.get_object("statusbar")
        context_id = self.statusbar.get_context_id("Status")
        self.statusbar.push(context_id, "No logbook is currently open.")

        # Create a Logbook so we can add/remove/edit logs and records,
        # once connected to the SQLite database.
        self.logbook = Logbook(self)
        self.toolbox = Toolbox(self)

        # Set up the menu and toolbar. These classes depend on the Logbook and Toolbox class.
        self.menu = Menu(self)
        self.popup = Popup(self)
        self.toolbar = Toolbar(self)

        # Clipboard for copy/paste operations.
        self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)

        # Show the main window.
        self.window.show_all()

        if(have_config):
            if(not config.getboolean("general", "show_toolbox")):
                self.toolbox.toggle_visible_callback()
        else:
            # Hide the Toolbox by default.
            self.toolbox.toggle_visible_callback()

        if(logbook_path):
            logging.info("Opening logbook: %s" % logbook_path)
            self.logbook.open(path=logbook_path)
        else:
            # If no logbook path is specified at the command line,
            # then check if the user wants to open a default logbook.
            (section, option) = ("general", "default_logbook")
            if(have_config and config.has_option(section, option)):
                open_default_logbook = config.getboolean(section, option)
                (section, option) = ("general", "default_logbook_path")
                if(open_default_logbook and config.has_option(section, option)):
                    logbook_path = config.get(section, option)
                    if(logbook_path is not None and logbook_path != ""):
                        logging.info("Opening the default logbook: %s" % logbook_path)
                        self.logbook.open(path=logbook_path)

        return

    def show_about(self, widget):
        """ Show the About dialog, which includes license information. """
        glade_file_path = pkg_resources.resource_filename("pyqso", os.path.join("res", "pyqso.glade"))
        self.builder.add_objects_from_file(glade_file_path, ("about_dialog",))
        about = self.builder.get_object("about_dialog")
        about.run()
        about.destroy()
        return

    def show_preferences(self, widget):
        """ Show the Preferences dialog. Any changes made by the user after clicking the 'Ok' button are saved in the configuration file. """
        preferences = PreferencesDialog(self)
        response = preferences.dialog.run()
        if(response == Gtk.ResponseType.OK):
            preferences.commit()
        preferences.dialog.destroy()
        return

if(__name__ == "__main__"):
    # Get any command line arguments.
    parser = argparse.ArgumentParser(prog="pyqso")
    parser.add_argument("-d", "--debug", action="store_true", default=False, help="Enable debugging. All debugging messages will be written to pyqso.debug.")
    parser.add_argument("-l", "--logbook", action="store", type=str, metavar="/path/to/my_logbook_file.db", default=None, help="Path to a Logbook file. If this file does not already exist, then it will be created.")
    args = parser.parse_args()

    # Output debugging messages to a file.
    if(args.debug):
        # Get the root logger.
        logger = logging.getLogger()
        logger.setLevel(logging.DEBUG)
        # Add a file handler.
        handler = logging.FileHandler("pyqso.debug", mode="w")
        formatter = logging.Formatter(fmt="%(asctime)s %(levelname)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
        handler.setFormatter(formatter)
        logger.addHandler(handler)

    # Enforce an absolute logbook file path.
    if(args.logbook):
        args.logbook = os.path.abspath(args.logbook)

    signal.signal(signal.SIGINT, signal.SIG_DFL)  # Exit PyQSO if a SIGINT signal is captured.
    application = PyQSO(args.logbook)  # Populate the main window and show it.
    Gtk.main()  # Start up the event loop!
