from os import path
import re

class Config:
    def __init__( self ) -> None:
        # The signals to create
        # This is a Dictionary[key, value] where:
        #        key = Name of widget
        #        value = Tuple[n] of
        #            value[i] = Tuple[2] of
        #                 value[0] = signal
        #                 value[1] = arguments
        self.handlers = \
            {
                "QPushButton"       : [["clicked", ""]],
                "QAction"           : [["triggered", ""]],
                "QCommandLinkButton": [["clicked", ""]],
                "QToolButton"       : [["clicked", ""]],
                "QDialogButtonBox"  : [["accepted", ""], ["rejected", ""]],
            }
        
        # System command to run pyrcc
        self.resource_command = 'pyrcc5 "{0}" -o "{1}"'
        
        # System command to run pyuic
        self.ui_command = 'pyuic5 "{0}" -o "{1}"'
        
        # How we format the signal handlers, where:
        #       0: Name
        #       1: Type
        #       2: Signal
        #       3: Params
        #       4: function_format (obviously not available when defining function_format itself)
        #       5: Formatted params (including "self")
        self.command_format = \
            """@exqtSlot({3})
            def {4}({5}) -> None:
                \"\"\"
                Signal handler:
                \"\"\"
                pass
            """
        
        # How we format the function names (same format as for `command_format`)
        self.function_format = "on_{0}_{2}"
        
        # How we find our widgets
        self.widget_regex = r"self\.(.*?) = QtWidgets\.(.*?)\("
        
        # How we find garbage
        # This is a Dictionary[key, value] where:
        #   key = find
        #   value = replace
        self.garbage_regex = [
            [re.escape( r"def setupUi(self, Dialog):" ), r"def __init__(self, Dialog):"],  # Swap our setupUi method for a constructor, so the IDE can see the field types properly
            [re.escape( r"QtWidgets.QDockWidget.DockWidgetFeatureMask" ), r"QtWidgets.QDockWidget.DockWidgetClosable | QtWidgets.QDockWidget.DockWidgetFloatable | QtWidgets.QDockWidget.DockWidgetMovable | QtWidgets.QDockWidget.DockWidgetVerticalTitleBar"],
            [re.escape( r"import resources_rc" ), ""]
        ]
        
        #
        # The extensions below assume the following structure as default, but they can be modified
        #
        # \xxx.qrc  <- resources
        # \xxx_rc.py <- resources autogenerated code
        #
        # ...and...
        #
        # \xxx.py   <- GUI logic
        # \xxx\xxx_designer.ui <- GUI designer
        # \xxx\xxx_designer.py <- GUI autogenerated code
        
        # QRC input suffix
        # Used when searching for resource files
        self.extension_qrc = ".qrc"
        
        # UI input suffix
        # Used when searching for interface files
        self.extension_ui = "_designer.ui"
        
        # QRC output suffix
        # This is added to the filename of the generated "py" resource files
        # The original suffix noted in `extension_qrc` is removed
        self.extension_qrc_py = "_rc.py"
        
        # UI output suffix
        # This is added to the filename of the generated "py" interface files
        # The original suffix noted in `extension_ui` is removed
        self.extension_ui_py = "_designer.py"
        
        # UI output prefix (added to start of filename)
        # This is added to the start of the filename of the generated "py" interface files
        # It can be blank
        self.prefix_ui_py = ""
        
        # UI logic output suffix (replaces `extension_ui`)
        # This is added to the UI filename when finding the logic "py" files
        # The original suffix noted in `extension_ui` is removed
        self.extension_ui_logic_py = ".py"
        
        # UI logic prefix (added to start of filename)
        # This is added to the start of the filename when finding the logic "py" files
        # It can be blank
        # As a prefix it can also specify a folder.
        # The default is "..", which assumes the user wants the designer files in a subfolder below the code files
        self.prefix_ui_logic_py = ".." + path.sep


def __fix_config( x: str ) -> str:
    return x.replace( "/", path.sep ).replace( "\\", path.sep )


def load():
    config = Config()
    
    config.prefix_ui_logic_py = __fix_config( config.prefix_ui_logic_py )
    config.prefix_ui_py = __fix_config( config.prefix_ui_py )
    
    return config
