import random
import time

import sysactions
from tablemgrapi import get_pos_ts
from xml.etree import cElementTree as eTree

large_data = None


@sysactions.action
def perform_network_test(
    pos_id,
):
    # type: (...) -> str

    global large_data
    start_time = time.time()
    if large_data is None:
        large_data = ""
        for i in range(1, 1 * 1024 * 1024):
            large_data += "b"

    model = sysactions.get_model(pos_id)
    if model is not None:
        pos_ot = sysactions.get_posot(model)

        current_order = sysactions.get_current_order(model)
        if current_order is not None:
            order_id = current_order.get("orderId")
            if order_id is not None:
                order_id = int(order_id)
                rand_order_id = random.randint(1, order_id)

                pos_ot.getOrderCustomProperties(orderid=str(rand_order_id))

        pos_ts = get_pos_ts(model)
        store_picture = pos_ts.storePicture(force_update=True)
        store_picture_xml = eTree.XML(store_picture)
        opened_table = store_picture_xml.find("Table[@status='3']")
        if opened_table is not None:
            table_id = opened_table.attrib["id"]
            pos_ts.getTablePicture(table_id)
    end_time = time.time()
    process_time = int((end_time - start_time) * 1000)
    return "{}-{}".format(process_time, large_data)
