from flask import request
from encrypt_decrypt import EncryptDecrypt
from static_functions import StaticFunctions
from vendor_static_functions import VendorStaticFunctions
import json


class Components:

    @classmethod
    def execute_component(cls, function_path, argument):
        from key_resolver import KeyResolver
        resolved_arg = ()
        body = request.get_json
        arg = next(iter(argument))
        arg = arg.split(',')
        for param in arg:
            if param == '':
                continue
            resolved_value = KeyResolver().resolve_key(param, body)
            resolved_arg = resolved_arg + (resolved_value,)
        if function_path in ['encrypt', 'decrypt']:
            encrypted_value = getattr(EncryptDecrypt, function_path)(*resolved_arg)
            return encrypted_value
        if function_path == 'upper':
            return str(resolved_arg[0]).upper()
        elif function_path == 'json_loads':
            return json.loads(resolved_arg[0]) if resolved_arg[0] else None
        if hasattr(StaticFunctions, function_path):
            result = getattr(StaticFunctions, function_path)(*resolved_arg)
        else:
            result = getattr(VendorStaticFunctions, function_path)(*resolved_arg)
        return result
