import json
import os

from django.core.files.uploadedfile import SimpleUploadedFile
from django.urls import reverse

from console.tests.test_utils import TIMEOUT
from networkmap.api import update_connections_model_data

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEST_IMAGE = os.path.join(BASE_DIR, "tests", "test_data/controller.png")


def get_mocked_elk_data(file_path, mocked_search, mocked_update):
    with open(file_path) as f:
        data = json.load(f)
    mocked_search.return_value = data
    mocked_update.return_value = 'ok'
    update_connections_model_data()


def create_new_background_image(test_netmap, client, new_pic_name):
    url = reverse('api_add_netmap_background', kwargs={'current_netmap_id': test_netmap.pk})
    f = SimpleUploadedFile(name='controller.png', content=open(TEST_IMAGE, 'rb').read(), content_type='image/png')
    user_input = {'name': new_pic_name, 'network_map': test_netmap.pk,
                  'bounds': ['{"x": 0, "y": 0, "width": 100, "height": 100}'], 'url': f}
    client.post(url, data=user_input, follow=True, file=f)
