import os
import threading
import time
import unittest

from OpenDrive.client_side import file_changes_json as c_json
from OpenDrive.client_side import interface
from OpenDrive.client_side import main
from OpenDrive.client_side import paths as client_paths
from OpenDrive.server_side import paths as server_paths
from tests.client_side.helper_client import h_register_dummy_user_device_client
from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, \
    h_clear_init_all_folders, h_create_empty


class TestMain(unittest.TestCase):

    def setUp(self) -> None:
        h_clear_init_all_folders()
        self._server_process = h_start_server_process()
        self.folder1_abs_local_path = client_paths.normalize_path(client_paths.LOCAL_CLIENT_DATA, "folder1")
        h_create_empty(self.folder1_abs_local_path)
        main.MIN_UPDATE_PAUSE_TIME = 1

    def tearDown(self) -> None:
        main.shutdown()
        h_stop_server_process(self._server_process)

    @h_client_routine(clear_folders=False)
    def putest_start_logged_in(self):
        user = h_register_dummy_user_device_client()
        main_thread = threading.Thread(target=main.start, daemon=True)
        main_thread.start()
        time.sleep(2)   # wait till changes.json is created

        interface.add_sync_folder(self.folder1_abs_local_path, "folder1")
        expected_content = c_json.get_all_data()
        file_path = os.path.join(self.folder1_abs_local_path, "dummy.txt")
        with open(file_path, "w") as f:
            f.write("Hello World")
        time.sleep(5)   # wait till synchronization finished
        expected_path = os.path.join(server_paths.get_users_root_folder(user.user_id), "folder1/dummy.txt")
        self.assertTrue(os.path.exists(expected_path), "dummy file is not pulled to server!")
        self.assertEqual(expected_content, c_json.get_all_data())
        time.sleep(1)   # wait till waiting...

[(29, 'tests.helper_all.h_client_routine', 'h_client_routine', False, 'from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, h_clear_init_all_folders, h_create_empty\n'), (19, 'tests.helper_all.h_clear_init_all_folders', 'h_clear_init_all_folders', False, 'from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, h_clear_init_all_folders, h_create_empty\n'), (20, 'tests.helper_all.h_start_server_process', 'h_start_server_process', False, 'from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, h_clear_init_all_folders, h_create_empty\n'), (21, 'OpenDrive.client_side.paths.normalize_path', 'client_paths.normalize_path', True, 'from OpenDrive.client_side import paths as client_paths\n'), (22, 'tests.helper_all.h_create_empty', 'h_create_empty', False, 'from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, h_clear_init_all_folders, h_create_empty\n'), (26, 'OpenDrive.client_side.main.shutdown', 'main.shutdown', False, 'from OpenDrive.client_side import main\n'), (27, 'tests.helper_all.h_stop_server_process', 'h_stop_server_process', False, 'from tests.helper_all import h_client_routine, h_start_server_process, h_stop_server_process, h_clear_init_all_folders, h_create_empty\n'), (31, 'tests.client_side.helper_client.h_register_dummy_user_device_client', 'h_register_dummy_user_device_client', False, 'from tests.client_side.helper_client import h_register_dummy_user_device_client\n'), (32, 'threading.Thread', 'threading.Thread', False, 'import threading\n'), (34, 'time.sleep', 'time.sleep', False, 'import time\n'), (36, 'OpenDrive.client_side.interface.add_sync_folder', 'interface.add_sync_folder', False, 'from OpenDrive.client_side import interface\n'), (37, 'OpenDrive.client_side.file_changes_json.get_all_data', 'c_json.get_all_data', True, 'from OpenDrive.client_side import file_changes_json as c_json\n'), (38, 'os.path.join', 'os.path.join', False, 'import os\n'), (41, 'time.sleep', 'time.sleep', False, 'import time\n'), (45, 'time.sleep', 'time.sleep', False, 'import time\n'), (42, 'OpenDrive.server_side.paths.get_users_root_folder', 'server_paths.get_users_root_folder', True, 'from OpenDrive.server_side import paths as server_paths\n'), (43, 'os.path.exists', 'os.path.exists', False, 'import os\n'), (44, 'OpenDrive.client_side.file_changes_json.get_all_data', 'c_json.get_all_data', True, 'from OpenDrive.client_side import file_changes_json as c_json\n')]