package System::Introspector::Report::Publish::MediaWiki::Producer;
use Moo;
use HTML::Zoom;

sub render {
  my ($self, $stream) = @_;
  return join "\n", map {
    ref($_) ? $self->_render_table($_) : $_;
  } @$stream;
}

sub _wrap {
  my ($self, $type, $id, $body) = @_;
  chomp $body;
  return join "\n",
    sprintf('<!-- SI:%s begin %s -->', uc($type), $id),
    $body,
    sprintf('<!-- SI:%s end %s -->', uc($type), $id),
}

sub _render_table {
  my ($self, $report) = @_;
  my $id     = $report->{id};
  my $str_id = ref($id) ? join(':', @$id) : $id;
  my $markup = $self->_load_markup;
  $markup = $self->_apply_identity($markup, $report);
  $markup = $self->_apply_table_head($markup, $report);
  $markup = $self->_apply_table_body($markup, $report);
  my $description = $report->{description} || [''];
  return join "\n",
    $self->_wrap('title', $str_id, $self->_render_title($report)),
    @$description,
    $self->_wrap('table', $str_id, $markup->to_html),
    '';
}

sub _render_title {
  my ($self, $report) = @_;
  return sprintf '== %s ==', $report->{title};
}

sub _apply_identity {
  my ($self, $markup, $report) = @_;
  my $id = $report->{id};
  return $markup
    ->select('table')
    ->set_attribute(id => 'si-id-' . join '__',
      ref($id) ? @$id : $id,
    );
}

sub _apply_table_body {
  my ($self, $markup, $report) = @_;
  my $index = 0;
  my $rows  = $report->{rows};
  my @cols  = map $_->{key}, @{$report->{columns}};
  return $markup->repeat('.data-row', sub {
    return HTML::Zoom::CodeStream->new({
      code => sub {
        return if $index > $#$rows;
        my $row = $rows->[$index++];
        return sub {
          $_->repeat('td', [ map {
            my $col_idx = $_;
            my $col = $cols[$_];
            my $value = $row->{$col};
            sub {
              $_->apply_if($col !~ m{^__}, sub {
                  $_->add_to_attribute('td', class => "si-column-$col");
                })
                ->replace_content('td', defined($value) ? $value : '')
                ->apply_if($col_idx != $#cols, sub {
                  $_->add_after('td', "\n    ");
                });
            };
          } 0 .. $#cols ]);
        };
      },
    });
  });
}

sub _apply_table_head {
  my ($self, $markup, $report) = @_;
  my @cols = @{$report->{columns}};
  return $markup
    ->repeat('th', [map {
      my $col_idx = $_;
      my $col = $cols[$_];
      sub {
        $_->apply_if($col->{key} !~ m{^__}, sub {
            $_->add_to_attribute('th', class => 'si-colhead-' . $col->{key});
          })
          ->replace_content('th', $col->{label})
          ->apply_if($col_idx != $#cols, sub {
            $_->add_after('th', "\n    ");
          });
      };
    } 0 .. $#cols ])
}

sub _load_markup {
  return HTML::Zoom->from_html(do {
    local $/;
    scalar <DATA>;
  });
}

1;

__DATA__
<table class="si-report">
  <tr>
    <th>Header</th>
  </tr>
  <tr class="data-row">
    <td>Value</td>
  </tr>
</table>
