package XXV::MODULES::REPORT;

use strict;

use Tools;
use Locale::gettext;


# This module method must exist for XXV
# ------------------
sub module {
# ------------------
    my $obj = shift || return error ('No Object!' );
    my $args = {
        Name => 'REPORT',
        Description => gettext('This module read in every module the status information and display this. Also this module send this informations e.g. as mail report.'),
        Version => '0.01',
        Date => '19.04.2005',
        Author => 'xpix',
        Level => 'user',
        Preferences => {
            active => {
                description => gettext('Activate this service'),
                default     => 'y',
                type        => 'confirm',
                required    => gettext('This is required!'),
            },
            interval => {
                description => gettext('Time in hours to send the report.'),
                default     => 6,
                type        => 'integer',
                required    => gettext('This is required!'),
            },
            host => {
                description => gettext('Used host of referred link inside reports.'),
                default     => main::getModule('STATUS')->name,
                type        => 'string',
            },
        },
        Commands => {
            report => {
                description => gettext("Display the report screen 'modname'"),
                short       => 'rp',
                callback    => sub{ $obj->report(@_) },
            },
            request => {
                description => gettext("Display the actual news site 'typ'"),
                short       => 'req',
                callback    => sub{ $obj->request(@_) },
            },
        },
    };
    return $args;
}

# ------------------
sub new {
# ------------------
	my($class, %attr) = @_;
	my $self = {};
	bless($self, $class);

    # paths
    $self->{paths} = delete $attr{'-paths'};

	# who am I
    $self->{MOD} = $self->module;

    # the big Config
    $self->{CONFIG} = $attr{'-config'};

    # the dbh handle
    $self->{dbh} = delete $attr{'-dbh'};

    # all configvalues to $self without parents (important for ConfigModule)
    map {
        $self->{$_} = $attr{'-config'}->{$self->{MOD}->{Name}}->{$_} || $self->{MOD}->{Preferences}->{$_}->{default}
    } keys %{$self->{MOD}->{Preferences}};

    $self->{LastReportTime} = time;

    # Interval to send report
    Event->timer(
        interval => $self->{interval}*3600,
        prio => 6,  # -1 very hard ... 6 very low
        cb => sub{
            $self->report();
            $self->{LastReportTime} = time;
        },
    );

    # The Initprocess
    my $erg = $self->init or return error('Problem to initialze Module');

	return $self;
}

# ------------------
sub init {
# ------------------
    my $obj = shift  || return error ('No Object!' );

    # Load the NEWS Plugins ...
    my @mods = glob($obj->{paths}->{NEWSMODS});

    # Try to use the news plugins
    foreach my $module (reverse @mods) {
        my $moduleName = 'XXV::OUTPUT::NEWS::'.(split('\.',(split('/', $module))[-1]))[0];

        # make an object for the module
	    eval "use $moduleName";
        error $@ if $@;
        $obj->{NEWSMODS}->{$moduleName} = $moduleName->new(
            -config => $obj->{CONFIG},
            -dbh    => $obj->{dbh},
            -paths  => $obj->{paths},
            -host  => $obj->{host},
        );

        main::addModule($moduleName, $obj->{NEWSMODS}->{$moduleName});

        debug sprintf("Load NEWS Module %s = %s\n",
            $moduleName,
            (ref $obj->{NEWSMODS}->{$moduleName})
                ? $obj->{NEWSMODS}->{$moduleName}->{MOD}->{Version}
                : 'Problem!');
    }

    return 1;
}


# ------------------
sub report {
# ------------------
    my $obj = shift || return error ('No Object!' );
    my $watcher = shift;
    my $console = shift;
    my $modulename = shift || '';

    my $mods = main::getModules();

    # Look for status entry in modCfg and call his
    my $result = {};
    foreach my $modName (sort keys %{$mods}) {
        my $modCfg = $mods->{$modName}->{MOD};
        next if($modulename and uc($modulename) ne $modCfg->{Name});
        if(exists $modCfg->{Status} and ref $modCfg->{Status} eq 'CODE') {
            $result->{$modCfg->{Name}} = $modCfg->{Status}($watcher, $console, $obj->{LastReportTime});
        }
    }

    $console->table($result)
        if(ref $console);

    return 1;
}

# ------------------
sub news {
# ------------------
    my $obj = shift  || return error ('No Object!' );
    my $title = shift || return error ('No Title!' );
    my $text  = shift || '';
    my $cmd   = shift || return error ('No Command!' );
    my $id    = shift || return error ('No Id!' );
    my $lev   = shift || 10; # Level for how important is this news? (1 ... 100)

    my $news = {
        AddDate => time,
        Title   => $title,
        Text    => $text,
        Cmd     => $cmd,
        Id      => $id,
        Level   => $lev,
    };

    foreach my $modName (sort keys %{$obj->{NEWSMODS}}) {
        my $erg = $obj->{NEWSMODS}->{$modName}->read($news)
            || error($obj->{NEWSMODS}->{$modName}->{ERROR});
    }

}

# ------------------
sub request {
# ------------------
    my $obj = shift  || return error ('No Object!' );
    my $watcher = shift || return error ('No Watcher!' );
    my $console = shift || return error ('No Console!' );
    # To this time you can set on
    # cmd=request&data=rss or
    # cmd=request&data=mail
    my $typ = shift  || return error ('No Typ!' );

    my ($mod) = grep(/${typ}$/i, keys %{$obj->{NEWSMODS}});

    return $console->message(sprintf(gettext("Sorry, but this type '%s' does not exist on this system!"), $typ))
        unless($mod);

    return $console->out(
        $obj->{NEWSMODS}->{$mod}->req(),
        $obj->{NEWSMODS}->{$mod}->{TYP}
    );
}


# ------------------
sub scala {
# ------------------
    my $obj = shift  || return error ('No Object!' );
    my $typ = shift  || 0;

    $obj->{SCALA} = {
        'harmless'      => 10,
        'intresting'    => 30,
        'veryintresting'=> 50,
        'important'     => 70,
        'veryimportant' => 100,
    } unless(exists $obj->{SCALA});

    return $obj->{SCALA}->{$typ}
        if($typ and exists $obj->{SCALA}->{$typ});

    return $obj->{SCALA};
}


1;
