"""
Silique (https://www.silique.fr)
Copyright (C) 2024-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 ..utils import CommonFormatter, dump
from ..i18n import _


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

    name = "asciidoc"
    _table_name = "asciidoc"
    level = 40

    def title(
        self,
        title: str,
        level: int,
    ) -> str:
        """Display family name as a title"""
        char = "="
        return f"{char * (level + 1)} {title}\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 += "\n\n"
                else:
                    string += " +\n"
            string += line

            previous = line
        return string

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

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

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

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

    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 = "\n\n* "
        char = "\n* "
        return prefix + char.join([dump(choice) for choice in choices])

    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"`{prop}`"

    def yaml(self, _dump: dict) -> str:
        """Dump yaml part of documentation"""
        return f"[,yaml]\n----\n---\n{dump(_dump)}\n----\n"

    def table(self, with_header: bool = True) -> str:
        """Transform list to a table in string format
        we change the first line because we want that col has the same width
        """
        table = super().table(with_header)
        stable = table.split("\n", 1)
        return '[cols="1a,1a"]\n' + stable[1]

    def link(
        self,
        comment: str,
        link: str,
        underline: bool,
    ) -> str:
        """Add a link"""
        if underline:
            link = self.underline(link)
        return f"`{link}[{comment}]`"

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

    def family_informations(self) -> str:
        info = self.bold(f"🛈 {_('Informations')}")
        return f"====\n{info}\n\n"

    def end_family_informations(self) -> str:
        return f"\n====\n"

    def family_informations_ends_line(self) -> str:
        return " +\n"
