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

namespace ReportGeneration.Abstract;

public abstract class TableGeneratorBase : StreamWriterBase, ITableGenerator
{
  /// <inheritdoc />
  public int Columns { get; }

  /// <inheritdoc />
  protected TableGeneratorBase(int columns) =>
    Columns = columns;

  /// <inheritdoc />
  protected TableGeneratorBase(int columns, Stream stream)
    : base(stream) => Columns = columns;

  /// <inheritdoc />
  protected TableGeneratorBase(int columns, Stream stream, Encoding encoding)
    : base(stream, encoding) => Columns = columns;


  #region Header

  /// <inheritdoc />
  public virtual ITableGenerator AppendHeader(string content) =>
    AppendHeader(Enumerable.Range(0, Columns).Select(_ => content));

  /// <inheritdoc />
  public abstract ITableGenerator AppendHeader(IEnumerable<string> row);

  /// <inheritdoc />
  public virtual ITableGenerator AppendHeader(IEnumerable<IEnumerable<string>> rows)
  {
    foreach (var row in rows)
    {
      AppendHeader(row);
    }

    return this;
  }

  #endregion

  #region Row

  /// <inheritdoc />
  public virtual ITableGenerator AppendRow(string content) =>
    AppendRow(Enumerable.Range(0, Columns).Select(_ => content));

  /// <inheritdoc />
  public abstract ITableGenerator AppendRow(IEnumerable<string> row);

  /// <inheritdoc />
  public virtual ITableGenerator AppendRows(IEnumerable<IEnumerable<string>> rows)
  {
    foreach (var row in rows)
    {
      AppendRow(row);
    }

    return this;
  }

  #endregion
}
