// import '../references/extensions.dist.js';

class ContentService {
    _popupService = null;

    _containerId = null;


    constructor(popupService) {
        const a  = this;

        a._popupService = popupService;
    }


    get Container() {
        const a = this;

        return document.getElementById(a._containerId);
    }

    // get Panel1() {
    //     return this.#getElement("div[role='options']", 1);
    // }

    // get Panel2() {
    //     return this.#getElement("div[role='filters']", 1);
    // }

    // get Panel3() {
    //     return this.#getElement("div[role='results']", 1);
    // }


    // Clear(clearAll) {
    //     const a = this;
    //     const label1 = a._getLabel(1);

    //     label1.innerText = "";

    //     if (Boolean.isTrue(clearAll)) {
    //         a._setHtml(a.Panel1, "");
    //         a._setHtml(a.Panel2, "");
    //     }

    //     a._setHtml(a.Panel3, "");
    // }

    // async Search() {
    //     const a = this;

    //     a.Clear(false);
    // }


    GenerateCheckBox(num, label, checked) {
        const randId = Math.randomN(100001, 999999);
        const id = "checkbox" + randId;
        const name = "checkbox" + num;

        let html = "";
        html += "<div class=\"form-check form-switch\">";
        html += "<input class=\"form-check-input\" type=\"checkbox\" role=\"switch\" id=\"" + id + "\" name=\"" + name + "\" " + (checked ? "checked" : "") + ">";
        html += "<label class=\"form-check-label\" for=\"" + id + "\">" + label + "</label>";
        html += "</div>";

        return html;
    }

    GenerateCSVLine(delimiter, ...args) {
        const a = this;

        if (args.length === 1) {
            if (Object.getDataType(args[0]) == "array") {
                args = args[0];
            }
        }

        let result = "";
        for (let i=0; i<args.length; i++) {
            if (i > 0) {
                result += delimiter
            }

            result += args[i];
        }

        return result;
    }

    // GenerateRadioBox(groupNo, num, label, value, checked) {
    //     const randId = Math.randomN(100001, 999999);

    //     let html = "";
    //     html += "<div class=\"form-check\">";
    //     html += "<input class=\"form-check-input\" type=\"radio\" name=\"radioGroup" + groupNo + "\" id=\"radioBox" + randId + "\" value=\"" + value + "\" " + (checked ? "checked" : "") + ">";
    //     html += "<label class=\"form-check-label\" for=\"radioBox" + randId + "\">" + label + "</label>";
    //     html += "</div>";

    //     return html;
    // }

    GenerateRow(...args) {
        const a = this;

        if (args.length === 1) {
            if (Object.getDataType(args[0]) == "array") {
                args = args[0];
            }
        }

        let result = "";
        for (let i=0; i<args.length; i++) {
            result = result + a._generateTableCell(args[i]);
        }

        return "<tr>" + result + "</tr>";
    }



    _generateCheckBoxPanel(col1, col2) {
        const a = this;

        let colContent1 = "";
        let colContent2 = "";

        for (let i=0; i<col1.length; i++) {
            colContent1 += a.GenerateCheckBox(col1[i].id, col1[i].text, col1[i].checked);
        }

        for (let i=0; i<col2.length; i++) {
            colContent2 += a.GenerateCheckBox(col2[i].id, col2[i].text, col2[i].checked);
        }

        return a.#generateCols(colContent1, colContent2);
    }

    _generateTextarea() {
        let html = "";
        html += "<textarea class=\"form-control font-monospace text-sm px-1 py-1\" rows=\"16\"></textarea>";

        return html;
    }

    _generateTable() {
        let html = "";
        html += "<table class=\"table table-bordered table-hover table-sm table-striped\">";
        html += "<thead></thead>";
        html += "<tbody></tbody>";
        html += "</table>";

        return html;
    }

    _generateTableCell(value) {
        return "<td class=\"\"><input type=\"text\" class=\"display\" readonly=\"readonly\" value=\"" + value + "\" /></td>";
    }

    _generateTableHeader(fixedColumns, conditionalColumns) {
        const a = this;

        let html = "";
        for (let i=0; i<fixedColumns.length; i++) {
            html += "<th>" + fixedColumns[i] + "</th>";
        }

        for (let i=0; i<conditionalColumns.length; i++) {
            if (!a._getCheckBox(conditionalColumns[i].id)) {
                continue;
            }

            html += "<th>" + conditionalColumns[i].name + "</th>";
        }

        return html;
    }


    _getButton(num) {
        return this.#getElement("button", num);
    }

    _getCheckBox(id) {
        const a = this;
        const name = "checkbox" + id;

        const el = this.#getElement("input[type='checkbox'][name='" + name + "']", 1);
        if (el == null) {
            return false;
        }

        return el.checked;
    }

    _getComboBox(num) {
        const a = this;

        const el = a.#getElement("select", num);
        if (el == null) {
            return "";
        }

        return el.value;
    }

    _getLabel(num) {
        return this.#getElement("[role='label']", num);
    }

    _getPanel(name, num) {
        return this.#getElement("div[role='" + name + "']", num);
    }

    _getTextBox(num) {
        return this.#getElement("input[type='text']", num);
    }


    _setHtml(element, htmlContent) {
        if (element == null) {
            return;
        }

        element.innerHTML = htmlContent;
    }


    #generateCols(...args) {
        let html = "";

        for (let i=0; i<args.length; i++) {
            html += "<div class=\"col\">" + args[i] + "</div>";
        }

        return html;
    }

    #getElement(nodeName, num) {
        const a = this;

        if (a.Container == null) {
            return null;
        }

        const nodes = a.Container.querySelectorAll(nodeName);
        if (nodes.length <= 0) {
            return null;
        }

        if (nodes.length > num) {
            return null;
        }

        return nodes[(num - 1)];
    }

}


export default ContentService;