#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
$Id$

Copyright 2010 Lars Kruse <devel@sumpfralle.de>
Copyright 2008-2009 Lode Leroy

This file is part of PyCAM.

PyCAM 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.

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

from pycam.Physics.ode_physics import override_ode_availability
import pycam.Gui.common as GuiCommon
from optparse import OptionParser
import sys
import os

EXAMPLE_MODEL_LOCATIONS = (
        os.path.join(os.path.dirname(__file__), "Samples", "STL"),
        "/usr/share/pycam/Samples/STL")
DEFAULT_MODEL_FILE = "pycam.stl"


# check if we were started as a separate program
if __name__ == "__main__":
    inputfile = None
    outputfile = None

    parser = OptionParser(usage="usage: %prog [options] [inputfile [outputfile]]")
    parser.add_option("-c", "--config", dest="config_file",
            default=None, action="store", type="string",
            help="load a task settings file")
    parser.add_option("", "--disable-ode", dest="ode_disabled",
            default=False, action="store_true", help="disable experimental ODE collision detection")
    (options, args) = parser.parse_args()

    if len(args) > 0:
        inputfile = args[0]

    # try the configured interface first and then try to fall back to the alternative, if necessary
    deps_gtk = GuiCommon.dependency_details_gtk()
    report_gtk = GuiCommon.get_dependency_report(deps_gtk, prefix="\t")
    if GuiCommon.check_dependencies(deps_gtk):
        from pycam.Gui.Project import ProjectGui
        gui_class = ProjectGui
    else:
        full_report = []
        full_report.append("Error: Failed to load the GTK interface.")
        full_report.append("Details:")
        full_report.append(report_gtk)
        full_report.append("")
        full_report.append("Detailed list of requirements: %s" % GuiCommon.REQUIREMENTS_LINK)
        gui_class = None
        GuiCommon.EmergencyDialog("PyCAM dependency problem", os.linesep.join(full_report))
        sys.exit(1)

    if options.ode_disabled:
        override_ode_availability(False)

    gui = gui_class()

    # try to load the default model file ("pycam" logo)
    if not inputfile:
        for inputdir in EXAMPLE_MODEL_LOCATIONS:
            inputfile = os.path.join(inputdir, DEFAULT_MODEL_FILE)
            if os.path.isfile(inputfile):
                gui.open(inputfile)
                break
            else:
                # fall back to the simple test model
                from pycam.Importers.TestModel import TestModel
                gui.load_model(TestModel())
    else:
        gui.open(inputfile)
    # load task settings file
    # only available for the GTK interface
    if options.config_file:
        gui.open_task_settings_file(options.config_file)

    # open the GUI
    gui.mainloop()

