package html;

use Data::Dump qw/dump/;
use amt;

sub table {
	my $cols = shift;
	my $th;

	if ( $cols < 0 ) {
		$cols = abs($cols);
		$th .= qq|<th>| . shift(@_) . qq|</td>| foreach ( 1 .. $cols );
		$th .= qq|</tr>\n<tr>|;
	}

	my @td = map { "<td>$_</td>" } @_;
	my $html = qq{<table>\n<tr>$th};
	my $row = 0;

	foreach ( 0 .. $#td ) {
			$html .= $td[$_];
			if ( ( $_ + 1 ) % $cols == 0 ) {
				$zebra = $row++ % 2 == 0 ? qq{ style="background: #eee"} : '';
				$html .= qq{</tr>\n<tr$zebra>};
			};
	}
	$html .= qq{</tr>\n</table>};
}

sub tabs {
	return unless @_;
	my $html = qq{<table border=1>\n<tr>};
	foreach my $row ( @_ ) {
		$row =~ s{\t}{</td><td>}g;
		$html .= qq|<tr><td>$row</td><tr>\n|;
	}
	$html .= qq|</table>|;
	return $html;
}
	

sub tt {
	qq|<tt>| . join(' ', @_) . qq|</tt>| if @_;
}

sub select {
	my $name = shift;
	my $selected_option = shift;
	unshift @_, '' unless $selected_option;
	return join("\n"	
		, qq|<select type=select name=$name>|
		, join("\n", map { my $selected = $_ eq $selected_option ? 'selected' : ''; qq|<option name=$_ $selected>$_</option>| } @_ )
		, qq|</select>|
	);
}

sub pre {
	qq|<pre>| . join(' ', @_) . qq|</pre>| if @_;
}

sub pre_dump {
	my $data = shift;
	my $dump = dump( $data );
	$dump =~ s{"([^"]+)"(\s*=>)}{$1  $2}gs;
	$dump =~ s{"([^"]*)"}{"<b>$1</b>"}gs;
	qq|<pre>$dump</pre>|;
}

sub conf {
	my ($ip,$conf,$format) = @_;
	my @editable = splice(@_,3);

#	warn "# conf ",dump( $ip, $conf, $format, [ @editable ] );

	$format ||= 'inline';

	my @opts = map {
		my $name = $_;
		my $html = $conf->{$name};

		if ( $format eq 'edit' && grep { m/^$name$/ } @editable ) {
			$size = length($html);
			( $name, qq|<input name=$name value="$html" size=$size>| )
		} else {
			if ( $name eq 'amt' ) {
				my $amt_ip = amt::ip($ip,$html);
				# $html contains password!
				$html = qq|<a href=http://$amt_ip:16992/logon.htm target=$amt_ip>$amt_ip</a>|;

				if ( $format ne 'inline' ) {
					my $power = amt::power_on($ip);
					$html .= qq| power: |
					. ( $power
						? qq|<a href=/amt/PowerDown/$ip title="turn off">on</a>|
						: qq|<a href=/amt/PowerUp/$ip   title="turn on" >off</a>|
					);
					client::conf( $ip, 'power' => $power ? 'on' : 'off' );
				}
			} elsif ( $name eq 'ssh' ) {
				$html =~ s{\s(\S{16}).+(\S{16})\s}{ $1..$2 };
				chomp($html);
			} elsif ( $name eq 'kvm' ) {
				$html = qq|<a href="/start_stop/kvm?nr=$html">$html</a>|;
			} elsif ( $name eq 'munin' ) {
				$html = join("\n", map { my $url = $_; $url =~ s/-d\w+\.\w+$/.html/; qq|<a href="$url"><img src="$_"></a>| } split(/\s+/s,$html));
			} elsif ( $name eq 'ports' ) {
				$html =~ s{\b(\S+)\t(\S+)\t(\S+)\b}{<b title="$1/$2">$3</b> }gs;
			} else {
				$html = qq|<tt>$html</tt>|;
			};
		
			if ( $format =~ /edit|table/ ) {
				( $name, $html );
			} else {
				qq|<div class="config $name"><em>$name</em> $html</div>|
			}
		}
	} grep { length($conf->{$_}) > 0 } sort keys %$conf;

	$format eq 'inline' ? join("\n", @opts) : @opts;
}

warn "loaded";

1;
