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

# Carla Native Plugins
# Copyright (C) 2012-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 doc/GPL.txt file.

# ------------------------------------------------------------------------------------------------------------
# Imports (Config)

from carla_config import *

# ------------------------------------------------------------------------------------------------------------
# Imports (Global)

if config_UseQt5:
    from PyQt5.QtCore import Qt
else:
    from PyQt4.QtCore import Qt

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

from carla_shared import *
from carla_utils import *

# -----------------------------------------------------------------------
# Imports (ExternalUI)

from carla_app import CarlaApplication
from externalui import ExternalUI
from digitalpeakmeter import DigitalPeakMeter

# -----------------------------------------------------------------------
# External UI

class DistrhoUIBigMeter(ExternalUI, DigitalPeakMeter):
    def __init__(self):
        ExternalUI.__init__(self)
        DigitalPeakMeter.__init__(self, None)

        channels = 2 #6 if argv[0].endswith("bigmeterM-ui") else 2

        self.setChannelCount(channels)
        self.setMeterColor(self.COLOR_GREEN)
        self.setMeterOrientation(self.VERTICAL)

        #self.setSmoothRelease(0) # till 5

        self.resize(50, 400)
        self.setWindowTitle(self.fUiName)

        self.fIdleTimer = self.startTimer(30)

        self.ready()

    # -------------------------------------------------------------------
    # DSP Callbacks

    def dspParameterChanged(self, index, value):
        if index == 0:
            color = int(value)

            if color not in (DigitalPeakMeter.COLOR_GREEN,
                             DigitalPeakMeter.COLOR_BLUE):
                return

            self.setMeterColor(color)

        elif index == 1:
            style = int(value)

            if style not in (DigitalPeakMeter.STYLE_DEFAULT,
                             DigitalPeakMeter.STYLE_OPENAV,
                             DigitalPeakMeter.STYLE_RNCBC):
                return

            self.setMeterStyle(style)

        else:
            self.displayMeter(index-1, value)

    # -------------------------------------------------------------------
    # ExternalUI Callbacks

    def uiShow(self):
        self.show()

    def uiFocus(self):
        self.setWindowState((self.windowState() & ~Qt.WindowMinimized) | Qt.WindowActive)
        self.show()

        self.raise_()
        self.activateWindow()

    def uiHide(self):
        self.hide()

    def uiQuit(self):
        self.closeExternalUI()
        self.close()
        app.quit()

    def uiTitleChanged(self, uiTitle):
        self.setWindowTitle(uiTitle)

    # -------------------------------------------------------------------
    # Qt events

    def timerEvent(self, event):
        if event.timerId() == self.fIdleTimer:
            self.idleExternalUI()
        DigitalPeakMeter.timerEvent(self, event)

    def closeEvent(self, event):
        self.closeExternalUI()
        DigitalPeakMeter.closeEvent(self, event)

        # there might be other qt windows open which will block the UI from quitting
        app.quit()

#--------------- main ------------------
if __name__ == '__main__':
    import resources_rc

    pathBinaries, pathResources = getPaths()
    gCarla.utils = CarlaUtils(os.path.join(pathBinaries, "libcarla_utils." + DLL_EXTENSION))
    gCarla.utils.set_process_name("BigMeter")

    app = CarlaApplication("BigMeter")
    gui = DistrhoUIBigMeter()
    app.exit_exec()
