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

# Carla plugin host
# Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
#
# 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 2 of
# the License, or 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.
#
# For a full copy of the GNU General Public License see the GPL.txt file

# ------------------------------------------------------------------------------------------------------------
# Imports (Custom Stuff)

from carla_host import *
from carla_patchbay import CarlaPatchbayW

# ------------------------------------------------------------------------------------------------------------
# Main Window

class CarlaHostW(HostWindow):
    def __init__(self, parent=None):
        HostWindow.__init__(self, parent)

        self.fContainer = CarlaPatchbayW(self)
        self.setupContainer(True, self.fContainer.themeData)

    # -----------------------------------------------------------------

    def showEvent(self, event):
        HostWindow.showEvent(self, event)

        # set our gui as parent for all plugins UIs
        if gCarla.host is not None:
            gCarla.host.set_engine_option(ENGINE_OPTION_FRONTEND_WIN_ID, 0, str(self.winId()))

# ------------------------------------------------------------------------------------------------------------
# Main

if __name__ == '__main__':
    # -------------------------------------------------------------
    # Read CLI args

    initName = os.path.basename(__file__) if ("__file__" in dir() and os.path.dirname(__file__) in PATH) else sys.argv[0]
    libPrefix = None

    for arg in sys.argv:
        if arg.startswith("--with-appname="):
            initName = os.path.basename(arg.replace("--with-initname=", ""))

        elif arg.startswith("--with-libprefix="):
            libPrefix = arg.replace("--with-libprefix=", "")

    # -------------------------------------------------------------
    # App initialization

    app = CarlaApplication("Carla2-Patchbay", libPrefix)

    # -------------------------------------------------------------
    # Set-up custom signal handling

    setUpSignals()

    # -------------------------------------------------------------
    # Init host backend

    gCarla.isControl   = False
    gCarla.isLocal     = True
    gCarla.isPlugin    = False
    gCarla.processMode = ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS

    initHost(initName, libPrefix)

    # -------------------------------------------------------------
    # Create GUI

    gCarla.gui = CarlaHostW()

    # -------------------------------------------------------------
    # Load project file if set

    args = app.arguments()

    if len(args) > 1:
        arg = args[-1]

        if arg.startswith("--with-appname=") or arg.startswith("--with-libprefix="):
            pass
        elif os.path.exists(arg):
            gCarla.gui.loadProjectLater(arg)

    # -------------------------------------------------------------
    # Show GUI

    gCarla.gui.show()

    # -------------------------------------------------------------
    # App-Loop

    app.exit_exec()
