package tech.tablesaw.io.html;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;

import tech.tablesaw.api.Table;
import tech.tablesaw.io.DataReader;
import tech.tablesaw.io.ReaderRegistry;
import tech.tablesaw.io.Source;
import tech.tablesaw.io.TableBuildingUtils;

public class HtmlReader implements DataReader<HtmlReadOptions> {

    private static final HtmlReader INSTANCE = new HtmlReader();

    static {
        register(Table.defaultReaderRegistry);
    }

    public static void register(ReaderRegistry registry) {
        registry.registerExtension("html", INSTANCE);
        registry.registerMimeType("text/html", INSTANCE);
        registry.registerOptions(HtmlReadOptions.class, INSTANCE);
    }

    @Override
    public Table read(HtmlReadOptions options) throws IOException {
        Document doc;
        InputStream inputStream = options.source().inputStream();
        if (inputStream != null) {
            // Reader must support mark, so can't use InputStreamReader
            // Parse the InputStream directly
            doc = Jsoup.parse(inputStream, null, "");
        } else {
            doc = Parser.htmlParser().parseInput(options.source().createReader(null), "");
        }
        Elements tables = doc.select("table");
        if (tables.size() != 1) {
            throw new IllegalStateException(
                    "Reading html to table currently works if there is exactly 1 html table on the page. "
                            + " The URL you passed has " + tables.size()
                            + ". You may file a feature request with the URL if you'd like your pagae to be supported");
        }
        Element htmlTable = tables.get(0);

        List<String[]> rows = new ArrayList<>();
        for (Element row : htmlTable.select("tr")) {
            Elements headerCells = row.getElementsByTag("th");
            Elements cells = row.getElementsByTag("td");
            String[] nextLine = Stream.concat(headerCells.stream(), cells.stream())
                    .map(Element::text).toArray(String[]::new);
            rows.add(nextLine);
        }

        Table table = Table.create(options.tableName());

        if (rows.size() == 0) {
            return table;
        }

        String[] headerRow = rows.get(0);
        List<String> columnNames = new ArrayList<>();
        for (int i = 0; i < headerRow.length; i++) {
            columnNames.add(headerRow[i]); // TODO: cleansing and fallback name
        }

        return TableBuildingUtils.build(columnNames, rows, options);
    }

    @Override
    public Table read(Source source) throws IOException {
      return read(HtmlReadOptions.builder(source).build());
    }

}

@@ -2478,96 +2478,139 @@
     
+List%3C
 String
-%5B%5D headerRow = rows.get(0);%0A        List%3CString%3E columnNames = new ArrayList%3C%3E();%0A
+%3E columnNames = new ArrayList%3C%3E();%0A        if (options.header()) %7B%0A            String%5B%5D headerRow = rows.remove(0);%0A    
     
@@ -2670,16 +2670,20 @@
         
+    
 columnNa
@@ -2708,45 +2708,154 @@
 i%5D);
- // TODO: cleansing and fallback name
+%0A            %7D%0A        %7D else %7B%0A            for (int i = 0; i %3C rows.get(0).length; i++) %7B%0A                columnNames.add(%22C%22 + i);%0A            %7D
 %0A   

Use header option in HtmlReader