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


class OutputFamily(CommonOutput):
    level = 20
    name = "github"
    variable_hidden_color = "brown"  # #A52A2A
    variable_normal_color = None
    value_unmodified_color = "darkgoldenrod"  # #B8860B
    value_modified_color = "darkgreen"  # #006400
    value_default_color = None
    error_color = 'bright_red'


    def run(self):
        ret = self.root.exporter()
        print(ret, self.out)
        return ret, "\n".join(self.out) + "\n"

    def print(self) -> None:
        ret, export = self.run()
        print(export)
        return ret

    def header(self):
        variables = []
        if self.root_family.variable_default_enable:
            variables.append(_("Variable"))
        if self.root_family.variable_hidden_enable:
            variables.append(f'<span style="color: {self.variable_hidden_color}">_("Unmodifiable variable")</span>')
        values = []
        if self.root_family.value_unmodified_enable:
            values.append(f'<span style="color: {self.value_unmodified_color}">_("Default value")</span>')
        if self.root_family.value_modified_enable:
            values.append(f'<span style="color: {self.value_modified_color}">_("Modified value")</span>')
        if self.root_family.value_default_enable:
            values.append(f'(:hourglass_flowing_sand: {_("Original default value")})')
        if not variables and not values:
            return ""
        caption = "> [!NOTE]" + "\n>\n"
        caption += f'> _("Caption")\n'
        if variables:
            caption += "> " + "\\\n> ".join(variables) + "\n"
        if values:
            caption += "> + " "\\\n> ".join(values) + "\n"
        
        if self.root.layer_datas:
            raise Exception('unsupported layers')
#            max_len = 0
#            for datas in self.root.layer_datas.values():
#                for data in datas.values():
#                    max_len = max(max_len, len(data))
#                display_layers = ["" for i in range(max_len)]
#            for datas in self.root.layer_datas.values():
#                for data in datas.values():
#                    last_index = len(data) - 1
#                    for idx in range(max_len):
#                        if last_index < idx:
#                            display_layers[idx] += "\n"
#                        else:
#                            display_layers[idx] += data[idx] + "\n"
#            layers.add_row(*[layer[:-1] for layer in display_layers])
#            self.out.append(Panel.fit(layers, title=_("Layers")))
        print('1', caption)
        self.out.append(caption)

    def error_header(self):
        self.out.append('> [!CAUTION]\n> ')

    def display_error(self, tree, error, level):
        if not level:
            self.out.append("> - :stop_sign: " + error)
        else:
            self.out.append("> " + " " * level + "- " + error)

    def display_warnings(
        self,
        warnings: list,
    ) -> None:
        tree = Tree(
            f"[bold][bright_yellow]:bell: {_('WARNINGS')}[/bright_yellow][/bold]",
            guide_style="bold bright_yellow",
        )
        for warning in warnings:
            tree.add(warning)
        print('ppp')
        self.out.append(tree)

    def add_variable(
        self, *args, **kwargs,
    ):
        key, value = super().add_variable(*args, **kwargs)
        if isinstance(value, list):
            subtree = self.get_tree().add(
                ":notebook: " + _("{0}:").format(key),
                guide_style="bold bright_blue",
            )
            for val in value:
                subtree.add(str(val))
        else:
            self.get_tree().add(":notebook: " + _("{0}: {1}").format(key, value))

    def colorize(
        self,
        values,
        option=None,
    ) -> str:
        multi = False
        ret = []
        default = []
        for idx, data in enumerate(values):
            value = data["value"]
            if isinstance(value, list):
                multi = True
            else:
                value = [value]
            for vidx, val in enumerate(value):
                if len(ret) == vidx:
                    ret.append("")
                    default.append(False)
                if idx:
                    if not default[vidx]:
                        if ret[vidx]:
                            ret[vidx] += " "
                        ret[vidx] += "("
                        default[vidx] = True
                    else:
                        ret[vidx] += " "
                    ret[vidx] += ":hourglass_flowing_sand: "
                if option:
                    val = self.convert_value(
                        option,
                        val,
                    )
                color = data["color"]
                if color is not None:
                    ret[vidx] += f"[{color}]{val}[/{color}]"
                else:
                    ret[vidx] += val
                loaded_from = data["loaded_from"]
                if loaded_from:
                    ret[vidx] += f" :arrow_backward: {loaded_from}"
        for idx in range(len(ret)):
            if default[idx]:
                ret[idx] += ")"
        if not multi:
            if not ret:
                return ""
            return ret[0]
        return ret

    def get_tree(self):
        if self.tree is None:
            if self.parent is None:
                tree = Tree
            else:
                tree = self.parent.add
#            if self.is_leader:
#                self.tree = tree(
#                    ":notebook: " + _("{0}:").format(self.family),
#                    guide_style="bold bright_blue",
#                )
#            elif self.no_icon:
            if self.no_icon:
                self.tree = tree(
                    self.family,
                    guide_style="bold bright_blue",
                )
            else:
                self.tree = tree(
                    f":open_file_folder: {self.family}",
                    guide_style="bold bright_blue",
                )
        return self.tree

