"""
Tools for simpler sieve filters generation.

This module is intented to facilitate the creation of sieve filters
without having to write or to know the syntax.

Only commands (control/test/action) defined in the ``commands`` module
are supported.
"""

import io
import sys
from typing import List, Optional, Tuple, TypedDict, Union
from typing_extensions import NotRequired

from sievelib import commands, tools
from sievelib.parser import Parser


class FilterAlreadyExists(Exception):
    pass


class Filter(TypedDict):
    """Type definition for filter."""

    name: str
    content: commands.Command
    enabled: bool
    description: NotRequired[str]


class FiltersSet:
    """A set of filters."""

    def __init__(
        self,
        name: str,
        filter_name_pretext: str = "# Filter: ",
        filter_desc_pretext: str = "# Description: ",
    ):
        """
        Represents a set of one or more filters.

        :param name: the filterset's name
        :param filter_name_pretext: the text that is used to mark a filter name
                                    (as comment preceding the filter)
        :param filter_desc_pretext: the text that is used to mark a filter
                                     description
        """
        self.name = name
        self.filter_name_pretext = filter_name_pretext
        self.filter_desc_pretext = filter_desc_pretext
        self.requires: List[str] = []
        self.filters: List[Filter] = []

    def __str__(self):
        target = io.StringIO()
        self.tosieve(target)
        ret = target.getvalue()
        target.close()
        return ret

    def __isdisabled(self, fcontent: commands.Command) -> bool:
        """Tells if a filter is disabled or not

        Simply checks if the filter is surrounded by a "if false" test.

        :param fcontent: the filter's name
        """
        if not isinstance(fcontent, commands.IfCommand):
            return False
        if not isinstance(fcontent["test"], commands.FalseCommand):
            return False
        return True

    def from_parser_result(self, parser: Parser) -> None:
        cpt = 1
        for f in parser.result:
            if isinstance(f, commands.RequireCommand):
                if type(f.arguments["capabilities"]) == list:
                    [self.require(c) for c in f.arguments["capabilities"]]
                else:
                    self.require(f.arguments["capabilities"])
                continue

            name = "Unnamed rule %d" % cpt
            description = ""
            for comment in f.hash_comments:
                if isinstance(comment, bytes):
                    comment = comment.decode("utf-8")
                if comment.startswith(self.filter_name_pretext):
                    name = comment.replace(self.filter_name_pretext, "")
                if comment.startswith(self.filter_desc_pretext):
                    description = comment.replace(self.filter_desc_pretext, "")
            self.filters += [
                {
                    "name": name,
                    "description": description,
                    "content": f,
                    "enabled": not self.__isdisabled(f),
                }
            ]
            cpt += 1

    def require(self, name: str):
        """Add a new extension to the requirements list

        :param name: the extension's name
        """
        name = name.strip('"')
        if name not in self.requires:
            self.requires += [name]

    def check_if_arg_is_extension(self, arg: str):
        """Include extension if arg requires one."""
        args_using_extensions = {":copy": "copy", ":create": "mailbox"}
        if isinstance(arg, str) and arg in args_using_extensions:
            self.require(args_using_extensions[arg])

    def __gen_require_command(self) -> Union[commands.Command, None]:
        """Internal method to create a RequireCommand based on requirements

        Called just before this object is going to be dumped.
        """
        if not len(self.requires):
            return None
        reqcmd = commands.get_command_instance("require")
        reqcmd.check_next_arg("stringlist", self.requires)
        return reqcmd

    #: Tests (other than header) that can be used as a condition's name
    supported_tests = (
        "true",
        "false",
        "size",
        "exists",
        "envelope",
        "address",
        "body",
        "currentdate",
    )

    def __add_string_arg(
        self, cmd: commands.Command, value: Union[str, List[str]]
    ) -> None:
        """Add a string or a string list argument to the given command

        Values are always quoted and escaped.

        :param cmd: the command to update
        :param value: the raw value(s)
        """
        if isinstance(value, (list, tuple)):
            cmd.check_next_arg("stringlist", [tools.quote(v) for v in value])
        else:
            cmd.check_next_arg("string", tools.quote(value))

    @staticmethod
    def __split_match_tag(tag: str) -> Tuple[str, bool]:
        """Split a negated match type, e.g. ":notis" gives (":is", True)."""
        if tag.startswith(":not"):
            return ":" + tag[4:], True
        return tag, False

    def __build_condition(
        self, condition: List[str], parent: commands.Command, tag: str
    ) -> commands.Command:
        """Translate a condition to a valid sievelib Command.

        :param list condition: condition's definition
        :param ``Command`` parent: the parent
        :param str tag: tag to use instead of the one included into :keyword:`condition`
        :rtype: Command
        :return: the generated command
        """
        cmd = commands.get_command_instance("header", parent)
        cmd.check_next_arg("tag", tag)
        self.__add_string_arg(cmd, condition[0])
        self.__add_string_arg(cmd, condition[2])
        return cmd

    def __create_filter(
        self,
        conditions: List[tuple],
        actions: List[tuple],
        matchtype: str = "anyof",
    ) -> commands.Command:
        """
        Create a new filter.

        A filter is composed of:
         * a name
         * one or more conditions (tests) combined together using ``matchtype``
         * one or more actions

        A condition must be given as a 3-uple of the form::

          (test's name, operator, value)

        An action must be given as a 2-uple of the form::

          (action's name, value)

        It uses the "header" test to generate the sieve syntax
        corresponding to the given conditions. When the first element
        is not the name of a supported test, it is considered as a
        header name. To match a header whose name collides with a
        test's name (``true``, ``size``, ``notexists``...), use the
        explicit form::

          ("header", header's name, operator, value)

        :param conditions: the list of conditions
        :param actions: the list of actions
        :param matchtype: "anyof" or "allof"
        """
        ifcontrol = commands.get_command_instance("if")
        mtypeobj = commands.get_command_instance(matchtype, ifcontrol)
        for c in conditions:
            negate = False
            cname = c[0]
            if cname == "header" and len(c) == 4:
                # explicit header test, never interpreted as something else
                c = c[1:]
                cname = None
            elif (
                isinstance(cname, str)
                and cname.startswith("not")
                and cname[3:] in self.supported_tests
            ):
                negate = True
                cname = cname[3:]
            if cname in ("true", "false"):
                cmd = commands.get_command_instance(cname, ifcontrol)
            elif cname == "size":
                cmd = commands.get_command_instance("size", ifcontrol)
                cmd.check_next_arg("tag", c[1])
                cmd.check_next_arg("number", c[2])
            elif cname == "exists":
                cmd = commands.get_command_instance("exists", ifcontrol)
                self.__add_string_arg(cmd, c[1:])
            elif cname == "envelope":
                cmd = commands.get_command_instance("envelope", ifcontrol, False)
                self.require("envelope")
                comp_tag, negated = self.__split_match_tag(c[1])
                negate = negate or negated
                cmd.check_next_arg("tag", comp_tag)
                self.__add_string_arg(cmd, c[2])
                self.__add_string_arg(cmd, c[3])
            elif cname == "address":
                cmd = commands.get_command_instance("address", ifcontrol, False)
                comp_tag, negated = self.__split_match_tag(c[1])
                negate = negate or negated
                cmd.check_next_arg("tag", comp_tag)
                for arg in c[2:]:
                    self.__add_string_arg(cmd, arg)
            elif cname == "body":
                cmd = commands.get_command_instance("body", ifcontrol, False)
                self.require(cmd.extension)
                cmd.check_next_arg("tag", c[1])
                comp_tag, negated = self.__split_match_tag(c[2])
                negate = negate or negated
                cmd.check_next_arg("tag", comp_tag)
                self.__add_string_arg(cmd, c[3:])
            elif cname == "currentdate":
                cmd = commands.get_command_instance("currentdate", ifcontrol, False)
                self.require(cmd.extension)
                next_arg_pos = 1
                if c[1] == ":zone":
                    cmd.check_next_arg("tag", c[1])
                    self.__add_string_arg(cmd, c[2])
                    next_arg_pos = 3
                comp_tag, negated = self.__split_match_tag(c[next_arg_pos])
                negate = negate or negated
                cmd.check_next_arg("tag", comp_tag, check_extension=False)
                next_arg_pos += 1
                if comp_tag == ":value":
                    self.require("relational")
                    self.__add_string_arg(cmd, c[next_arg_pos])
                    next_arg_pos += 1
                self.__add_string_arg(cmd, c[next_arg_pos])
                next_arg_pos += 1
                self.__add_string_arg(cmd, c[next_arg_pos:])
            else:
                # header command fallback
                comp_tag, negate = self.__split_match_tag(c[1])
                cmd = self.__build_condition(c, ifcontrol, comp_tag)
            if negate:
                not_cmd = commands.get_command_instance("not", ifcontrol)
                not_cmd.check_next_arg("test", cmd)
                cmd = not_cmd
            mtypeobj.check_next_arg("test", cmd)
        ifcontrol.check_next_arg("test", mtypeobj)

        for actdef in actions:
            action = commands.get_command_instance(actdef[0], ifcontrol, False)
            if action.extension is not None:
                self.require(action.extension)
            for arg in actdef[1:]:
                self.check_if_arg_is_extension(arg)
                if isinstance(arg, int):
                    atype = "number"
                elif isinstance(arg, list):
                    atype = "stringlist"
                elif arg.startswith(":"):
                    atype = "tag"
                else:
                    atype = "string"
                    arg = tools.quote(arg)
                action.check_next_arg(atype, arg, check_extension=False)
            ifcontrol.addchild(action)
        return ifcontrol

    def _unicode_filter_name(self, name) -> str:
        """Convert name to unicode if necessary."""
        return name.decode("utf-8") if isinstance(name, bytes) else name

    def __find(self, name: str) -> Optional[Filter]:
        """Return the filter with the given name, or None."""
        name = self._unicode_filter_name(name)
        for f in self.filters:
            if f["name"] == name:
                return f
        return None

    def filter_exists(self, name: str) -> bool:
        """Check if a filter with name already exists."""
        return self.__find(name) is not None

    def addfilter(
        self,
        name: str,
        conditions: List[tuple],
        actions: List[tuple],
        matchtype: str = "anyof",
    ) -> None:
        """Add a new filter to this filters set

        :param name: the filter's name
        :param conditions: the list of conditions
        :param actions: the list of actions
        :param matchtype: "anyof" or "allof"
        """
        name = self._unicode_filter_name(name)
        if self.filter_exists(name):
            raise FilterAlreadyExists
        ifcontrol = self.__create_filter(conditions, actions, matchtype)
        self.filters += [
            {
                "name": name,
                "content": ifcontrol,
                "enabled": True,
            }
        ]

    def updatefilter(
        self,
        oldname: str,
        newname: str,
        conditions: List[tuple],
        actions: List[tuple],
        matchtype: str = "anyof",
    ) -> bool:
        """Update a specific filter

        Instead of removing and re-creating the filter, we update the
        content in order to keep the original order between filters.

        :param oldname: the filter's current name
        :param newname: the filter's new name
        :param conditions: the list of conditions
        :param actions: the list of actions
        :param matchtype: "anyof" or "allof"
        """
        filter_def = self.__find(oldname)
        if not filter_def:
            return False
        newname = self._unicode_filter_name(newname)
        if newname != filter_def["name"] and self.filter_exists(newname):
            raise FilterAlreadyExists
        filter_def["name"] = newname
        filter_def["content"] = self.__create_filter(conditions, actions, matchtype)
        if not filter_def["enabled"]:
            return self.disablefilter(newname)
        return True

    def replacefilter(
        self,
        oldname: str,
        sieve_filter: commands.Command,
        newname: Optional[str] = None,
        description: Optional[str] = None,
    ) -> bool:
        """replace a specific sieve_filter

        Instead of removing and re-creating the sieve_filter, we update the
        content in order to keep the original order between filters.

        :param oldname: the sieve_filter's current name
        :param newname: the sieve_filter's new name
        :param sieve_filter: the sieve_filter object as get from
                             FiltersSet.getfilter()
        """
        filter_def = self.__find(oldname)
        if not filter_def:
            return False
        oldname = filter_def["name"]
        newname = oldname if newname is None else self._unicode_filter_name(newname)
        if newname != oldname and self.filter_exists(newname):
            raise FilterAlreadyExists
        filter_def["name"] = newname
        filter_def["content"] = sieve_filter
        if description is not None:
            filter_def["description"] = description
        if not filter_def["enabled"]:
            return self.disablefilter(newname)
        return True

    def getfilter(self, name: str) -> Union[commands.Command, None]:
        """Search for a specific filter

        :param name: the filter's name
        :return: the Command object if found, None otherwise
        """
        f = self.__find(name)
        if not f:
            return None
        if not f["enabled"]:
            return f["content"].children[0]
        return f["content"]

    def get_filter_matchtype(self, name: str) -> Union[str, None]:
        """Retrieve matchtype of the given filter."""
        flt = self.getfilter(name)
        if not flt:
            return None
        for node in flt.walk():
            if isinstance(node, (commands.AllofCommand, commands.AnyofCommand)):
                return node.__class__.__name__.lower().replace("command", "")
        return None

    def get_filter_conditions(self, name: str) -> Union[List[str], None]:
        """Retrieve conditions of the given filter."""
        flt = self.getfilter(name)
        if not flt:
            return None
        conditions = []
        negated = []
        for node in flt.walk():
            if isinstance(node, commands.NotCommand):
                negated.append(node["test"])
                continue
            if not isinstance(
                node,
                (
                    commands.HeaderCommand,
                    commands.SizeCommand,
                    commands.ExistsCommand,
                    commands.BodyCommand,
                    commands.EnvelopeCommand,
                    commands.AddressCommand,
                    commands.CurrentdateCommand,
                    commands.TrueCommand,
                    commands.FalseCommand,
                ),
            ):
                continue
            args = node.args_as_tuple()
            if node in negated:
                if node.name in ["exists", "size", "true", "false"]:
                    # negated by prefixing the test name: notexists, notsize...
                    args = ("not" + args[0],) + args[1:]
                else:
                    # negated by prefixing the match type: :notis, :notcontains...
                    pos = 1
                    if node.name == "body":
                        pos = 2
                    elif node.name == "currentdate" and args[1] == ":zone":
                        pos = 3
                    args = args[:pos] + (":not" + args[pos][1:],) + args[pos + 1 :]
            conditions.append(args)
        return conditions

    def get_filter_actions(self, name: str) -> Union[List[str], None]:
        """Retrieve actions of the given filter."""
        flt = self.getfilter(name)
        if not flt:
            return None
        actions: list = []
        for node in flt.walk():
            if isinstance(node, commands.ActionCommand):
                actions.append(node.args_as_tuple())
        return actions

    def removefilter(self, name: str) -> bool:
        """Remove a specific filter

        :param name: the filter's name
        """
        f = self.__find(name)
        if not f:
            return False
        self.filters.remove(f)
        return True

    def enablefilter(self, name: str) -> bool:
        """Enable a filter

        Just removes the "if false" test surrouding this filter.

        :param name: the filter's name
        """
        f = self.__find(name)
        if not f or not self.__isdisabled(f["content"]):
            return False
        f["content"] = f["content"].children[0]
        f["enabled"] = True
        return True

    def is_filter_disabled(self, name: str) -> bool:
        """Tells if the filter is currently disabled or not

        :param name: the filter's name
        """
        f = self.__find(name)
        return not f or self.__isdisabled(f["content"])

    def disablefilter(self, name: str) -> bool:
        """Disable a filter

        Instead of commenting the filter, we just surround it with a
        "if false { }" test.

        :param name: the filter's name
        :return: True if filter was disabled, False otherwise
        """
        f = self.__find(name)
        if not f or self.__isdisabled(f["content"]):
            return False
        ifcontrol = commands.get_command_instance("if")
        falsecmd = commands.get_command_instance("false", ifcontrol)
        ifcontrol.check_next_arg("test", falsecmd)
        ifcontrol.addchild(f["content"])
        f["content"] = ifcontrol
        f["enabled"] = False
        return True

    def movefilter(self, name: str, direction: str) -> bool:
        """Moves the filter up or down

        :param name: the filter's name
        :param direction: string "up" or "down"
        """
        f = self.__find(name)
        if not f:
            return False
        pos = self.filters.index(f)
        newpos = pos - 1 if direction == "up" else pos + 1
        if not 0 <= newpos < len(self.filters):
            return False
        self.filters.insert(newpos, self.filters.pop(pos))
        return True

    def dump(self, target=sys.stdout):
        """Dump this object

        Available for debugging purposes
        """
        target.write("Dumping filters set %s\n\n" % self.name)
        cmd = self.__gen_require_command()
        if cmd:
            target.write("Dumping requirements\n")
            cmd.dump(target=target)
            target.write("\n")

        for f in self.filters:
            target.write("Filter Name: %s\n" % f["name"])
            if "description" in f:
                target.write("Filter Description: %s\n" % f["description"])
            f["content"].dump(target=target)

    def tosieve(self, target=sys.stdout):
        """Generate the sieve syntax corresponding to this filters set

        This method will usually be called when this filters set is
        done. The default is to print the sieve syntax on the standard
        output. You can pass an opened file pointer object if you want
        to write the content elsewhere.

        :param target: file pointer where the sieve syntax will be printed
        """
        cmd = self.__gen_require_command()
        if cmd:
            cmd.tosieve(target=target)
            target.write("\n")
        for f in self.filters:
            # Name and description go in a hash comment: a line break
            # would end it and let the rest be read as sieve commands.
            target.write(
                "{}{}\n".format(
                    self.filter_name_pretext, " ".join(f["name"].splitlines())
                )
            )
            if "description" in f and f["description"]:
                target.write(
                    "{}{}\n".format(
                        self.filter_desc_pretext,
                        " ".join(f["description"].splitlines()),
                    )
                )
            f["content"].tosieve(target=target)


if __name__ == "__main__":
    fs = FiltersSet("test")

    fs.addfilter(
        "rule1",
        [
            ("Sender", ":is", "toto@toto.com"),
        ],
        [
            ("fileinto", "Toto"),
        ],
    )
    fs.tosieve()
