import base64
from pathlib import Path

import cv2
from fastapi.testclient import TestClient
from mocka.main import get_application
from mocka.configuration import Configuration, Server
from mocka.octiva import Artefact

client = TestClient(get_application(Configuration(Server("localhost", 8585))))


# def test_read_main():
#     response = client.get("/", headers={})
#     assert response.status_code == 201
#     assert response.json() == {
#                 "port": "ok",
#                 "output": {
#                     "artefact_1": "<uri>",
#                     "artefact_2": "<uri>",
#                 }
#             }

# def test_post_main():
#     file_path = Path(__file__).parent.parent / Path("data/mock_requirements.txt")
#     requirements = file_path.read_text()
#     mock_input = {
#         "ctrl": "cin",
#         "input": {
#             "din": {
#                 "type": "inline",
#                 "content": requirements,
#                 "encoding": "text/plain"
#             }
#         }
#     }
#     response = client.post("/", json=mock_input)
#     assert response.status_code == 200
#     assert response.json() == {
#         "ctrl": "ok",
#         "output": {
#             "dout": {
#                 "type": "inline",
#                 "content": requirements + "\n\nChecked!",
#                 "name": file_path.name,
#                 "encoding": "text/plain"
#             }
#         }
#     }


def test_post_octiva():
    file_path = Path(__file__).parent.parent / Path("tests/octiva/rails_0.jpg")
    rail_image = cv2.imread(str(file_path))
    retval, buffer = cv2.imencode(file_path.suffix, rail_image)
    data = base64.b64encode(buffer).decode()
    image_artefact = Artefact("inline", data, "", "base64")
    algo_artefact = Artefact("inline", f"print('hello')", "", "text/plain")

    mock_input = {
        "ctrl": "cin",
        "input": {
            "image": {
                "type": image_artefact.type,
                "content": image_artefact.content,
                "name": image_artefact.name,
                "encoding": image_artefact.encoding,
            },
            "algorithm": {
                "type": algo_artefact.type,
                "content": algo_artefact.content,
                "name": algo_artefact.name,
                "encoding": algo_artefact.encoding,
            },
        }
    }
    response = client.post("/octiva/", json=mock_input)
    assert response.status_code == 200

    temp_image_loc = Path(__file__).parent.parent / Path("data") / Path("rail_image_annotated.jpg")
    with temp_image_loc.open("rb") as image_file:
        data = base64.b64encode(image_file.read()).decode()
    annotated_image_artefact = Artefact("inline", data, "", "base64")
    expected_data_content =  "D: [[[108 847 108   0]]\n\n [[477 847 477   0]]]\nP: [0, 1]"
    data_artefact = Artefact("inline", expected_data_content, "", "text/plain")
    assert response.json() ==  {
        "ctrl": "ok",
        "output": {
            "image": {
                "type": annotated_image_artefact.type,
                "content": annotated_image_artefact.content,
                "name": annotated_image_artefact.name,
                "encoding": annotated_image_artefact.encoding
            },
            "data": {
                "type": data_artefact.type,
                "content": data_artefact.content,
                "name": data_artefact.name,
                "encoding": data_artefact.encoding
            },
        }
    }
