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

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 ..i18n import _
from ..util import CommonOutput


class OutputFamily(CommonOutput):
    level = 20
    name = "github"
    variable_hidden_color = "#A52A2A"
    variable_normal_color = None
    value_unmodified_color = "#B8860B"
    value_modified_color = "#006400"
    value_default_color = None
    error_color = "#C23636"
    error_icon = "stop_sign"
    warning_color = "#EFBF04"
    warning_icon = "bell"

    def _run(self, root):
        ret = self.header() + "\n".join(root) + "\n"
        return True, ret

    def header(self):
        variables = []
        if self.variable_default_enable:
            variables.append(_("Variable"))
        if self.variable_hidden_enable:
            variables.append(
                self.set_color(self.variable_hidden_color, _("Unmodifiable variable"))
            )
        values = []
        if self.value_unmodified_enable:
            values.append(
                self.set_color(self.value_unmodified_color, _("Default value"))
            )
        if self.value_modified_enable:
            values.append(
                self.set_color(self.value_modified_color, _("Modified value"))
            )
        if self.value_default_enable:
            values.append(f'(:hourglass_flowing_sand: {_("Original default value")})')
        if not variables and not values:
            return ""
        caption = self.title(_("Caption:"))
        if variables:
            caption += "> - " + "\n> - ".join(variables) + "\n"
        if values:
            caption += "> - " + "\n> - ".join(values) + "\n"
        if self.layer_datas:
            layers = "\n" + self.title(_("Layers:"))
            first_char = "> - "
            display_layers = [first_char + data for data in self.layer_datas]
            layers += "\n".join(display_layers)
        else:
            layers = ""
        return caption + layers + "\n"

    def title(self, msg):
        caption = "> [!NOTE]" + "\n>\n"
        caption += f"> **{msg}**\n"
        return caption

    def error_header(self):
        return ["> [!CAUTION]\n> "]

    def warning_header(self):
        return ["> [!WARNING]\n> "]

    def error_end(self):
        self.out[-1] += "\n"

    def warning_end(self):
        self.out[-1] += "\n"

    def display(self, tree):
        return "\n".join(tree) + "\n"

    def display_error(self, level, tree, msg, default_color):
        if default_color == "errors":
            color = self.error_color
        else:
            color = self.warning_color
        if isinstance(msg, tuple):
            if len(msg) == 3:
                if msg[2] == "error":
                    color = self.error_color
                    icon = self.error_icon
                else:
                    color = self.warning_color
                    icon = self.warning_icon
                msg = f"{msg[0]}: :{icon}: {self.set_color(color, msg[1])}"
            else:
                if msg[1] == "error":
                    icon = self.error_icon
                else:
                    color = self.warning_color
                    icon = self.warning_icon
                msg = f"<span style='color: {color}'>:{icon}: {msg[0]}</span>"
        tree.append("> " + " " * (level - 1) * 2 + "- " + msg)
        return tree

    def add_variable(
        self,
        parent,
        description,
        value,
        icon,
        level,
    ):
        if parent is None:
            parent = []
        if icon == "leaf":
            icon = "notebook"
        else:
            icon = "open_file_folder"
        before = " " * (level - 1) * 2 + "- "
        if isinstance(value, list):
            description = description.replace("◀", "←")
            subtree = parent.append(
                f"{before}:{icon}: " + _("{0}:").format(description),
            )
            before = " " * (level * 2) + "- "
            for val in value:
                parent.append(before + str(val))
        else:
            parent.append(
                before + f":{icon}: " + _("{0}: {1}").format(description, value)
            )

    def set_color(self, color, msg):
        if not color:
            return msg
        return f'<span style="color: {color}">{msg}</span>'

    def set_loaded(self, msg):
        return f" ← {msg}"

    def get_tree(self):
        return None

    def has_variable(self):
        return self.out != []

    def get_parent(self, parent, description, level):
        if parent is None:
            parent = []
            parent.append(description)
        else:
            parent.append(
                " " * (level - 1) * 2 + "- " + ":open_file_folder: " + description
            )
        return parent
