"""
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 dump, CommonFormater


class Formater(CommonFormater):
    """The markdown (for github) formater"""

    name = "github"
    _table_name = "github"
    level = 50
    enter_table = "<br/>"
    remove_multiple_enter = False

    def __init__(self) -> None:
        self.max_line = 0
        super().__init__()

    def header(self) -> str:
        """Header of the documentation"""
        if self.header_setted:
            return ""
        self.header_setted = True
        return "---\ngitea: none\ninclude_toc: true\n---\n"

    def title(
        self,
        title: str,
        level: int,
    ) -> str:
        """Display family name as a title"""
        char = "#"
        return f"{char * level} {title}\n\n"

    def join(
        self,
        lst: List[str],
    ) -> str:
        """Display line in table from a list"""
        return self.enter_table.join(lst)

    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 stripped(
        self,
        text: str,
    ) -> str:
        """Return stripped text (as help)"""
        if self.remove_multiple_enter:
            while True:
                new_text = text.replace("\n\n", "\n")
                if new_text == text:
                    text = new_text
                    break
                text = new_text
        return text.strip().replace("\n", self.enter_table)

    def list(
        self,
        choices,
    ):
        """Display a liste of element"""
        char = f"{self.enter_table}- "
        ret = ""
        for choice in choices:
            if not isinstance(choice, str):
                choice = dump(choice)
            ret += char + choice
        return ret

    def prop(
        self,
        prop: str,
        italic: bool,
    ) -> str:
        """Display property"""
        prop = f"`{prop}`"
        if italic:
            prop = self.italic(prop)
        return prop

    def table_header(self, lst):
        """Manage the header of a table"""
        return lst[0] + "&nbsp;" * (self.max_line - len(lst[0])), lst[1] + "&nbsp;" * (
            self.max_line - len(lst[1])
        )

    def yaml(self, _dump):
        """Dump yaml part of documentation"""
        return f"```yaml\n---\n{dump(_dump)}\n```\n"

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

    def columns(
        self,
        col: List[str],
    ) -> None:
        """count columns length"""
        for line in col:
            for l in line.split(self.enter_table):
                self.max_line = max(self.max_line, len(l) + 1)