import threading
from enum import Enum

from sysactions import get_model

local_thread_data = threading.local()


class ThreadLocalKey(Enum):
    model = "model"
    pos_id = "pos_id"
    user_level = "user_level"
    order_service = "order_service"
    pos_service = "pos_service"


def get_thread_var(varname):
    var = getattr(local_thread_data, varname, None)
    return var


def set_thread_var(varname, value):
    setattr(local_thread_data, varname, value)


def get_current_pos_id():
    if get_thread_var("pos_id"):
        return get_thread_var("pos_id")

    return None


def get_current_model():
    if get_thread_var("model"):
        return get_thread_var("model")

    model = get_model(get_current_pos_id())
    set_thread_var("model", model)
    return model
