import re
from typing import Tuple

from tarka.utility.file.name import split_extension
from tarka.utility.file.reserve import ReserveFile

from clarabot_nano_common.utility.path.validate import MAX_NAME_LEN


class ReserveTaggedFile(ReserveFile):
    """
    To easily differentiate or filter out files for any reason they can be tagged.
    """

    def __init__(self, prefix_tag: str, suffix_tag: str):
        if not prefix_tag and not suffix_tag:
            raise Exception("some tag is required")
        ReserveFile.__init__(self, MAX_NAME_LEN)
        self.prefix_tag = prefix_tag
        self.suffix_tag = suffix_tag
        self.tag_re = re.compile(rf"^{re.escape(prefix_tag)}.+{re.escape(suffix_tag)}$")

    def _adjust_name_direct(self, name: str) -> Tuple[str, str]:
        file_name, file_extension = split_extension(name)
        return self.prefix_tag + file_name, file_extension + self.suffix_tag


DRIVE_PUT_FILE = ReserveTaggedFile(".~CbN.", "#")


