import time
import unittest
import builtins
from steamvr.lighthouse_console import LighthouseConsole, LighthouseDeviceNotFound
from steamvr.unbuffered.lighthouse import static_vars
from subprocess import Popen
import logging
from unittest.mock import patch
import os

logging.basicConfig(level=logging.DEBUG)

class TestLighthouseUnbuffered(unittest.TestCase):
    @patch('steamvr.lighthouse_console.Popen')
    def test_list_devices_no_devices(self, mock_popen):
        # mock the readline function to return the contents of a file
        with open('./mock_data/lighthouse_no_devices.txt', 'rb') as f:
            mock_popen.return_value.stdout = f
            self.assertRaises(LighthouseDeviceNotFound, LighthouseConsole)

    @patch('steamvr.lighthouse_console.Popen')
    def test_list_devices_four_devices(self, mock_popen):
        # mock the readline function to return the contents of a file
        with open('./mock_data/lighthouse_list_multiple_devices.txt', 'rb') as f:
            mock_popen.return_value.stdout = f
            self.console = LighthouseConsole()
            self.console.open()
            device_list = self.console.list_devices()
            self.assertEqual(len(device_list), 4)
            self.assertEqual(device_list[0], 'LHR-F0B63E7D')
            self.assertEqual(device_list[1], 'LHR-735174D6')
            self.assertEqual(device_list[2], 'LHR-556BB9F2')
            self.assertEqual(device_list[3], 'LHR-80EAD668')

    def test_list_devices_four_devices(self):
        # mock the readline function to return the contents of a file
        console = LighthouseConsole()
        console.open()
        device_list = console.list_devices()
        self.assertEqual(len(device_list), 4)
        self.assertIn('LHR-80EAD668', device_list)
        self.assertIn('LHR-735174D6', device_list)
        self.assertIn('LHR-556BB9F2', device_list)
        self.assertIn('LHR-F0B63E7D', device_list)

    def test_list_devices_four_devices(self):

        # mock the readline function to return the contents of a file
        console = LighthouseConsole()
        console.open()
        conf = console.download_config()
        self.assertIn("imu", conf)


    def test_pair(self):
        self.console.pair()
