using ReportGeneration.Abstract;
using ReportGeneration.Interface;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace ReportGeneration.Generators;


internal class HtmlTableGenerator : TableGeneratorBase
{
  public string Class { get; init; }

  private static readonly (string start, string end) rowFormat = ("<tr>", "</tr>");
  private static readonly (string start, string end) headerFormat = ("<th>", "</th>");
  private static readonly (string start, string end) columnFormat = ("<td>", "</td>");

  /// <inheritdoc />
  public HtmlTableGenerator(int columns) : base(columns) { }

  /// <inheritdoc />
  public HtmlTableGenerator(int columns, Stream stream) : base(columns, stream) { }

  /// <inheritdoc />
  public HtmlTableGenerator(int columns, Stream stream, Encoding encoding) : base(columns, stream,
    encoding)
  { }

  #region State

  /// <inheritdoc />
  protected override void OnOpen()
  {
    base.OnOpen();
    Writer.Write($"<table class={Class}>");
  }

  /// <inheritdoc />
  protected override void OnClose()
  {
    base.OnClose();
    Writer.Write("</table>");
  }

  #endregion

  #region Writing

  /// <inheritdoc />
  public override ITableGenerator AppendHeader(IEnumerable<string> row) =>
    AppendRow(row, rowFormat, headerFormat);

  /// <inheritdoc />
  public override ITableGenerator AppendRow(IEnumerable<string> row) =>
    AppendRow(row, rowFormat, columnFormat);

  private ITableGenerator AppendRow(
    IEnumerable<string> row,
    (string, string) rowFormat,
    (string, string) columnFormat
  )
  {
    var (rowStart, rowEnd) = rowFormat;
    var (colStart, colEnd) = columnFormat;

    this
      .Write(rowStart)
      .Write(colStart)
      .Write(string.Join(colEnd + colStart, row))
      .Write(colEnd)
      .Write(rowEnd);

    return this;
  }

  #endregion
}