#! /bin/python
# -*- coding: utf-8 -*-
## Copyright (C) 2013 ABRT team <abrt-devel-list@redhat.com>
## Copyright (C) 2013 Red Hat, Inc.

## 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
## (at your option) 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.

## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335  USA


import sys
from pyfros.froslogging import info, set_verbosity
from pyfros.controls import Controls
#pylint: disable=E0611
from gi.repository import Gtk
#pylint: disable=W0401
from pyfros.plugins import *
from pyfros.i18n import _
from pyfros import i18n
import pyfros.plugins as plugins
import argparse

PLUGIN_DIR = "."


def load_plugins():
    loaded_plugins = []
    import inspect
    #pylint: disable=W0612
    for name, obj in inspect.getmembers(plugins):
        try:
            plugin_instance = obj.getScreencastPluginInstance()
            if plugin_instance.IsSuitable() > 0:  # append only suitable plugins
                loaded_plugins.append(plugin_instance)
                info("Added plugin:", plugin_instance)
        #pylint: disable=W0703
        except Exception, ex:
            info("'{0}' is not a plugin: '{1}'".format(obj, ex))

    # return plugins sorted by their IsSuitable weight, the best match first
    return sorted(loaded_plugins, key=lambda plugin: plugin.IsSuitable(), reverse=True)

if __name__ == "__main__":
    # init internationalization
    i18n.init("fros")

    commands = argparse.ArgumentParser('parent', description=_("Screencasting cmdline"))
    commands.add_argument("--verbose", "-v", action="count", default=0)
    commands.add_argument("--is-available", action="store_true",
        help=_("check if the current environment supports screencasting (0: is available, 1: not available)"),
        default=False)

    args = commands.parse_args()
    set_verbosity(args.verbose)
    available_plugins = load_plugins()

    if args.is_available:
        if available_plugins:
            sys.exit(0)
        sys.exit(1)

    info("Selected plugin: ", available_plugins[0])
    controls = Controls(available_plugins[0])
    controls.show_all()
    Gtk.main()
