import importlib
import logging
import os
from typing import Optional
from typing import (
    Optional,
    Dict,
)

# DO NOT REMOVE THIS - IT FIX A CYCLIC IMPORT
from application.domain import I18nImpl, I18n
from application.repository.impl import MwI18nRepository

from os.path import dirname, isfile, join
from msgbus import MBEasyContext

mb_context = None  # type: Optional[MBEasyContext]
logger = logging.getLogger("PosActions")

USECS_PER_SEC = 1000000
pos_config = None


class PosConfig(object):
    def __init__(
        self,
        max_transfer_value,                           # type: float
        skim_digit_limit,                             # type: Dict[str, int]
        sangria_levels,                               # type: str
        auto_generate_skim_number,                    # type: bool
        store_id,                                     # type: str
        min_value_to_ask_bordereau_justify_config,    # type: float
        ask_cash_out_justification,                   # type: bool
        keyboard_skim_numeric=True,                   # type: Optional[bool]
    ):

        self.max_transfer_value = max_transfer_value
        self.skim_digit_limit = skim_digit_limit
        self.sangria_levels = sangria_levels
        self.auto_generate_skim_number = auto_generate_skim_number
        self.store_id = store_id
        self.min_value_to_ask_bordereau_justify_config = min_value_to_ask_bordereau_justify_config
        self.keyboard_skim_numeric = keyboard_skim_numeric
        self.ask_cash_out_justification = ask_cash_out_justification


def get_modules(verify_file):
    mods = []
    for dir_path, dir_names, filenames in os.walk(dirname(__file__)):
        if dir_path == dirname(__file__):
            continue

        path_list = [join(dirname(__file__), "util"), join(dirname(__file__), "models")]
        if not verify_file(dir_path, path_list):
            continue

        for filename in filenames:
            if isfile(join(dir_path, filename)) and not filename.endswith("__init__.py") and filename.endswith(".py"):
                module_name = join(dir_path, filename)[len(dirname(__file__)) + 1:].replace(os.path.sep, '.')[:-3]
                mods.append("actions." + module_name)
    return mods

def verify_util_modules(path, path_list):
    return path in path_list

def verify_all_modules(path, path_list):
    return path not in path_list

def import_all_modules():
    for module in all_modules:
        importlib.import_module(module)

def import_util_modules():
    for module in util_modules:
        importlib.import_module(module)

def set_mb_context(mb_context_param):
    global mb_context
    mb_context = mb_context_param

def set_pos_config(pos_config_param):
    # type: (PosConfig) -> None

    global pos_config
    pos_config = pos_config_param

    for m in all_modules:
        reload(importlib.import_module(m))
    for m in util_modules:
        reload(importlib.import_module(m))


all_modules = get_modules(verify_all_modules)
all_modules = sorted(all_modules)

util_modules = get_modules(verify_util_modules)
util_modules = sorted(util_modules)

import_all_modules()
import_util_modules()
