from __future__ import absolute_import, print_function, unicode_literals

import contextlib

import Live


class ControlSurface(object):
    """Minimal legacy `_Framework.ControlSurface` compatibility layer."""

    def __init__(self, c_instance):
        self._c_instance = c_instance

    def application(self):
        if hasattr(self._c_instance, "application"):
            return self._c_instance.application()
        return Live.Application.get_application()

    def song(self):
        app = self.application()
        if hasattr(app, "get_document"):
            return app.get_document()
        return None

    def log_message(self, message):
        if hasattr(self._c_instance, "log_message"):
            self._c_instance.log_message(message)

    def show_message(self, message):
        if hasattr(self._c_instance, "show_message"):
            self._c_instance.show_message(message)

    def schedule_message(self, delay_in_ticks, callback, *args, **kwargs):
        if args or kwargs:
            def wrapped():
                return callback(*args, **kwargs)
        else:
            wrapped = callback

        if hasattr(self._c_instance, "schedule_message"):
            return self._c_instance.schedule_message(delay_in_ticks, wrapped)
        if int(delay_in_ticks or 0) <= 0:
            return wrapped()
        return None

    @contextlib.contextmanager
    def component_guard(self):
        yield

    def request_rebuild_midi_map(self):
        if hasattr(self._c_instance, "request_rebuild_midi_map"):
            return self._c_instance.request_rebuild_midi_map()
        return None

    def set_pad_translations(self, *args, **kwargs):
        if hasattr(self._c_instance, "set_pad_translations"):
            return self._c_instance.set_pad_translations(*args, **kwargs)
        return None

    def set_feedback_channels(self, *args, **kwargs):
        if hasattr(self._c_instance, "set_feedback_channels"):
            return self._c_instance.set_feedback_channels(*args, **kwargs)
        return None

    def set_controlled_track(self, *args, **kwargs):
        if hasattr(self._c_instance, "set_controlled_track"):
            return self._c_instance.set_controlled_track(*args, **kwargs)
        return None

    def instance_identifier(self):
        if hasattr(self._c_instance, "instance_identifier"):
            return self._c_instance.instance_identifier()
        return None

    def disconnect(self):
        return None

    def update_display(self):
        return None

    def build_midi_map(self, midi_map_handle):
        return None

    def receive_midi(self, midi_bytes):
        return None

    def handle_sysex(self, midi_bytes):
        return None

    def connect_script_instances(self, instantiated_scripts):
        return None

    def can_lock_to_devices(self):
        return False

    def lock_to_device(self, device):
        return None

    def unlock_from_device(self, device):
        return None

    def refresh_state(self):
        return None

    def port_settings_changed(self):
        return None

    def suggest_input_port(self):
        return ""

    def suggest_output_port(self):
        return ""

    def suggest_map_mode(self, cc_no, channel):
        return None
