class RaUtil:

    def __init__(self, Components):
        self.components = Components


    def getColor(self, color):
        """
        Convertiert einen Wert in eine unterstützte Farbe

        :param color: Die angegebene Farbe die Convertiert werden soll

        :returns: Gibt die convertierte Farbe zurück
        :rtype: list|str
        """
        if isinstance(color, str):
            if "," in color:
                c = color.split(",")
                if len(c)==3:
                    return (int(c[0].strip()),int(c[1].strip()),int(c[2].strip()))
                elif len(c)==4:
                    return (int(c[0].strip()),int(c[1].strip()),int(c[2].strip()),int(c[3].strip()))
            elif "#" in color:
                return self.hex_to_rgb(color)
            return color
        elif isinstance(color, int):
            return color
        elif "r" in color and "g" in color and "b" in color:
            if "a" in color:
                return (color["r"],color["g"],color["b"],color["a"])
            else:
                return (color["r"],color["g"],color["b"])

    def hex_to_rgb(self,value:str):
        """
        Convertiert Hex zu RGB

        :param color: Die angegebene Farbe die Convertiert werden soll

        :returns: Gibt die convertierte Farbe zurück
        :rtype: list
        """
        value = value.lstrip('#')
        lv = len(value)
        return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))

    def rgb_to_hex(self,rgb):
        return '#%02x%02x%02x' % rgb


    def ExecuteFunc(self,s : str):
        print("Execute: " + s)
        if "." in s:
            args = []
            sp = s.split(".")
            c = sp[0].lower().strip()
            if "(" in sp[1]:
                if ")" in sp[1]:
                    tmpParam=sp[1].split("(")[1].split(")")[0].strip()
                    args = tmpParam.split(",")
                else:
                    print("Error Function declation")
            func = None
            if c=="ramenu":
                func = getattr(self.components.Menu,sp[1])
            elif c=="radisplay":
                func = getattr(self.components.Display,sp[1])
            elif c=="ranas":
                func = getattr(self.components.Nas,sp[1])
            elif c=="rakeypad":
                func = getattr(self.components.Keypad,sp[1])
            elif c=="rautil":
                func = getattr(self.components.Util,sp[1])
            elif c=="raaction":
                func = getattr(self.components.Action,sp[1])
            else:
                func = getattr(sp[0],sp[1])
            if func is not None:
                argc = len(args)
                if argc==0:
                    func()
                elif argc==1:
                    func(args[0])
                elif argc==2:
                    func(args[0],args[1])
                elif argc==3:
                    func(args[0],args[1],args[2])
                elif argc==4:
                    func(args[0],args[1],args[2],args[3])
                elif argc==5:
                    func(args[0],args[1],args[2],args[3],args[4])
                elif argc==6:
                    func(args[0],args[1],args[2],args[3],args[4],args[5])
            else:
                print("  Function does not exists")
        else:
            func = getattr(self,s)
            if func is not None:
                func()
            else:
                print("  Function does not exists")