import json

from custom_sysactions import user_authorized_to_execute_action
from sysactions import action

from .service_period_commons import get_selection_service_period


@action
@user_authorized_to_execute_action
def do_service_period_table(pos_id):
    # type: (int) -> bool

    from msgbusboundary import MsgBusTableService as tableService
    from table_actions import create_table_modified_event

    selected_table = tableService().get_selected_table(pos_id)
    if not selected_table:
        return False

    service_period = get_selection_service_period(pos_id)
    if service_period is None:
        return False

    service_period_json = json.dumps(service_period)
    tableService().set_table_custom_property(pos_id, "SERVICE_PERIOD_TABLE", service_period_json, selected_table.id)
    table_picture = tableService().get_table_picture(pos_id, selected_table.id)

    create_table_modified_event(pos_id, table_picture)
    return True
