"""
Silique (https://www.silique.fr)
Copyright (C) 2025

This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from typing import List
from html import escape
from ..utils import CommonFormatter, dump


class Formatter(CommonFormatter):
    """The asciidoc formatter"""

    name = "html"
    _table_name = "unsafehtml"
    level = 45

    def title(
        self,
        title: str,
        level: int,
    ) -> str:
        """Display family name as a title"""
        return f"<h{level}>{title}</h{level}>\n\n"

    def join(
        self,
        lst: List[str],
    ) -> str:
        """Display line in table from a list"""
        string = ""
        previous = ""
        for line in lst:
            if string:
                #                if self.is_list(previous.split("\n", 1)[-1]):
                #                    string += "<br/><br/>"
                #                else:
                string += "<br/>"
            string += line

            previous = line
        return string

    def bold(
        self,
        msg: str,
    ) -> str:
        """Set a text to bold"""
        return f"<b>{msg}</b>"

    def italic(
        self,
        msg: str,
    ) -> str:
        """Set a text to italic"""
        return f"<i>{msg}</i>"

    def delete(
        self,
        msg: str,
    ) -> str:
        """Set a text to deleted"""
        return f"<del>{msg}</del>"

    def underline(
        self,
        msg: str,
    ) -> str:
        """Set a text to underline"""
        return f"<ins>{msg}</ins>"

    def stripped(
        self,
        text: str,
    ) -> str:
        """Return stripped text (as help)"""
        return text.strip()

    def list(
        self,
        choices: list,
        *,
        inside_table: bool=True,
        type_: str="variable",
    ) -> str:
        """Display a liste of element"""
        prefix = "<ul>"
        char = "\n"
        return (
            "<ul>"
            + char.join(["<li>" + dump(choice) + "</li>" for choice in choices])
            + "</ul>"
        )

    def prop(
        self,
        prop: str,
        italic: bool,
        delete: bool,
        underline: bool,
    ) -> str:
        """Display property"""
        if italic:
            prop = self.italic(prop)
        if delete:
            prop = self.delete(prop)
        if underline:
            prop = self.underline(prop)
        return f"<mark>{prop}</mark>"

    def yaml(self, _dump: dict) -> str:
        """Dump yaml part of documentation"""
        return f"<pre>{dump(_dump)}</pre>"

    def link(
        self,
        comment: str,
        link: str,
        underline: bool,
    ) -> str:
        """Add a link"""
        return self.prop(f"<a href='{link}'>{comment}</a>", False, False, underline)

    def is_list(
        self,
        txt: str,
    ) -> str:
        """verify if a text is a list"""
        return txt.strip().startswith("<ul>")

    def to_phrase(self, text: str) -> str:
        return escape(text)
