import json
from typing import Dict

from library_test.mock_manage.mock_handlers.mock_cmd_handler import MockCmdHandler


class AnsibleModule:

    def __init__(self, params: Dict, cmd_handler: MockCmdHandler, *args, **kwargs):
        self.params = params
        self.cmd_handler = cmd_handler

    class FailJson(Exception):
        pass

    class ExitJson(Exception):
        pass

    def fail_json(self, **kwargs):
        res = json.dumps(kwargs, indent=4)
        print(res)
        raise AnsibleModule.FailJson(res)

    def exit_json(self, **kwargs):
        res = json.dumps(kwargs, indent=4)
        print(res)
        raise AnsibleModule.ExitJson(res)

    def run_command(self, cmd, *args, **kwargs):
        return self.cmd_handler.run_command(cmd)

    def get_bin_path(self, bin_part):
        return self.cmd_handler.find_bin_path(bin_part)

    def log(self, msg, level="", module="", exception=""):
        all_msg = ""
        if module:
            all_msg += f'module:{module}'
        if level:
            all_msg += f'[{level}]'
        all_msg += f'msg={msg}'
        if exception:
            all_msg += f' exception={exception}'
        print(all_msg)