import os
import tempfile
import pytest
import hevclient
from tests.integration.integration_utils import start_background_arduinosim_hevserver, \
    kill_process_group, assert_posix, assert_pythonpath
from NativeUI import set_window_size, interpret_resolution
from NativeUI import NativeUI
import json

hevclient.mmFileName = tempfile.gettempdir() + os.path.sep + "HEVCLIENT_last_Data.mmap"

PERSONAL_JSON = json.load(
    open(
        os.environ["PYTHONPATH"].split(os.pathsep)[0]
        + "/tests/unittests/fixtures/personalSample.json"
    )
)
BATTERY_JSON = json.load(
    open(
        os.environ["PYTHONPATH"].split(os.pathsep)[0]
        + "/tests/unittests/fixtures/batterySample.json"
    )
)

@pytest.fixture(scope="class")
def background_procs():
    """
    Fixutre to start the Arduino emulator and hevserver as background
    processes and kill the processes after the test.
    """
    background_processes = start_background_arduinosim_hevserver()
    yield background_processes
    if background_processes is not None:
        for proc in background_processes:
            kill_process_group(proc.pid)

@pytest.fixture()
def native_ui(qtbot):
    """
    Fixture to inject the NativeUI. Fixture waits until GUI is shown.
    """
    native_gui = NativeUI()
    set_window_size(
        native_gui,
        windowed=False,
    )
    qtbot.addWidget(native_gui)
    native_gui.show()
    qtbot.waitExposed(native_gui)
    return native_gui

class TestPersonalDisplay:
    def test_personal_handler_correct_initial_text(background_procs, native_ui, qtbot):
        """
        Create the NativeUI with background processes in play. Manually send a personal 
        payload to the NativeUI get_updates method and confirm that the display updates 
        accordingly.
        """
        assert_posix()
        assert_pythonpath()
        assert str(native_ui.widgets.personal_display.info_label.text()) == "No personal information set"


    def test_personal_handler_correct_initial_text(background_procs, native_ui, qtbot):
        """
        Create the NativeUI with background processes in play. Manually send a personal 
        payload to the NativeUI get_updates method and confirm that the display updates 
        accordingly.
        """
        assert_posix()
        assert_pythonpath()
        native_ui.get_updates(PERSONAL_JSON)
        assert str(native_ui.widgets.personal_display.info_label.text()) == "Justin Atest, 1.77m"

class TestBatteryDisplay:
    def test_battery_display_correct_initial_text(background_procs, native_ui, qtbot):
        assert_posix()
        assert_pythonpath()
        assert native_ui.widgets.battery_display.status == {
            "on_mains_power": False,
            "on_battery_power": True,
            "battery_percent": 0,
            "electrical_problem": "No Battery Info",
        }
        assert str(native_ui.widgets.battery_display.text_display.text()) == "No Battery Info"

    def test_battery_display_updates_on_battery_payload_mains(background_procs, native_ui, qtbot):
        assert_posix()
        assert_pythonpath()
        native_ui.get_updates(BATTERY_JSON)
        assert native_ui.widgets.battery_display.status == {
            "on_mains_power": True,
            "on_battery_power": False,
            "battery_percent": 85,
            "electrical_problem": None
        }
        assert str(native_ui.widgets.battery_display.text_display.text()) == ""

    def test_battery_display_updates_on_battery_payload_bat(background_procs, native_ui, qtbot):
        assert_posix()
        assert_pythonpath()
        native_ui.get_updates(
            {
                "type":"BATTERY",
                "BATTERY":{
                    "version": 182,
                    "timestamp": 0,
                    "type": "BATTERY",
                    "bat": 1,
                    "ok": 0,
                    "alarm": 0,
                    "rdy2buf": 0,
                    "bat85": 1,
                    "prob_elec": 0,
                    "dummy": False
                },
                "alarms":[]
            }
        )
        assert native_ui.widgets.battery_display.status == {
            "on_mains_power": False,
            "on_battery_power": True,
            "battery_percent": 85,
            "electrical_problem": None
        }
        assert str(native_ui.widgets.battery_display.text_display.text()) == "85.0 %"




