package Statusmon::HTMLLog;

use Statusmon::Statusmon;

@Statusmon::HTMLLog::ISA = qw(Statusmon::Statusmon);


sub new
{

    my ($this) = @_;
    my  $class = ref($this) || $this;
    my  $self = {};

    bless($self, $class);

    $self->{"statedir"} = "$BigSister::common::fs{'web'}/logs";
    $self->register( "config" );
    return($self);
}


sub start {
    my( $self ) = @_;
    if( $self->getarg( "immediate" ) =~ /^(yes|ja|true)/i ) {
	$self->register( "status" );
    }
    else {
	$self->register( "statuschange" );
	$self->register( "textchange" );
    }
    $self->register( "config" );
    my $config = $self->get_feature_provider( "displayconfig" );
    if( $config ) { $self->read_config( $config->get_current_config() ) };
}
    

sub read_config {
    my( $self, $newconfig, $oldconfig ) = @_;

    $self->{"config"} = $newconfig;
    $self->register( "-status", "-statuschange", "-textchange" );
    if( $newconfig->{"options"}->{"ImmediateHTML"} ) {
        $self->register( "status" );
    }
    else {
	$self->register( "statuschange" );
	$self->register( "textchange" );
    }
}



sub event {
    my( $self, $cmd ) = @_;

    return if( $cmd->{"id"} eq $self->{"lastid"} );
    print STDERR "Statusmon::HTMLLog got ".($cmd->{"cmd"})." event\n" if( $BigSister::common::dl>6 );

    if( grep( $_ eq $cmd->{"cmd"}, "status", "statuschange", "textchange" ) ) {
	$self->statuschanged( $cmd );
	$self->{"lastid"} = $cmd->{"id"};
    }
    elsif( $cmd->{"cmd"} eq "displayconfig" ) {
	$self->read_config( $cmd->{"config"}, $cmd->{"previous_config"} );
    }
}


sub statuschanged {
    my( $self, $cmd ) = @_;

    print STDERR "event: ".($cmd->{"cmd"})."\n";
    my @log = $self->extract_log( $self->statusfile_name( $cmd->{"host"}, $cmd->{"check"} ) );

    if( $cmd->{"cmd"} eq "statuschange" ) {
	my $text = $cmd->{"text"};
	$text =~ s/\&html.*//s;
	$text =~ s/\n/ \|\>/gms;
	$text =~ s/-->/--#>/g;
	$text = substr( $text, 0, 120 );
	unshift( @log, [
	    time,
	    $cmd->{"statuscode"},
	    $text
	] );
    }

    $self->write_html_status( 
	$cmd->{"host"},
	$cmd->{"check"},
	$cmd->{"statuscode"},
	$cmd->{"rawtext"},
	$cmd->{"statussince"},
	$self->{"config"}->{"logskin"},
	\@log
	);
}



sub extract_log {
    my( $self, $filename ) = @_;

    open( _LOG, "<$filename" ) || return();
    my @log = ();
outer: while( <_LOG> ) {
        if( /<!--# log raw data/ ) {
	    while( <_LOG> ) {
	        last outer if( /-->/ );
		chomp;
		my @data = split( /\s\<\|\>\s/, $_ );
		push( @log, \@data );
	    }
	}
    }
    close _LOG;
    return( @log );
}



sub statusfile_name {
    my( $self, $group, $item ) = @_;

    return( $Statusmon::display->{"dir"}."/html/".$group.".$item.html" );
}

    

sub write_html_status {
    my( $self, $group, $item, $state, $text, $date, $skin, $log ) = @_;
    my( %vars, $html );

    print "skin: $skin\n";
    my $display = $Statusmon::display;

    $skin->{"_BASE"} = "..";

    my $printlog = "";
    my $rawlog = "<!--# log raw data\n";
    my $logcount = 0;
    foreach my $logentry (@$log) {
	last if( $logcount++ > 10 );
        $rawlog .= join( " <|> ", @$logentry )."\n";
	$printlog .= $display->replace_vars( $skin, {
	    "STATUSCODE" => $logentry->[1],
	    "STATUS" => $BigSister::common::status_texts{$logentry->[1]},
	    "TIME" => scalar localtime( $logentry->[0] ),
	    "TEXT" => $display->fixHTMLinText( $logentry->[2] )
	}, $skin->{"loghtml_historyline.proto"} );

    }
    $rawlog .= "-->\n";

    $vars{"HOST"} = $group;
    $vars{"ITEM"} = $item;

    if( $text =~ /^(.*?:\d\d \d{2,4})/ ) 
    {
	$vars{"DATE"} = $1;
	$text = $';
    };

    $text = $display->fixHTMLinText( $text );

    $vars{"STATUS"} = $BigSister::common::status_texts{$state};
    $vars{"TEXT"} = $text;
    $vars{"TIME"} = localtime( time );
    $vars{"LASTCHANGE"} = localtime( $date );
    $vars{"BGROUND"} = $BigSister::common::status_texts{$state};
    $vars{"TEXT"} = $display->replace_vars( $skin, \%vars, $skin->{"loghtml_text.proto"} );
    $vars{"HISTORYDATA"} = $rawlog;
    $vars{"HISTORY"} = $printlog;
    $display->replace_vars_file( $skin, \%vars, $skin->{"loghtml.proto"}, $self->statusfile_name( $group, $item ) );
    delete $skin->{"_BASE"};
}


