import re


class Generator:

    def __init__(self) -> None:
        self.screen_name: str = """<<name_of_screen>> = """
        self.screen_class: str = """Screen("<<name_of_screen>>") \\"""

    def re_sub_screen_name(self, pattern: str, repl: str, string: str) -> None:
        self.screen_name = re.sub(pattern=pattern,
                                             repl=repl,
                                             string=string)
    
    def re_sub_screen_class(self, pattern: str, repl: str, string: str) -> None:
        self.screen_class = re.sub(pattern=pattern,
                                             repl=repl,
                                             string=string)
    
    def re_sub_strings(self, pattern: str, repl: str, string: str):
        string = re.sub(pattern=pattern,
                                repl=repl,
                                string=string)
        return string

    def generate_text_tag(self, dictionary, identification_or_element):
        text_tag: str = """TextTag(text="<<text>>", method="<<method>>", mask="<<mask>>", threshold=<<threshold>>)"""

        for value in dictionary[identification_or_element]:
            if isinstance(value, dict):
                if list(value.keys())[0] == "text":
                    text_tag = self.re_sub_strings("<<text>>", value["text"], text_tag)
                elif list(value.keys())[0] == "method":
                    text_tag = self.re_sub_strings("<<method>>", value["method"], text_tag)
                elif list(value.keys())[0] == "Mask":
                    text_tag = self.re_sub_strings("<<mask>>", list(value["Mask"].keys())[0], text_tag)
                elif list(value.keys())[0] == "threshold":
                    text_tag = self.re_sub_strings("<<threshold>>", str(value["threshold"]), text_tag)

        return text_tag
    
    def generate_icon_tag(self, dictionary, identification_or_element):
        icon_tag: str = """IconTag(mask="<<mask>>", icon="<<icon>>")"""

        for value in dictionary[identification_or_element]:
            if isinstance(value, dict):
                if list(value.keys())[0] == "Mask":
                    icon_tag = self.re_sub_strings("<<mask>>", list(value["Mask"].keys())[0], icon_tag)
                elif list(value.keys())[0] == "icon":
                    icon_tag = self.re_sub_strings("<<icon>>", value["icon"], icon_tag)

        return icon_tag
    
    def generate_text_tool_tag(self, dictionary, identification_or_element):
        textool_tag: str = """TextToolTag(threshold=<<threshold>>, mask="<<mask>>", texttool="<<texttool>>")"""

        for value in dictionary[identification_or_element]:
            if isinstance(value, dict):
                if list(value.keys())[0] == "Mask":
                    textool_tag = self.re_sub_strings("<<mask>>", list(value["Mask"].keys())[0], textool_tag)
                elif list(value.keys())[0] == "threshold":
                    textool_tag = self.re_sub_strings("<<threshold>>", str(value["threshold"]), textool_tag)
                elif list(value.keys())[0] == "texttool":
                    textool_tag = self.re_sub_strings("<<texttool>>", value["texttool"], textool_tag)

        return textool_tag
    
    def generate_or_tag(self, dictionary, identification_or_element):
        """To be done in the future when the teleport is ready."""
        or_tag = ""
        pass

    def generate_placeholder_tag(self, dictionary, identification_or_element):
        placeholder_tag: str = """PlaceHolderTag(mask="<<mask>>", key="<<key>>", preprocessing_mode="<<preprocessing_mode>>")"""
        
        for value in dictionary[identification_or_element]:
            if isinstance(value, dict):
                if list(value.keys())[0] == "Mask":
                    placeholder_tag = self.re_sub_strings("<<mask>>", list(value["Mask"].keys())[0], placeholder_tag)
                elif list(value.keys())[0] == "key":
                    placeholder_tag = self.re_sub_strings("<<key>>", value["key"], placeholder_tag)
                elif list(value.keys())[0] == "preprocessing_mode":
                    placeholder_tag = self.re_sub_strings("<<preprocessing_mode>>", value["preprocessing_mode"], placeholder_tag)

        return placeholder_tag
