from konabot.plugins.marchtoy.obj import Object
from konabot.plugins.marchtoy.utilities import ArgParser

OPERATION_ENTRIES = {}

def make_operation(*name: str):
    def decorator(op):
        # OPERATION_ENTRIES[name] = op
        for alias in [*name]:
            OPERATION_ENTRIES[alias] = op
        return op

    return decorator


@make_operation("pos", "translate", "position", "p")
def translate(obj: Object, args: list[str]):
    pos = ArgParser.as_vec3(args)
    obj.transform.translate(pos[0], pos[1], pos[2])


@make_operation("rot", "rotate", "r")
def rotate(obj: Object, args: list[str]):
    pos = ArgParser.as_vec3(args)
    obj.transform.rotate(pos[0], pos[1], pos[2])


@make_operation("lookat", "look", "l")
def lookat(obj: Object, args: list[str]):
    pos = ArgParser.as_vec3(args)
    obj.transform.lookat(pos[0], pos[1], pos[2])


@make_operation("color", "col", "texture")
def color(obj: Object, args: list[str]):
    try:
        if len(args) == 1:
            col = ArgParser.as_literal_color(args)
            obj.texture.color = (col[0], col[1], col[2], col[3])
        elif len(args) == 3:
            col = ArgParser.as_vec3(args)
            obj.texture.color = (col[0], col[1], col[2], 1.0)
        elif len(args) == 4:
            col = ArgParser.as_vec4(args)
            obj.texture.color = (col[0], col[1], col[2], col[3])
        else:
            raise Exception("unknown color")

    except:
        raise Exception("unknown color")
