import ContentService from './content-service.js';


class TabContentService extends ContentService {
    _button1 = null;
    _button2 = null;
    _label1 = null;
    _panel1 = null;
    _panel2 = null;
    _panel3 = null;
    _textBox1 = null;

    _optionsCol1 = [];
    _optionsCol2 = [];
    _filtersCol1 = [];
    _filtersCol2 = [];


    constructor(popupService) {
        super(popupService);
    }

    InitialiseComponents() {
        const a = this;

        const buttons = a.Container.querySelectorAll("button");
        a._button1 = buttons[0];
        a._button2 = buttons[1];
        a._label1 = a._getLabel(1);
        a._panel1 = a._getPanel("options", 1);
        a._panel2 = a._getPanel("filters", 1);
        a._panel3 = a._getPanel("results", 1);
        a._textBox1 = a._getTextBox(1);

        a.Clear(true);

        // Initialise options
        const htmlContent1 = a._generateCheckBoxPanel(a._optionsCol1, a._optionsCol2);

        a._setHtml(a._panel1, htmlContent1);

        // Initialise filters
        const htmlContent2 = a._generateCheckBoxPanel(a._filtersCol1, a._filtersCol2);

        a._setHtml(a._panel2, htmlContent2);

        // Events
        a._button1.addEventListener('click', async function (e) {
            a._button2.disabled = true;

            a._textBox1.value = "";

            await a.Search();

            a._button2.disabled = false;
        });

        a._button2.addEventListener('click', async function (e) {
            a._button2.disabled = true;

            await a.Search();

            a._button2.disabled = false;
        });
    }

    Clear(clearAll) {
        const a = this;

        a._label1.innerText = "";

        if (Boolean.isTrue(clearAll)) {
            a._setHtml(a._panel1, "");
            a._setHtml(a._panel2, "");
        }

        a._setHtml(a._panel3, "");
    }

    async Search() {
        const a = this;

        const result = await a._retrieveResult();
        const totalCount = result.length;

        const newResults = await a._filterResult(result.copy());

        if (newResults.length == totalCount) {
            a._label1.innerText = "Showing " + totalCount + " result" + (totalCount == 1 ? "" : "s");
        } else {
            a._label1.innerText = "Showing " + newResults.length + " of " + totalCount + " result" + (totalCount == 1 ? "" : "s");
        }

        switch (a._getComboBox(2)) {
            case "table":
                a._renderTable(newResults);
                break;
            case "csv":
                a._renderCSV(newResults);
                break;
            case "links":
                a._renderLinks(newResults);
                break;
            default:
                break;
        }
    }

    async _retrieveResult() {
        return [];
    }

    async _filterResult(result) {
        return result;
    }

    _renderTable(result) {
    }

    _renderCSV(result) {
    }

    _renderLinks(result) {
    }

}


export default TabContentService;